The Java class hierarchy
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Programming Concepts using Java
Week 3
Multiple inheritance
C1 C2
C3 extends C1,C2
Can a subclass extend multiple parent classes?
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 2/7
Multiple inheritance
C1 C2
public int f(); public int f();
C3 extends C1,C2
Can a subclass extend multiple parent classes?
If f() is not overridden, which f() do we use in C3?
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 2/7
Multiple inheritance
C1 C2
public int f(); public int f();
C3 extends C1,C2
Can a subclass extend multiple parent classes?
If f() is not overridden, which f() do we use in C3?
Java does not allow multiple inheritance
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 2/7
Multiple inheritance
C1 C2
public int f(); public int f();
C3 extends C1,C2
Can a subclass extend multiple parent classes?
If f() is not overridden, which f() do we use in C3?
Java does not allow multiple inheritance
C++ allows this if C1 and C2 have no conflict
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 2/7
Java class hierarchy
No multiple inheritance — tree-like
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 3/7
Java class hierarchy
No multiple inheritance — tree-like
In fact, there is a universal superclass Object
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 3/7
Java class hierarchy
No multiple inheritance — tree-like
In fact, there is a universal superclass Object
Useful methods defined in Object
public boolean equals(Object o) // defaults to pointer equality
public String toString() // converts the values of the
// instance variables to String
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 3/7
Java class hierarchy
No multiple inheritance — tree-like
In fact, there is a universal superclass Object
Useful methods defined in Object
public boolean equals(Object o) // defaults to pointer equality
public String toString() // converts the values of the
// instance variables to String
For Java objects x and y, x == y invokes x.equals(y)
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 3/7
Java class hierarchy
No multiple inheritance — tree-like
In fact, there is a universal superclass Object
Useful methods defined in Object
public boolean equals(Object o) // defaults to pointer equality
public String toString() // converts the values of the
// instance variables to String
For Java objects x and y, x == y invokes x.equals(y)
To print o, use System.out.println(o+"");
Implicitly invokes o.toString()
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 3/7
Java class hierarchy
Can exploit the tree structure to write generic functions
Example: search for an element in an array
public int find (Object[] objarr, Object o){
int i;
for (i = 0; i < objarr.length(); i++){
if (objarr[i] == o) {return i};
}
return (-1);
}
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 4/7
Java class hierarchy
Can exploit the tree structure to write generic functions
Example: search for an element in an array
public int find (Object[] objarr, Object o){
int i;
for (i = 0; i < objarr.length(); i++){
if (objarr[i] == o) {return i};
}
return (-1);
}
Recall that == is pointer equality, by default
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 4/7
Java class hierarchy
Can exploit the tree structure to write generic functions
Example: search for an element in an array
public int find (Object[] objarr, Object o){
int i;
for (i = 0; i < objarr.length(); i++){
if (objarr[i] == o) {return i};
}
return (-1);
}
Recall that == is pointer equality, by default
If a class overrides equals(), dynamic dispatch will use the redefined function
instead of Object.equals() for objarr[i] == o
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 4/7
Overriding functions
For instance, a class Date with instance
variables day, month and year
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 5/7
Overriding functions
For instance, a class Date with instance
variables day, month and year
May wish to override equals() to
compare the object state, as follows
public boolean equals(Date d){
return ((this.day == d.day) &&
(this.month == d.month) &&
(this.year == d.year));
}
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 5/7
Overriding functions
For instance, a class Date with instance
variables day, month and year
May wish to override equals() to
compare the object state, as follows
public boolean equals(Date d){
return ((this.day == d.day) &&
(this.month == d.month) &&
(this.year == d.year));
}
Unfortunately,
boolean equals(Date d)
does not override
boolean equals(Object o)!
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 5/7
Overriding functions
For instance, a class Date with instance Should write, instead
variables day, month and year public boolean equals(Object d){
if (d instanceof Date){
May wish to override equals() to
Date myd = (Date) d;
compare the object state, as follows return ((this.day == myd.day) &&
public boolean equals(Date d){ (this.month == myd.month)
return ((this.day == d.day) && (this.year == myd.year));
(this.month == d.month) && }
(this.year == d.year)); return(false);
} }
Note the run-time type check and the
Unfortunately,
cast
boolean equals(Date d)
does not override
boolean equals(Object o)!
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 5/7
Overriding functions
Overriding looks for “closest” match
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 6/7
Overriding functions
Overriding looks for “closest” match
Suppose we have public boolean equals(Employee e) but no equals() in
Manager
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 6/7
Overriding functions
Overriding looks for “closest” match
Suppose we have public boolean equals(Employee e) but no equals() in
Manager
Consider
Manager m1 = new Manager(...);
Manager m2 = new Manager(...);
...
if (m1.equals(m2)){ ... }
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 6/7
Overriding functions
Overriding looks for “closest” match
Suppose we have public boolean equals(Employee e) but no equals() in
Manager
Consider
Manager m1 = new Manager(...);
Manager m2 = new Manager(...);
...
if (m1.equals(m2)){ ... }
public boolean equals(Manager m) is compatible with both
boolean equals(Employee e) and boolean equals(Object o)
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 6/7
Overriding functions
Overriding looks for “closest” match
Suppose we have public boolean equals(Employee e) but no equals() in
Manager
Consider
Manager m1 = new Manager(...);
Manager m2 = new Manager(...);
...
if (m1.equals(m2)){ ... }
public boolean equals(Manager m) is compatible with both
boolean equals(Employee e) and boolean equals(Object o)
Use boolean equals(Employee e)
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 6/7
Summary
Java does not allow multiple inheritance
A subclass can extend only one parent class
The Java class hierarchy forms a tree
The root of the hierarchy is a built-in class called Object
Object defines default functions like equals() and toString()
These are implicitly inherited by any class that we write
When we override functions, we should be careful to check the signature
Madhavan Mukund The Java class hierarchy Programming Concepts using Java 7/7