15: Object Object Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.

Slides:



Advertisements
Similar presentations
CS 211 Inheritance AAA.
Advertisements

Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
C# Programming: From Problem Analysis to Program Design1 Advanced Collections C# Programming: From Problem Analysis to Program Design 3 rd Edition 8.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
ABHISHEK BISWAS.NET Reflection Dynamically Create, Find and Invoke Types.
Topic 4 Inheritance.
Reflection.NET Support for Reflection. What is Reflection Reflection: the process by which a program can observe and modify its own structure and behavior.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
The Object class Object package java.lang Object clone equals hashCode toString aCopy toThis hash string ! yesOrNo.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
.Net Reflection Taipan Tamsare. Overview Reflection core concepts Exploring metadata Detail information Attributes Building Types at runtime.
METADATA IN.NET Presented By Sukumar Manduva. INTRODUCTION  What is Metadata ? Metadata is a binary information which contains the complete description.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 7 th Lecture Pavel Ježek
EKT472: Object Oriented Programming Overloading and Overriding Method.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Polymorphism.
CMSC 202 Polymorphism.
Overriding Method.
Chapter 11 Inheritance and Polymorphism
16: Equals Equals Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Ch 10- Advanced Object-Oriented Programming Features
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Lecture 17: Polymorphism (Part II)
Polymorphism.
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Advanced .NET Programming I 7th Lecture
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
5.1 Being Objects and A Glimpse into Coding
Implementing Polymorphism
Chapter 9 Inheritance and Polymorphism
Inheritance Basics Programming with Inheritance
Properties 7: Properties Programming C# © 2003 DevelopMentor, Inc.
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Polymorphism, Abstract Classes & Interfaces
Computer Programming with JAVA
Polymorphism CMSC 202, Version 4/02.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Lecture 6 Inheritance CSE /26/2018.
Lecture 18: Polymorphism (Part II)
CIS 199 Final Review.
Chapter 8 Class Inheritance and Interfaces
Chapter 8 Inheritance Part 2.
Based on Murach (ch 14) and Deitel 2012
Interface 11: Interface Programming C# © 2003 DevelopMentor, Inc.
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
Advanced .NET Programming I 8th Lecture
- This slide is intentionally left blank -
Lecture 6 Inheritance CSE /26/2018.
Presentation transcript:

15: Object Object Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Objectives Introduce the Object class concept references methods overriding methods Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Unified inheritance hierarchy 15: Object Unified inheritance hierarchy Type system is unified all types derive from System.Object root of type hierarchy Object Person Shape Student Employee Circle Rectangle Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Object class alias C# provides a convenient alias for Object lower case object object a; System.Object b; equivalent Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Motivation Unified inheritance hierarchy is useful 15: Object Motivation Unified inheritance hierarchy is useful Object reference is generic handle Object methods universally supported Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Object reference Object reference can refer to anything legal because all types are derived from Object object a = new Person (); object b = new Student (); object c = new Stock (); object d = new Rational(); use object references Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Access through Object reference Access through Object reference limited can only access Object members object o = new Person(); o.Name = "Bob"; error, not Object member class Person { public string Name; public int Age; ... } Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Cast Can cast to recover more specific reference 15: Object Cast Can cast to recover more specific reference allows access to entire object void Rename(object o) { Person p = (Person)o; p.Name = "Bob"; ... } cast access Person member Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Object methods Object class provides several methods provide basic services to all types class Object { public Type GetType () ... public virtual string ToString () ... public virtual int GetHashCode () ... public virtual bool Equals (object o) ... public static bool Equals (object a, object b) ... public static bool ReferenceEquals(object a, object b) ... protected void Finalize () ... protected object MemberwiseClone() ... } Object methods Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

GetType GetType gives access to type metadata returns Type object not virtual, cannot be overridden Person p = new Person("Bob", 32); Type t = p.GetType(); get Type object for Person Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Type object Type object contains complete information about the type runtime type information facility called reflection public class Type { public string FullName { get; } public bool IsAbstract { get; } public bool IsClass { get; } public bool IsPrimitive { get; } public bool IsInterface { get; } public Type BaseType { get; } public ConstructorInfo[] GetConstructors() ... public FieldInfo [] GetFields () ... public MethodInfo [] GetMethods () ... public Type [] GetInterfaces () ... ... } properties methods Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Unique Type object One Type object per type shared by all instances Person a = new Person("Ann", 27); Person b = new Person("Bob", 32); Type ta = a.GetType(); Type tb = b.GetType(); references are to same Type object Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

15: Object Object ToString Object version of ToString returns fully qualified type name Person p = new Person("Bob", 32); string s = p.ToString(); returns "Person" Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Simple type string conversion 15: Object Simple type string conversion Simple types have overridden ToString method returns string representation of data implicitly called when simple type used in string context int age = 32; string s = age.ToString(); string t = "Bob " + age; call explicitly called implicitly when used in string context Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Override ToString Common to override ToString virtual method 15: Object Override ToString Common to override ToString virtual method return more specific string containing object data class Person { public string Name; public int Age; public override string ToString() return Name + " " + Age; } ... override ToString Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Automatic use of ToString 15: Object Automatic use of ToString ToString sometimes called automatically when object used in string context inside WriteLine(object) method Person p = new Person("Bob", 32); string s = "Hello " + p; Console.WriteLine(p); calls ToString calls ToString Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Dynamic binding ToString is dynamically bound 15: Object Dynamic binding ToString is dynamically bound derived class version called through Object reference object o = new Person("Bob", 32); string s = o.ToString(); calls Person.ToString Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Hashing Hash table is efficient way to store key/value pairs 15: Object Hashing Hash table is efficient way to store key/value pairs Hash code computed on key to determine placement in table Person bob = new Person("Bob", 32); House home = new House(...); Hashtable addresses = new Hashtable(); addresses.Add(bob, home); hash code computed to position the new entry in the table Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Object GetHashCode Object supplies GetHashCode method called by library hash table when entry added few guarantees given about exact behavior of Object version public class Object { public virtual int GetHashCode() ... } object GetHashCode Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Override GetHashCode Can override virtual method GetHashCode 15: Object Override GetHashCode Can override virtual method GetHashCode typical motivation is to increase performance of hash tables goal is to get a random distribution of hash codes class Person { public override int GetHashCode() return Name.GetHashCode() * Age; } ... } override GetHashCode hash on Person data Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Summary Object class is root of type system all types are derived from Object Object reference is generic handle all types automatically convert to Object reference cast required when recovering original type Object methods provide basic operations supported by all types Programming C# © 2003 DevelopMentor, Inc. 12/1/2003