COMP 250 Lecture 32 polymorphism Nov. 25, 2016 1
Recall example from lecture 30 Dog myDog = new Beagle(); class Dog String serialNumber myDog.bark(); Person owner void bark() ?????? {print “woof”} : extends extends class Beagle class Doberman void hunt () void fight () void bark() void bark() {print “ aowwwuuu ”} {print “ Arh! Arh! Arh !”}
Recall example from lecture 30 Dog myDog = new Beagle(); class Dog String serialNumber myDog.bark(); Person owner void bark() “ aowwwuuu ” {print “woof”} : extends extends class Beagle class Doberman void hunt () void fight () void bark() void bark() {print “ aowwwuuu ”} {print “ Arh! Arh! Arh !”}
Polymorphism “poly” = multiple “morph” = form We will look at “sub - type” polymorphism. More general discussion about this in higher level courses e.g. COMP 302.
Polymorphism Compile time: Suppose a reference variable has a declared type: class C. C var; Runtime: That reference variable can reference any object of class C or that extends class C.
Polymorphism Compile time: Suppose a reference variable has a declared type: abstract class A. A var; Runtime: That reference variable can reference any object whose class extends abstract class A.
Polymorphism Compile time: Suppose a reference variable has a declared type: interface I. I var; Runtime: That reference variable can reference any object whose class implements interface I.
boolean b; Object obj; : if ( b ) obj = new float[23]; else obj = new Dog(); System.out.print( obj ); // calls toString()
How does polymorphism work? How does all this class relationship stuff work in a running program ? (Sketch only.)
Java Class Descriptors A class descriptor is an object that contains all the information about a class that is used in a running program. Beagle Dog class descriptor class descriptor String LinkedList class descriptor class descriptor
Class Descriptors • class name • fields (names, types) • methods (names, parameters, implementation) • reference to superclass • ….
Q: Each object is an instance of a class. So, if class descriptors are objects, then what class(es) are they instances of ? Dog Beagle Beagle object class descriptor object String Dog class descriptor Beagle object Doberman object class descriptor LinkedList Doberman Doberman class descriptor object class descriptor other objects
Q: Each object is an instance of a class. So, if class descriptors are objects, then what class(es) are they instances of ? A: the Class class https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html Dog Beagle Beagle object class descriptor object String Dog class descriptor Beagle object Doberman object class descriptor LinkedList Doberman Doberman class descriptor object class descriptor other objects
class Object boolean equals( Object ) int hashCode( ) extends String toString( ) class Animal (automatic) Object clone( ) : Class getClass() extends extends class Class class Dog : Class getSuperClass() Method[ ] getMethods( ) Field[ ] getFields( ) extends String getName( ) : class Beagle : ‘class descriptor’ class
All objects inherit the Object.getClass() method. This method returns the class descriptor for that object. getClass() Dog returns Dog object Beagle class descriptor object getClass() returns Beagle Beagle String Doberman object object class descriptor class descriptor getClass() returns Doberman Doberman object LinkedList class descriptor class descriptor
All objects inherit the Object.getClass() method. This method returns the class descriptor for that object. getClass() Object returns class descriptor getClass() Dog returns Class Dog object Beagle class descriptor class descriptor object getClass() returns Beagle Beagle String Doberman object getClass() object returns class descriptor class descriptor getClass() returns Doberman Doberman object LinkedList class descriptor class descriptor
class Object boolean equals( Object ) int hashCode( ) extends String toString( ) class Animal (automatic) Object clone( ) : Class getClass() extends extends class Class class Dog : Class getSuperClass() Method[ ] getMethods( ) Field[ ] getFields( ) extends String getName( ) : class Beagle : ‘class descriptor’ class
Object class descriptor Dog object getSuperClass() Beagle returns object Dog Beagle getSuperClass() class descriptor Doberman object returns object getSuperClass() returns Doberman object Beagle Doberman class descriptor class descriptor
Object class descriptor Dog getClass() object getSuperClass() returns Beagle returns object Dog Beagle class descriptor Doberman object object getClass() getSuperClass() returns returns Doberman object Beagle Doberman class descriptor class descriptor
Dog myDog = new Beagle(); Object System.out.println( myDog ); class descriptor getSuperClass() returns Dog Beagle class descriptor object getClass() getSuperClass() returns returns Beagle class descriptor
Object class descriptor Dog class descriptor What else do we need to run a program? Beagle e.g. Consider running a Beagle class descriptor Test class which has a object main() method. Test class descriptor other objects
Object Call Stack class descriptor goForWalk() Dog class descriptor anotherMethod() someMethod() Beagle Beagle class descriptor object Test.main() Test class descriptor other objects
Object Call Stack class descriptor Dog myDog = new Beagle(); Dog // in main() method class descriptor Beagle() Beagle class descriptor Test.main() Dog myDog Test class descriptor other objects
Object Call Stack class descriptor Dog myDog = new Beagle(); Dog class descriptor // after instruction is done…. Beagle Beagle class descriptor Test.main() object Dog myDog Test class descriptor other objects
Object Call Stack class descriptor Dog myDog = new Beagle(); Dog myDog.bark() class descriptor bark() Beagle this Beagle class descriptor Test.main() object Dog myDog TestProgram class descriptor Look for the bark() method other in the class descriptor objects
Class Call Stack Objects Descriptors Methods are here Local variables and Object Object instance parameters of fields are here methods are here class descriptor Dog Beagle object bark() class descriptor Beagle Test.main() class descriptor other objects TestProgram class descriptor
Recommend
More recommend