Download presentation
Presentation is loading. Please wait.
Published byMaryam Gibbins Modified over 10 years ago
1
Polymorphism
2
3 Fundamental Properties of OO 1. Encapsulation 2. Polymorphism 3. Inheritance These are the 3 building blocks of object-oriented design.
3
What is Polymorphism? poly-morph = many forms A method is polymorphic if it has "many forms". In O-O this means, the same method can apply to different classes of objects. Each class can implement it differently (many forms). Example: Object x = new String( "poly" ); out.print( x.toString() ); // "poly" x = new Date( ); out.print( x.toString() ); // "Mon Dec 14 09:00.."
4
Example: Arrays.sort( ) Arrays.sort( ) can sort any kind of object, as long as the object provides a compareTo( ) method. Arrays.sort can sort types that didn't exist when it was written and compiled. Coin "declares" that it has a valid compareTo by stating " implements Comparable ". Coin [] coins = { new Coin(10), new Coin(2), new Coin(20), new Coin(5) }; java.util.Arrays.sort( coins ); System.out.println( coins[0] ); // prints "2 Baht" System.out.println( coins[1] ); // prints "5 Baht"
5
Why is Polymorphism Useful? Reusable Software: like Arrays.sort and Arrays.search Extensible Software: System.out.println( "The coin is: " + coin ); // calls coin.toString( ) System.out.println( "The purse is: " + purse ); // calls purse.toString( ) System.out.println can print our objects.
6
Why is Polymorphism Useful? Suppose we have a List of Products. We want to check if the list contains Ice Cream. List list = new LinkedList ( ); // hey, that's polymorphism! list.add( product1 );// add some products list.add( product2 );... Product p = new Product( 1111,"Ice Cream",10.0 ); list.contains( p );// do we have ice cream? // polymorphism: // list.contains( ) calls p.equals( )
7
Using Polymorphism 1.An object reference can point to any compatible object. 2.The type of the reference defines what methods are available. 3.The actual method is decided at runtime, based on the actual object. List mylist = new ArrayList( ); // ArrayList is a kind of List mylist.add( "stuff" ); // invokes ArrayList.add( ) mylist.size( ); // invokes ArrayList.size( ) mylist = new LinkedList( ); mylist.add( "more stuff" ); // invokes LinkedList.add
8
Polymorphism using Interface > List +add( Object ) : bool +size( ) : int ArrayList +add( Object ) : bool +size( ) : int LinkedList +add( Object ) : bool +size( ) : int
9
Polymorphism using Inheritance Object +equals(Object): bool +hashCode() : int +toString() : String Coin +equals(Object): bool +toString(): String Course +equals(Object): bool +toString(): String
10
Example (1) Object p1 = new Person( ); Person p2 = new Person( ); Person e1 = new Employee( ); Employee e2 = new Employee( ); p1.equals( p2 ) p2.equals( p1 ) p2.equals( e1 ) e1.equals( e2 ) e2.equals( e1 ) Which equals? Person + equals( Object ) Employee + equals( Object ) Object # clone( ) : Object + equals( Object ) + hashCode( ) + toString( )
11
Example (1b) Object p1 = new Person( ); Person p2 = new Person( ); Person e1 = new Employee( ); Employee e2 = new Employee( ); p1.equals( p2 ) p2.equals( p1 ) p2.equals( e1 ) e1.equals( e2 ) e2.equals( e1 ) Which equals? Person + equals( Person ) Employee + equals( Employee ) Object # clone( ) : Object + equals( Object ) + hashCode( ) + toString( )
12
Example (2) Person e1 = new Employee( ); Employee e2 = new Employee( ); e1.equals( e2 ) e2.equals( e1 ) Which equals? What the compiler does:.equals( ) # clone( ) + equals( Object) + hashCode( ) + toString( ) + equals( Person ) + equals( Employee ) + getSalary( ) Object Employee Person Virtual Method Table.equals( )
13
Example (4) Person + equals( Person ) Employee + equals( Person ) + equals( Employee ) + getSalary( ) Person p1 = new Employee( ); Employee p2 = new Employee( ); p1.equals( p2 ) p2.equals( p1 ) Which equals?.equals( )
14
Example (1) Person + equals( Person ) Employee + equals( Employee ) + getSalary( ) Object # clone( ) : Object + equals( Object ) + hashCode( ) + toString( ) Person p1 = new Person( ); Person p2; if ( n < 0 ) p2 = new Employee( ); else p2 = new Person( ); p1.getSalary( ); p2.getSalary( ); Can we do this? Answer: _____
15
Example Person + equals( Person ) Employee + equals( Employee ) + getSalary( ) Object # clone( ) : Object + equals( Object ) + hashCode( ) + toString( ) Person p1 = new Person( ); Person p2; if ( n < 0 ) p2 = new Employee( ); else p2 = new Person( ); p1.getSalary( ); p2.getSalary( ); Can we do this? Answer: _____
16
Limits of Polymorphism ArrayList doesn't know about the Product class. ArrayList.contains( p ) does something like this: public boolean contains( T obj ) { for( int k=0; k<list.size(); k++ ) { if ( obj.equals( list.get(k) ) ) return true; } return false; } The compiler looks for an equals( ) method with signature: public boolean equals( Object ) It doesn't know what "T" is. It treats "T" like an Object.
17
Limits of Polymorphism (2) The compiler says: "call equals(Object) that belongs to the type of thing referred to by obj." 1. If the actual type (class) of obj has an equals(Object) method, then that method is used (polymorphism). 2. If the actual type (class) of obj does not have such a method, then it tries the parent class, then its parent class,... until it finds an equals(Object) method. 3. The Object class has an equals(Object) method, so this method is used if it is not "overridden" by subclass. Object.equals(Object b) is same as a == b
18
Limits of Polymorphism (3) Case 1: Product contains equals(Object) Object clone( ) equals(Object): bool toString( ): String... Product equals(Object): bool getPrice( ): double getDescription(): String toString( ): String Case 1: Product contains equals(Product) Object clone( ) equals(Object): bool toString( ): String... Product equals(Product): bool getPrice( ): double getDescription(): String toString( ): String winner
19
Example Person + equals( Person ) Employee + equals( Employee ) + getSalary( ) Object # clone( ) : Object + equals( Object ) + hashCode( ) + toString( ) Person p1 = new Person( ); Person p2; if ( n < 0 ) p2 = new Employee( ); else p2 = new Person( ); p1.getSalary( ); p2.getSalary( ); Can we do this? Answer: _____
20
Example Person + equals( Person ) Employee + equals( Employee ) + getSalary( ) Object # clone( ) : Object + equals( Object ) + hashCode( ) + toString( ) Person p1 = new Person( ); Person p2; if ( n < 0 ) p2 = new Employee( ); else p2 = new Person( ); p1.getSalary( ); p2.getSalary( ); Can we do this? Answer: _____
21
Declared Type You can only invoke methods that the Java compiler can verify the object has. Therefore: you can invoke methods that are available to the declared type of a reference (a variable). # clone( ) + equals( Object) + hashCode( ) + toString( ) + equals( Person ) + equals( Employee ) + getSalary( ) Object Employee Person Person p1 = new Person( ); Person p2 = new Employee( ); p1.getSalary( ); // NO p2.getSalary( ); // NO
22
Method Signatures Person has 2 equals methods: equals( Object ) equals( Person ) Employee has 3 equals methods: equals( Object ) equals( Person ) equals( Employee ) # clone( ) + equals( Object) + hashCode( ) + toString( ) + equals( Person ) + equals( Employee ) + getSalary( ) Object Employee Person
23
Another Example Collection list = new ArrayList( ); list.add( 0, object ); // NO -- Collection doesn't have Rule: Java selects the best matching method (at runtime) based on method signature using the declared type of the variables.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.