Chapter-8-part2

Chapter-8-part2 - Chapter 8 Part 2 POLYMORPHISM...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University Chapter 8 Part 2 POLYMORPHISM Polymorphism via Inheritance (contd.) Interface Polymorphism via Interface
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University Creating a list where we can add any Object ArrayList class Few methods of ArrayList class: boolean remove(Object elem) boolean add(Object elem) Polymorphism via Inheritance (contd.) ArrayList<Object> list = new ArrayList<Object>(); list.add( new Car() ); list.add( new Bike() ); list.add( new String() ); list.add( new Integer() ); <<abstract>> Vehicle Car Bike Object
Background image of page 2
3 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University Object +equals(o: Object): boolean +toString(): String +hashCode():int +getClass(): Class Car c = new Car(); Bike b = new Bike(); if( c.equals( b ) ) System.out.println(true); else System.out.println(false); Answer: false Car c = new Car(); System.out.println( c.getClass() ); Answer: class Car Car c = new Car(); System.out.println(c); Answer??
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University Vehicle, Car, Bike public abstract class Vehicle { } public abstract boolean hasEngine (); public class Car extends Vehicle { } public boolean hasEngine () { return true ; } public class Bike extends Vehicle { } public boolean hasEngine () { return false ; }
Background image of page 4
5 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University ArrayList<Object> list = new ArrayList<Object>(); list.add( new Car() ); Car c = list.get( 0 ); Examples Object o = new Car(); boolean b = o.hasEngine(); boolean b = ((Car)o).hasEngine(); ArrayList<Car> list = new ArrayList<Car>(); list.add( new Car() ); Car c = list.get( 0 ); Car c = (Car) list.get( 0 );
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University More Examples public void foo() { Car c1 = new Car(); Car c2 = getObject( c1 ); } public Object getObject( Object o ) { return o; } Object c2 = getObject( c1 );
Background image of page 6
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University More Examples public void foo() { Car c1 = new Car(); Object c2 = getObject( c1 ); String s = c2.toString(); boolean b = c2.hasEngine(); } public Object getObject( Object o ) { return o; } The compiler decides whether you can call a  method based on the reference type, not the  actual object type. boolean b = ((Car)c2).hasEngine();
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 33

Chapter-8-part2 - Chapter 8 Part 2 POLYMORPHISM...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online