Lecture16

Lecture16 - Late binding Polymorphism via Inheritance...

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

View Full Document Right Arrow Icon
1 Late binding Polymorphism via Inheritance Polymorphism via Interfaces The interface name, speaker, can be used to declare an object reference variable. Speaker guest = new Philospher(); The guest reference can be used to point to any object of any class that implements the Speaker interface The version of speak depends on the type of object that current is referencing guest.speak(); <Interface> Speaker Speak() Philosopher Speak() pontificate() Dog Speak()
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 public interface Speaker { int i= 0; int j=1000; public void speak(); } class Philosopher implements Speaker { int i=10; int s = 16; public void speak() {…} public void pontificate() {…} } Speaker guest = new Philosopher(); guest.speak(); guest.pontificate();//?? guest.i; guest.j; guest.s; //??
Background image of page 2
interface Flyer { int i = 10; int k = 1000; public boolean hasWings();} interface Mammal { static boolean hasLungs = true; public boolean hasLegs();} interface Noisy extends Mammal { public boolean hasBark();} class Dog implements Noisy { public boolean hasLegs() { return true; } public boolean hasBark() { return true; } } class Bird implements Mammal, Flyer { int i = 100; int s = 106; public boolean hasLegs(){ return true;} public boolean hasWings() { return true;}} class InterfaceExample { public static void main( String args[] ) { Bird robin = new Bird(); Flyer crow = new Bird(); Noisy fido = new Dog(); Mammal fifi = new Noisy(); //??? robin.hasLegs();
Background image of page 3

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

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

This note was uploaded on 12/12/2010 for the course COE 318 taught by Professor Ken during the Spring '08 term at Ryerson.

Page1 / 12

Lecture16 - Late binding Polymorphism via Inheritance...

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

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