TM2 Inheritance PDF
TM2 Inheritance PDF
Inheritance
Multilevel Hierarchy
Each of these classes will add only those attributes and behaviors that are unique to it
When an object ‘has-a’ another object, then you have got an aggregation between them
• When an object contains the other object, if the contained object cannot exist without the
existence of container object, then it is called composition
• Example: A class contains students. A student cannot exist without a class. There exists
composition between class and students
4 Employees of a department
Their Manager
But, it cannot directly access those members of the super class that have been declared as
private.
class A{
int money;
private int pocketMoney;
This is the only exception to the rule that a subclass inherits all the properties of its
superclass
class X {
X( ){
System.out.println(“Inside X’s Constructor”); } }
class Y extends X {
Y( ) {
System.out.println(“Inside Y’s Constructor”); } }
class Z extends Y {
Z(){
System.out.println(“Inside Z’s Constructor”); } }
class OrderOfConstructorCallDemo{
public static void main(String args[]){
You can easily find the output of
Z z = new Z(); this program..
}
}
© 2017 Wipro wipro.com confidential 30
Constructors – Order of Invocation (Contd.).
When we invoke a super() statement from within a subclass constructor, we are invoking the
immediate super class’ constructor
Remember, super( ) can only be given as the first statement within a constructor
first line of a constructor must EITHER be a super (call on the super class constructor)
OR a this (call on the constructor of same class)
If the first statement within a constructor is NEITHER super() NOR this(), then the
compiler will automatically insert a super(). (That is, invocation to the super class’ no
argument constructor)
class A1 {
A1(){ System.out.println("A1's no arg constructor"); }
A1(int a){ System.out.println(“A1's constructor "+ a); }
}
class B1 extends A1{
B1(){ System.out.println("B1's no arg constructor"); }
B1(int b){ super(1000);
System.out.println(“B1's constructor "+ b); }
}
class C1 extends B1{
C1() {System.out.println(“C1's no arg constructor"); }
C1(int c){ super(100);
System.out.println(“C1's constructor "+ c); }
}
class TestingInheritance{
public static void main(String args[]){ The participants are expected to
C1 ca = new C1(); answer this question during session
}
}
© 2017 Wipro wipro.com confidential 33
QUIZ(Contd.).
class A1 {
A1(){ System.out.println("A1's no arg constructor"); }
A1(int a){ System.out.println(“A1's constructor "+ a); }
}
class B1 extends A1{
B1(){ System.out.println("B1's no arg constructor"); }
B1(int b){ super(1000);
System.out.println(“B1's constructor "+ b); }
}
class C1 extends B1{
C1() {System.out.println(“C1's no arg constructor"); }
C1(int c){ super(100);
System.out.println(“C1's constructor "+ c); }
}
class TestingInheritance{ The participants are expected to
public static void main(String args[]){
C1 ca = new C1(10); answer this question during
} session
}
© 2017 Wipro wipro.com confidential 34
QUIZ(Contd.).
class A1 {
A1(){ System.out.println("A1's no arg constructor"); }
A1(int a){ System.out.println(“A1's constructor "+ a); }
}
class B1 extends A1{
B1(){ System.out.println("B1's no arg constructor"); }
B1(int b){ super(1000);
System.out.println(“B1‘s constructor "+ b); }
}
class C1 extends B1{
C1() {System.out.println(“C1's no arg constructor"); }
C1(int c){ System.out.println(“C1's constructor "+ c); }
}
class TestingInheritance{
public static void main(String args[]){
C1 ca = new C1(10); The participants are expected to
}
}
answer this question during session
© 2017 Wipro wipro.com confidential 35
QUIZ(Contd.).
class A1 {
A1(){ System.out.println("A1's no arg constructor"); }
A1(int a){ System.out.println(“A1's constructor "+ a); }
}
class B1 extends A1{
B1(){ System.out.println("B1's no arg constructor"); }
B1(int b){ System.out.println(“B1‘s constructor "+ b); }
}
class C1 extends B1{
C1() { super(100);
System.out.println(“C1's no arg constructor"); }
C1(int c){ System.out.println(“C1's constructor "+ c); }
}
class TestingInheritance{
public static void main(String args[]){
C1 ca = new C1(10); The participants are expected to
} answer this question during session
}
© 2017 Wipro wipro.com confidential 36
QUIZ(Contd.).
class A1 {
A1(){ System.out.println("A1's no arg constructor"); }
A1(int a){ System.out.println(“A1's constructor "+ a); }
}
class B1 extends A1{
B1(){ super(50);
System.out.println("B1's no arg constructor"); }
B1(int b){ super(1000);
System.out.println(“B1‘s constructor "+ b); }
}
class C1 extends B1{
C1() {System.out.println(“C1's no arg constructor"); }
C1(int c){ System.out.println(“C1's constructor "+ c); }
}
class TestingInheritance{
public static void main(String args[]){
C1 ca = new C1(10); The participants are expected to answer
} this question during session
} © 2017 Wipro wipro.com confidential 37
QUIZ(Contd.).
class A1 {
A1(){ System.out.println("A1's no arg constructor"); }
A1(int a){ System.out.println(“A1's constructor "+ a); }
}
class B1 extends A1{
B1(String x){ super(50);
System.out.println("B1's no arg constructor"); }
B1(int b){ super(1000);
System.out.println(“B1‘s constructor "+ b); }
}
class C1 extends B1{
C1() {System.out.println(“C1's no arg constructor"); }
C1(int c){ super(100);
System.out.println(“C1's constructor "+ c); }
}
class TestingInheritance{
public static void main(String args[]){
C1 ca = new C1(10); The participants are expected to
} answer this question during session
}
© 2017 Wipro wipro.com confidential 38
QUIZ(Contd.).
class A1 {
A1(){ System.out.println("A1's no arg constructor"); }
A1(int a){ System.out.println(“A1's constructor "+ a); }
}
class B1 extends A1{
B1(){ System.out.println("B1's no arg constructor"); }
B1(int b){ this("x");
System.out.println(“B1's constructor "+ b); }
B1(String b){ super(1000);
System.out.println(“B1's constructor "+ b); }
}
class C1 extends B1{
C1() {System.out.println(“C1's no arg constructor"); }
C1(int c){ super(100);
System.out.println(“C1's constructor "+ c); }
}
class TestingInheritance{
public static void main(String args[]){
C1 ca = new C1(10);
} The participants are expected to answer
} this question during session
© 2017 Wipro wipro.com confidential 39
Multilevel Hierarchy
We can define a superclass and a subclass, with the subclass in turn becoming a superclass
for another subclass