COMP 250 Lecture 30 inheritance overriding vs overloading Nov. 17, 2017 1
All dogs are animals. All beagles are dogs. relationships between classes 2
All dogs are animals. All beagles are dogs. relationships between classes Animals are born (and have a birthdate). Dogs bark. class definitions Beagles chase rabbits. 3
Inheritance class Animal Date birth Date death Place home void eat() extends class Dog String serialnumber Person owner void bark() class Beagle hunt () extends 4
Inheritance extends class Poodle void show() class Dog String serialnumber Person owner void bark() extends class Beagle void hunt () extends class Doberman 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
Constructors are not inherited. extends class Poodle Poodle() show() class Dog String serialnumber Person owner Dog() void bark() class Beagle Beagle() hunt () extends extends class Doberman Doberman() fight () Each object belongs to a unique class. 6
class Animal { Place home; Constructor chaining 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
class Animal { Place home; Animal() {... } Constructor chaining (a few details ) } 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
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
Example of overloading LinkedList<E> void add( E e) void add( int index, E e ) E remove( int index) E remove( ) // removes head 10
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
Overriding subclass method overrides a superclass method same method signatures (i.e. same method name and parameter types) 12
Overriding e.g. bark() extends class Poodle void show() void bark() class Dog String serialnumber Person owner void bark() {print woof } extends class Beagle void hunt () void bark() extends class Doberman void fight () void bark() {print arw } https//www.youtu be.com/watch?v=_ wqk15etcmo {print aowwwuuu } https//www.youtub e.com/watch?v=esje c0jwexu {print Arh! Arh! Arh! } https//www.youtube.c om/watch?v=s5y- Gyt57Dw 13
class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object class extends (automatic) class Animal extends class Dog extends class Beagle 14
class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) 15
Object.equals( Object ) Object obj1, obj2 obj1.equals( obj2 ) is equivalent to obj1 == obj2 16
https//docs.oracle.com/javase/7/docs/api/java/lang/object.html see MATH 240 17
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
class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.equals( Object ) extends (automatic) class Animal Animal( ) boolean equals( Object ) Animal.equals( Object) This is overriding. 19
class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.equals( Object ) extends (automatic) class Animal Animal( ) boolean equals( Animal ) Animal.equals( Animal ) This is overloading. 20
overloading vs. overriding I will say a bit more about when we use one versus the other over the next few lectures. 21
class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.equals( Object ) extends (automatic) class String String( ) String.equals( Object ) boolean equals( Object ) int hashcode( ) This is overriding. 22
https//docs.oracle.com/javase/7/docs/api/java/lang/string.html String.equals( Object ) 23
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"); System.out.println("sur" == s1); System.out.println(("surprise" == "surprise")); System.out.println("surprise" == new String("surprise")); System.out.println((s1 + "prise") == "surprise"); System.out.println((s1 + "prise") == s2); System.out.println((s1 + "prise").equals("surprise")); System.out.println((s1 + "prise").equals(s2)); System.out.println( s2.equals(s1 + "prise")); // true // true // true // false // false // false // true // true // true 24
class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.equals( Object ) extends (automatic) class LinkedList LinkedList( ) boolean equals( Object ) LinkedList.equals( Object ) This is overriding. 25
https//docs.oracle.com/javase/7/docs/api/java/util/list.html LinkedList.equals( Object ) List interface next lecture 26
class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) 27
class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.hashCode() Returns a 32 bit integer extends (automatic) class String String( ) boolean equals( String ) int hashcode( ) String tostring( ) Object clone( ) String.hashCode() This is overriding. 28
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
https//docs.oracle.com/javase/7/docs/api/java/lang/string.html String.hashCode() 30
For fun, check out hashcode() method for other classes e.g. LinkedList. 31
class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) 32
class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.toString() Returns? extends (automatic) class Animal Animal( ) boolean equals( Animal ) int hashcode( ) String tostring( ) Animal.toString() This is overriding. Returns? 33
class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.toString() Returns classname + @ + hashcode() extends (automatic) class Animal Animal( ) boolean equals( Animal ) int hashcode( ) String tostring( ) Animal.toString() This is overriding. Returns. however you define it 34
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
As you know from Assignment 1, we can write any positive integer m uniquely as a sum of powers of any number called the base (or radix). m = N 1 i=0 a i (base) i The coefficients a i are in {0, 1,.., base 1} We write (a N 1 a N 2 a N 3 a 2 a 1 a 0 ) base Humans usually use base 10. Computers use base 2. 36
e.g. Hexadecimal (base 16) m = N 1 i=0 a i (16) i The coefficients a i are in {0, 1,.., 10, 11, 15} Instead we use a i in {0, 1,.., 8, 9, a, b, c, d, e, f} 37
38 Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 8 9 a b c d e f Hexadecimal
Common use of hexadecimal representing long bit strings Example 0010 1111 1010 0011 2 f a 3 Example 2 10 1100 We write 2c (10 1100), not b0 (1011 00). 39
Object.toString() Returns classname + @ + hashcode() In Eclipse, we get the package name also. 32 bit integer (8 hexadecimal digits) Address of object 40
Object.toString() System.out.println( new Object() ); What does this print? 41
Object.toString() System.out.println( new Object() ); What does this print? java.lang.object@7852e922 32 bit integer represented in hexadecimal. You ll get a different number if you run it again. 42
Object.toString() Object o = new Object(); System.out.println( o ); What does this print? java.lang.object@7852e922 package + class name 32 bit integer represented in hexadecimal. You ll get a different number if you run it again. 43
class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.toString() Returns classname + @ + hashcode() class String String( ) boolean equals( Object ) int hashcode( ) String tostring( ) String.toString() This is overriding. Returns itself! 44