15inheritance PDF
15inheritance PDF
Inheritance
Inheritance
l
Conceptual
l
Is-A relationship compared to contains-a
l
Terminology
l
Overloading compared to Overriding
l
super
l
isInstanceOf and getClass()
Shape Animal
Person
●
Student bob =
●
new Student("Bob Goodstudent","123-45-6789",2004,4.0 );
●
System.out.println( "Bob's info: " + bob.toString( ) );
Since bob is Student,
Student toString used
CMSC 131 - Lecture Set #15 10
Overriding vs. Overloading
Overriding: a derived class defines a method with same name, parameters as base
class
Overloading: two or more methods have the same name, but different parameters
Example
●
public class Person {
●
public void setName( String n ) { name = n; }
Base class setName( )
●
…
●
}
●
public class Faculty extends Person {
●
public void setName( String n ) {
●
super.setName( “The Evil Professor ” + n ); Overriding
●
}
●
public void setName( String first, String last ) {
Overloading
●
super.setName( first + “ ” + last );
●
}
●
}
●
public void someMethod2( ) {
●
setName( “Mr. Foobar” ); // OK
●
}
Why?
Although Student inherits from Person …
… they are different classes
CMSC 131 - Lecture Set #15 18
Public, Protected,
Package(default) and Private
Select which level of visibility
Access Levels
Access Level/Group Class Package SubClass World
public Y Y Y Y
protected(a Y Y Y N
void)
package (default) Y Y N N
private Y N N N
CMSC 131 - Lecture Set #15 19
Shadowing
Can we override instance variables just like methods?
Yes, but be careful!
Overriding instance variable is called shadowing
Shadowing hides instance variables of base class (can still access them
using super.varName in subclass, but not in “outside world”)
●
public class Person {
●
String name;
●
…
●
}
●
public class Administrator extends Person {
●
String name;// name refers to Administrator’s name
●
}
Confusing! Better to pick a new variable name
CMSC 131 - Lecture Set #15 20
Example of
Overloading/Overriding
public class Base {
public void m (int x) { … }
}
public class Derived extends Base {
public void m (int x) { … } Overriding: with increased visibility
public int m (int x) { … }
public void m (double d) { … } Error! duplicate method declaration
}
Overloading
// The following appears in the same package as above
Base b = new Base( );
Base d = new Derived( );
Derived e = new Derived( );
b.m (5);
d.m (6);
d.m (7.0); calls
e.m (8.0); Base:m(int)
calls Derived:m(int)
Error! Since d is declared Base, the compiler looks for Base:m(doub
Doesn’t exist! So this does not make it past the compiler, even
though Derived:m(double) is defined!
CMSC 131 - Lecture Set #15 21
calls Derived:m(double)
Object
Recall: inheritance induces “is-a” hierarchy on classes
Undergrad “is-a” Student
Student “is-a” Person
etc.
Person “is-a” ….?
Person “is-a”(n) Object
Student “is-a”(n) Object
Object
Person
– if ( bob.getClass() == ted.getClass() )
– // false (ted is really a Student)
instanceof
Java boolean operator (not a method)
Returns true if given object “is-a”(n) object of given (class) type
E.g.
– Student carol = new Student ( … );
– if (carol instanceof Person) // true, because carol “is-a” Person
Classes can implement multiple interfaces, but inherit (directly) from only one class
Main Uses of Interfaces
API for classes
Polymorphism
“Faking multiple inheritance”
Specifying sets of symbolic constants
Person
StudentAthlete
Example
public class Person { … }