Java Inheritance
Java Inheritance
Concept
Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you can reuse the existing classs methods and fields, and you can also add new methods and fields to adapt the new classes to new situations
Superclass public class Person{ private String name; public Person ( ) { name = no_name_yet; } public Person ( String initialName ) { this.name = initialName; } public String getName ( ) { return name; }
Subclass public class Student extends Person { private int studentNumber; public Student ( ) { super( ); studentNumber = 0; } public Student (String initialName, int initialStudentNumber) { super(initialName); studentNumber = initialStudentNumber; } public int getStudentNumber ( ) { return studentNumber; } public void setStudentNumber (int newStudentNumber ) { studentNumber = newStudentNumber; }
Classes hierarchy
Every class is an extended (inherited) class, whether or not its declared to be. If a class does not declared to explicitly extend any other class, then it implicitly extends the Object class Class hierarchy of previous example
Object Person
Student
What are the fields for a Student object in the previous example ?
The invoked superclasss constructor is executed using the same three-phase constructor. This process is executed recursively until the Object class is reached
xOri
0 0 0 0 1 1 1 1
yOri
0 0 0 0 0 0 2 2
whichOri
0 0 0 0 0 1 1 2
Overriding: replacing the superclasss implementation of a method with your own design.
both the parameter lists and the return types must be exactly the same
if an overriding method is invoked on an object of the subclass, then its the subclasss version of this method that gets implemented an overriding method can have different access specifier from its superclasss version, but only wider accessibility is allowed the overriding methods throws clause can have fewer types listed than the method in the superclass, or more specific types
package P1;
public class Base { private void pri( ) { System.out.println(Base.pri()); } void pac( ) { System.out.println(Base.pac()); } protected void pro( ) { System.out.println(Base.pro()); } public void pub( ) { System.out.println(Base.pub()); }
public final void show( ) { pri(); pac(); pro(); pub(); }
package P2; import P1.Base; public class Concrete1 extends Base { public void pri( ) { System.out.println(Concrete1.pri()); public void pac( ) { System.out.println(Concrete1.pac()); public void pro( ) { System.out.println(Concrete1.pro()); public void pub( ) { System.out.println(Concrete1.pub()); }
} } } }
Output?
public class Concrete2 extends Concrete1 { public void pri( ) { System.out.println(Concrete2.pri()); public void pac( ) { System.out.println(Concrete2.pac()); public void pro( ) { System.out.println(Concrete2.pro()); public void pub( ) { System.out.println(Concrete2.pub()); }
} } } }
} } } }
Output?
Base.pri() Concrete3.pac() Concrete3.pro() Concrete3.pub()
Hiding fields
Fields cannot be overridden, they can only be hidden If a field is declared in the subclass and it has the same name as one in the superclass, then the field belongs to the superclass cannot be accessed directly by its name any more
Polymorphism
An object of a given class can have multiple forms: either as its declared class type, or as any subclass of it
an object of an extended class can be used wherever the original class is used
Question: given the fact that an objects actual class type may be different from its declared type, then when a method accesses an objects member which gets redefined in a subclass, then which member the method refers to (subclasss or superclasss)?
when you invoke a method through an object reference, the actual class of the object decides which implementation is used when you access a field, the declared type of the reference decides which implementation is used
protected members
To allow subclass methods to access a superclass field, define it protected. But be cautious! Making methods protected makes more sense, if the subclasses can be trusted to use the method correctly, but other classes cannot.
public void printHireDay (Employee p) { System.out.println(eHireDay: + (p.hireDay).toString()); } // wrong! The class is Manager, but the object reference type is Employee // which is a supertype of Manager . . .
5.
6.
Subclasses
Inheritance of a Class class clsName2 extends clsName1 { // Body of class }
Declaration of Variable of a Class
class X12 extends X1 { } class X21 extends X2 { } class X22 extends X2 { } class InheritanceHierarchy { public static void main(String args[]) { X x; System.out.println("Instantiating X"); x = new X(); System.out.println("Instantiating X1"); x = new X1(); System.out.println("Instantiating X11"); x = new X11(); System.out.println("Instantiating X12"); x = new X12(); System.out.println("Instantiating X2"); x = new X2(); System.out.println("Instantiating X21"); x = new X21(); System.out.println("Instantiating X22"); x = new X22(); } }
class X1 extends X { }
Inherit
Subclasses
Reference to a Subclass Variable
Inherited Variables
class X extends W { StringBuffer sb; } class Y extends X { String s; Inherit } class Z extends Y { Integer i; }
Z z = new Z(); z.f = 4.567f; z.sb = new StringBuffer("abcde"); z.s = "Teach Yourself Java"; z.i = new Integer(41); System.out.println("z.f = " + z.f); System.out.println("z.sb = " + z.sb); System.out.println("z.s = " + z.s); System.out.println("z.i = " + z.i); } } Result : z.f = 4.567
z.sb = abcde
z.s = Teach Yourself Java
class E { int x; }
Subclasses
class F extends E { String x; } class Ef { public static void main(String args[]) { F f = new F(); f.x = "This is a string"; System.out.println("f.x = " + f.x); E e = new E(); e.x = 45; System.out.println("e.x = " + e.x); } }
Result : f.x = This is a string e.x = 45
Subclasses
class M100 { int i = 100; } class M200 extends M100 { int i = 200; void display() { System.out.println("i = " + i); System.out.println("super.i = " + super.i); } } class SuperKeyword { public static void main(String args[]) { M200 m200 = new M200(); m200.display(); } }
Method Overriding
Dynamic Method Dispatch Mechanism
class C1 extends B1 { void hello() { System.out.println("Hello from C1"); } } class MethodOverriding1 { public static void main(String args[]) { C1 obj = new C1(); obj.hello(); } }
B1
Method Overriding
class B2 extends A2 { void hello() { System.out.println("Hello from B2"); } } class C2 extends B2 { void hello() { System.out.println("Hello from C2"); } } class MethodOverriding2 { public static void main(String args[]) { A2 obj = new C2(); obj.hello(); } Object of C2 }
Overriden by hello of B2
super.mthName(args)
class I1 { void hello(String s) { System.out.println("I1: " + s); } } class J1 extends I1 { void hello(String s) { super.hello(s); System.out.println("J1: " + s); } }
class K1 extends J1 { void hello(String s) { super.hello(s); System.out.println("K1: " + s); }
System.out.println("Instantiating I1"); I1 obj = new I1(); obj.hello("Good morning"); System.out.println("Instantiating J1"); obj = new J1(); obj.hello("Good afternoon"); System.out.println("Instantiating K1"); obj = new K1(); obj.hello("Good evening"); } } Result : Instantiating I1 I1: Good morning Instantiating J1 I1: Good afternoon J1: Good afternoon Instantiating K1 I1: Good evening J1: Good evening K1: Good evening
class U1 extends T1 { int u1; U1() { System.out.println("U1 Constructor"); u1 = 3; } } class InheritanceAndConstructors1 { public static void main(String args[]) { U1 u1 = new U1(); System.out.println("u1.s1 = " + u1.s1); System.out.println("u1.t1 = " + u1.t1); System.out.println("u1.u1 = " + u1.u1); } }
1) Constructor of Super Class 2) Initializing Field 3) Constructor Body class S2 { int s2; S2(int s2) { this.s2 = s2; } } class T2 extends S2 { int t2; T2(int s2, int t2) { super(s2); this.t2 = t2; } }
class InheritanceAndConstructors2 { public static void main(String args[]) { U2 u2 = new U2(1, 2, 3); System.out.println("u2.s2 = " + u2.s2); System.out.println("u2.t2 = " + u2.t2); System.out.println("u2.u2 = " + u2.u2); } }
Result : u2.s2 = 1 u2.t2 = 2 u2.u2 = 3
Class Modifier
Qualifier of Class
abstract : Can not be instantiated
final : Cannot extend public : Can be referenced from all classes.
Abstract Class
A class which has abstract method(s). It has not detailed implementation, and can be implemented in a subclass
abstract class Shape { void display() { } } class Circle extends Shape { void display() { System.out.println("Circle"); } }
class Rectangle extends Shape { void display() { System.out.println("Rectangle"); } } class Triangle extends Shape { void display() { System.out.println("Triangle"); } } class AbstractClassDemo { public static void main(String args[]) { Shape s = new Circle(); s.display(); s = new Rectangle(); s.display(); s = new Triangle(); s.display(); } } Result : Circle Rectangle Triangle
Variable Modifier
class L { static final int x = 5; } class FinalVariable { public static void main(String args[]) { System.out.println(L.x); } } Result : 5
Variable Modifier
final : a constant
private : can be accessed only in the same class protected : can be accessed from subclass and the same package public : Can be referenced from all classes. static : not instance variable transient : variable which is not a part of consistent state of class volatile :
protect of optimization such as constant propagation.
Constructor Modifier
Qualifier of Constructor private : can be accessed only in same class
class PrivateConstructor { public static void main(String args[]) { // public Constructor Invocation Person p1 = new Person("John", 30); System.out.println(p1.name); System.out.println(p1.age); // private constructor invoction // Person p2 = new Person(); }
Method Modifier
Method Modifier
abstract : method without implementation final: cannot override native : Machine code usage such as C or C++ private : can be accessed only in the same class protected : can be accessed from subclass and the same package public : Can be referenced from all classes. static : not instance method synchronized : when executed, lock among threads
abstract class JetPlane { abstract int numEngines(); } class DC8 extends JetPlane { int numEngines() { return 4; } }
Result : 4 3
equals() method
boolean equals(Object obj)
getName() method
String getName()
getClass() method
Class getClass()
getSuperClass() method
Class getSuperClass()
toString() method
String toString()
forName() method
Static Class forName (String cIsName) throws ClassNotFoundException