Sunteți pe pagina 1din 36

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

Subclass and superclass


Subclass and superclass have a IsA relationship: an object of a subclass IsA(n) object of its superclass

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; }

public void setName ( String newName ) { name = newName; }

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

Fields/Methods in Extended Classes


An object of an extended class contains two sets of variables and methods
1. fields/methods which are defined locally in the extended class

2. fields/methods which are inherited from the superclass

What are the fields for a Student object in the previous example ?

Constructors in extended classes


A constructor of the extended class can invoke one of the superclasss constructors by using the super method. If no superclass constructor is invoked explicitly, then the superclasss no-arg constructor super( ) is invoked automatically as the first statement of the extended classs constructor.

Constructors are not methods and are NOT inherited.

Three phases of an objects construction


When an object is created, memory is allocated for all its fields, which are initially set to be their default values. It is then followed by a three-phase construction:
invoke a superclasss constructor initialize the fields by using their initializers and initialization blocks execute the body of the constructor

The invoked superclasss constructor is executed using the same three-phase constructor. This process is executed recursively until the Object class is reached

To Illustrate the Construction Order. . .


class X { protected int xOri = 1; protected int whichOri;
public X() { whichOri = xOri; } } } class Y extends X { protected int yOri = 2;

public Y() { whichOri = yOri; }

Y objectY = new Y();


Step what happens
0 1 2 3 4 5 6 7 fields set to default values Y constructor invoked X constructor invoked Object constructor invoked X field initialization X constructor executed Y field initialization Y constructor executed

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

Overloading and Overriding Methods


Overloading: providing more than one method with the same name but different parameter list
overloading an inherited method means simply adding new method with the same name and different signature

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

Accessibility and Overriding


a method can be overridden only if its accessible in the subclass
private methods in the superclass
cannot be overridden if a subclass contains a method which has the same signature as one in its superclass, these methods are totally unrelated

package methods in the superclass


can be overridden if the subclass is in the same package as the superclass

protected, public methods


always will be

Not as that simple as it seems!

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()); }

} } } }

Concrete1 c1 = new Concrete1(); c1.show( );

Output?

Base.pri() Base.pac() Concrete1.pro() Concrete1.pub()

Sample classes (cont.)


package P1; import P2.Concrete1;

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()); }

} } } }

Concrete2 c2 = new Concrete2(); c2.show( );


Output? Base.pri() Concrete2.pac() Concrete2.pro() Concrete2.pub()

Sample classes (cont.)


package P3; import P1.Concrete2; public class Concrete3 extends Concrete2 { public void pri( ) { System.out.println(Concrete3.pri()); public void pac( ) { System.out.println(Concrete3.pac()); public void pro( ) { System.out.println(Concrete3.pro()); public void pub( ) { System.out.println(Concrete3.pub()); }

} } } }

Concrete3 c3 = new Concrete3(); c3.show( );

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

class SuperShow { public String str = SuperStr;

Output? Extend.show: ExtendStr Extend.show: ExtendStr sup.str = SuperStr ext.str = ExtendStr

public void show( ) { System.out.println(Super.show: + str); }

class ExtendShow extends SuperShow { public String str = ExtendedStr;

public void show( ) { System.out.println(Extend.show: + str); }


public static void main (String[] args) { ExtendShow ext = new ExtendShow( ); SuperShow sup = ext; sup.show( ); //1 ext.show( ); //2 methods invoked through object reference System.out.println(sup.str = + sup.str); //3 System.out.println(ext.str = + ext.str); //4 field access }

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.

What protected really means


Precisely, a protected member is accessible Within the class itself within code in the same package it can also be accessed from a class through object references that are of at least the same type as the class that is , references of the classs type or one of its subtypes

What protected really means


public class Employee { protected Date hireDay; . . . } public class Manager extends Employee { . . . public void printHireDay (Manager p) { System.out.println(mHireDay: + (p.hireDay).toString()); } // ok! The class is Manager, and the object reference type is also Manager

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 . . .

Object: the ultimate superclass


The object class is the ultimate ancestor: every class in Java extends Object without mention Utility methods of Object class
equals: returns whether two object references have the same value hashCode: return a hash code for the object, which is derived from the objects memory address. Equal objects should return identical hash codes clone: returns a clone of the object getClass: return the run expression of the objects class, which is a Class object finalize: finalize the object during garbage collection toString: return a string representation of the object

Design hints for inheritance


1. 2. 3. 4. Place common operations and fields in the superclass Try not to use protected fields Use inheritance to model a IsA relationship Dont use inheritance unless all inherited methods make sense Dont change the expected behavior when you override a method Use polymorphism, not type information
if (x is of type1) action1(x); else if (x is of type2) action2(x); Do action1 and action2 represent a common concept? If it is, make the concept a method of a common superclass or interface of both types, and then you can simply call x.action().

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(); } }

clsName varName; class X { }


Instantiating

class X1 extends X { }
Inherit

class X2 extends X { } class X11 extends X1 { }

Subclasses
Reference to a Subclass Variable

super.varName class W { float f; }

class Wxyz { public static void main(String args[])

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 P { static int x; }

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

class Q extends P { static String x; }


class Pq { public static void main(String args[]) { P p = new P(); p.x = 55; System.out.println("p.x = " + p.x); Q q = new Q(); q.x = "This is a string"; System.out.println("q.x = " + q.x); } }
Result : p.x = 55 q.x = This is a string

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(); } }

Result : i = 200 super.i = 100

Method Overriding
Dynamic Method Dispatch Mechanism

Overloading & Overriding


Overriding : When the method with same si-gnature is defined in Subclass Overloading : Only the same name, but different in no. or type of parameter

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(); } }

class A1 { void hello() { System.out.println("Hello from A1"); } Overrided by hello of }

B1

class B1 extends A1 { void hello() { System.out.println("Hello from B1"); } }

Result : Hello from C1

class A2 { void hello() { System.out.println("Hello from A2"); } }

Method Overriding

Result : Hello from C2

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

Inheritance and Methods


Reference of Methods in Superclass

class SuperForMethods1 { public static void main(String args[]) {

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

Inheritance and Constructors


Invoking of Super-Constructor super (args) Invoking Constructor of Same Class this (args)
class S1 { int s1; S1() { System.out.println("S1 Constructor"); s1 = 1; } } class T1 extends S1 { int t1; T1() { System.out.println("T1 Constructor"); t1 = 2; }

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); } }

Result : S1 Constructor T1 Constructor U1 Constructor u1.s1 = 1 u1.t1 = 2 u1.u1 = 3

What does this mean?

Invocation Order of Constructor

Inheritance and U2 extends T2 { Constructors class int u2;


U2(int s2, int t2, int u2) { super(s2, t2); this.u2 = u2; } }

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(); }

protected : can be accessed from subclass and same package


public : Can be referenced from all classes. class Person { String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } private Person() { } }

What is the result of compilation when we removed this comment?

Current Result : John 30

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; } }

class DC10 extends JetPlane { int numEngines() { return 3; } }


class JetPlanes { public static void main(String args[]) { System.out.println(new DC8().numEngines()); System.out.println(new DC10().numEngines()); } }

Result : 4 3

The Object & Class Classes


Object Class : Top class in Java Class Class

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

The Object & Class Classes


class ClassDemo { public static void main(String args[]) { Integer obj = new Integer(8); Class cls = obj.getClass(); System.out.println(cls); } }
Result : Class java.lang.Integer

S-ar putea să vă placă și