polymorphism
play

polymorphism Nov. 25, 2016 1 Recall example from lecture 30 Dog - PowerPoint PPT Presentation

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


  1. COMP 250 Lecture 32 polymorphism Nov. 25, 2016 1

  2. 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 !”}

  3. 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 !”}

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. boolean b; Object obj; : if ( b ) obj = new float[23]; else obj = new Dog(); System.out.print( obj ); // calls toString()

  9. How does polymorphism work? How does all this class relationship stuff work in a running program ? (Sketch only.)

  10. 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

  11. Class Descriptors • class name • fields (names, types) • methods (names, parameters, implementation) • reference to superclass • ….

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. Object Call Stack class descriptor goForWalk() Dog class descriptor anotherMethod() someMethod() Beagle Beagle class descriptor object Test.main() Test class descriptor other objects

  23. 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

  24. 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

  25. 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

  26. 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