inheritance
play

inheritance overriding vs overloading Nov. 17, 2017 1 All dogs - PowerPoint PPT Presentation

COMP 250 Lecture 30 inheritance overriding vs overloading Nov. 17, 2017 1 All dogs are animals. relationships between All beagles are dogs. classes 2 All dogs are animals. relationships between All beagles are dogs. classes Animals


  1. COMP 250 Lecture 30 inheritance overriding vs overloading Nov. 17, 2017 1

  2. All dogs are animals. relationships between All beagles are dogs. classes 2

  3. All dogs are animals. relationships between All beagles are dogs. classes Animals are born (and have a birthdate). class Dogs bark. definitions Beagles chase rabbits. 3

  4. Inheritance class Animal Date birth Date death Place home void eat() : extends class Dog String serialNumber Person owner void bark() : extends class Beagle hunt () : 4

  5. Inheritance class Dog String serialNumber Person owner void bark() : extends extends extends class Poodle class Beagle class Doberman void show() void hunt () void fight () : : : e.g. Beagle is a subclass of Dog. Dog is a superclass of Beagle. A subclass inherits the fields and methods of its superclass. 5

  6. Constructors are not inherited. class Dog String serialNumber Person owner Dog() void bark() : extends extends extends class Poodle class Beagle class Doberman Poodle() Beagle() Doberman() show() hunt () fight () : : : Each object belongs to a unique class. 6

  7. Constructor chaining class Animal { Place home; Animal( ) { ... } Animal( Place home) { this.home = home; } } class Dog extends Animal { String owner; Dog( ) { } // This constructor automatically creates // fields that are inherited from the superclass } 7

  8. class Animal { Constructor chaining Place home; (a few details…) Animal() { ... } Animal( Place home) { this.home = home; } } class Dog extends Animal { String owner; Dog() { } // This constructor automatically calls super() which creates // fields that are inherited from the superclass Dog(Place home, String owner) { super(home); // Here we need to explicitly write it. this.owner = owner; } : 8 }

  9. Sometimes we have two versions of a method: (method) overloading vs. (method) overriding Today we will see some examples. The reasons why we do this will hopefully become more clear over the next few lectures. 9

  10. Example of overloading LinkedList<E> void add( E e) void add( int index, E e ) E remove( int index) E remove( ) // removes head 10

  11. Overloading • same method name, but different parameter types (i.e. different method “signature” note: “signature” does not include the return type) • within a class, or between a class and its superclass Example on previous slide was within a class 11

  12. Overriding • subclass method overrides a superclass method • same method signatures (i.e. same method name and parameter types) 12

  13. Overriding e.g. bark() class Dog String serialNumber Person owner void bark() : {print “woof”} extends extends extends class Beagle class Doberman class Poodle void hunt () void show() void fight () void bark() void bark() void bark() {print “ aowwwuuu ”} {print “ Arh! Arh! Arh !”} {print “ arw ”} https://www.youtub https://www.youtube.c https://www.youtu be.com/watch?v=_ e.com/watch?v=esje om/watch?v=s5Y- wqK15EtCMo c0JWEXU Gyt57Dw 13

  14. class Object Object class boolean equals( Object ) int hashCode( ) extends (automatic) String toString( ) Object clone( ) class Animal : : extends class Dog : extends class Beagle : 14

  15. class Object boolean equals( Object ) int hashCode( ) String toString( ) Object clone( ) : 15

  16. Object.equals( Object ) Object obj1, obj2 obj1.equals( obj2 ) is equivalent to obj1 == obj2 16

  17. https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html see MATH 240 17

  18. Object.equals( Object ) x.equals(x) should always return true x.equals(y) should return true if and only if y.equals(x) returns true if x.equals(y) and y.equals(z) both return true, Then x.equals(z) should return true x.equals(null) should return false. The above rules should hold for non-null references. 18

  19. class Object boolean equals( Object ) Object.equals( Object ) int hashCode( ) String toString( ) Object clone( ) : extends (automatic) class Animal Animal( ) : boolean equals( Object ) Animal.equals( Object) : This is overriding. : 19

  20. class Object boolean equals( Object ) Object.equals( Object ) int hashCode( ) String toString( ) Object clone( ) : extends (automatic) class Animal Animal( ) : boolean equals( Animal ) Animal.equals( Animal ) : This is overloading. : 20

  21. overloading vs. overriding I will say a bit more about when we use one versus the other over the next few lectures. 21

  22. class Object Object.equals( Object ) boolean equals( Object ) int hashCode( ) String toString( ) Object clone( ) : extends (automatic) class String String( ) String.equals( Object ) boolean equals( Object ) This is overriding. int hashCode( ) 22

  23. https://docs.oracle.com/javase/7/docs/api/java/lang/String.html String.equals( Object ) 23

  24. You should Compare strings using String.equals( Object ) rather than “==“ to avoid nasty surprises. String s1 = "sur"; String s2 = "surprise"; System.out.println(("sur" + "prise") == "surprise"); // true System.out.println("sur" == s1); // true System.out.println(("surprise" == "surprise")); // true System.out.println("surprise" == new String("surprise")); // false System.out.println((s1 + "prise") == "surprise"); // false System.out.println((s1 + "prise") == s2); // false System.out.println((s1 + "prise").equals("surprise")); // true System.out.println((s1 + "prise").equals(s2)); // true System.out.println( s2.equals(s1 + "prise")); // true 24

  25. class Object boolean equals( Object ) Object.equals( Object ) int hashCode( ) String toString( ) Object clone( ) : extends (automatic) class LinkedList LinkedList( ) : LinkedList.equals( Object ) boolean equals( Object ) : This is overriding. : 25

  26. https://docs.oracle.com/javase/7/docs/api/java/util/List.html LinkedList.equals( Object ) List interface: next lecture 26

  27. class Object boolean equals( Object ) int hashCode( ) String toString( ) Object clone( ) : 27

  28. class Object Object.hashCode() boolean equals( Object )  Returns a 32 bit integer int hashCode( ) String toString( ) Object clone( ) : extends (automatic) class String String( ) boolean equals( String ) String.hashCode() int hashCode( ) String toString( ) This is overriding. Object clone( ) 28

  29. SLIDE ADDED Nov. 20 (I will discuss this next lecture too) Java API for Object.hashCode() recommends: If o1.equals(o2) is true then o1.hashCode() == o2.hashCode() should be true. The converse need not hold. It can easily happen that two objects have the same hashCode but the objects are not considered equal. 29

  30. https://docs.oracle.com/javase/7/docs/api/java/lang/String.html String.hashCode() 30

  31. For fun, check out hashcode() method for other classes e.g. LinkedList. 31

  32. class Object boolean equals( Object ) int hashCode( ) String toString( ) Object clone( ) : 32

  33. class Object Object.toString() boolean equals( Object ) int hashCode( )  Returns ? String toString( ) Object clone( ) : extends (automatic) class Animal Animal.toString() This is overriding. Animal( ) boolean equals( Animal ) int hashCode( )  Returns ? String toString( ) 33

  34. class Object Object.toString() boolean equals( Object ) int hashCode( ) String toString( )  Returns classname + “@” + hashCode() Object clone( ) : extends (automatic) class Animal Animal.toString() This is overriding. Animal( ) boolean equals( Animal ) int hashCode( )  Returns …. however you define it String toString( ) : 34

  35. Object.toString() returns classname + “@” + hashCode() In order to explain this, I need to take a detour. I have also added the following slides to lecture 2 (binary numbers). That is really where the following material belongs. 35

  36. As you know from Assignment 1, we can write any positive integer 𝑛 uniquely as a sum of powers of any number called the base ( or radix). 𝑂−1 𝑏 𝑗 (𝑐𝑏𝑡𝑓) 𝑗 𝑛 = 𝑗=0 The coefficients 𝑏 𝑗 are in {0, 1, ….., 𝑐𝑏𝑡𝑓 – 1} We write (𝑏 𝑂−1 𝑏 𝑂−2 𝑏 𝑂−3 … 𝑏 2 𝑏 1 𝑏 0 ) 𝑐𝑏𝑡𝑓 Humans usually use base 10. Computers use base 2. 36

  37. e.g. Hexadecimal (base 16) 𝑂−1 𝑏 𝑗 (16) 𝑗 𝑛 = 𝑗=0 The coefficients 𝑏 𝑗 are in {0, 1, ….., 10, 11, … 15} Instead we use 𝑏 𝑗 in {0, 1, ….., 8, 9, a, b, c, d, e, f} 37

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend