Programming in Java, 2e Sachin Malhotra Saurabh Choudhary
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary
Sachin Malhotra
Saurabh Choudhary
Chapter 3
Inheritance
Objectives
• Know the difference between Inheritance and
aggregation
• Understand how inheritance is done in Java
• Learn polymorphism through Method
Overriding
• Learn the keywords : super and final
• Understand the basics of abstract class.
Inheritance
• Is the ability to derive something specific from
something generic.
• aids in the reuse of code.
• A class can inherit the features of another class and
add its own modification.
• The parent class is the super class and the child class
is known as the subclass.
• A subclass inherits all the properties and methods of
the super class.
Inheritance
Aggregation
• We can make up objects out of other objects.
• The behaviour of the bigger object is defined by the
behaviour of its component objects, separately and
in conjunction with each other.
• cars contain engine which in turn contains an ignition
system and starter motor.
• a whole-part relationship exists in aggregation
• car being the whole and engine being its part.
Types of Inheritance
• Single
• Multiple
• Multilevel
• Hierarchical
• Hybrid
Single level inheritance
Classes have only base class
Multi level
There is no limit to this chain of inheritance (as
shown below) but getting down deeper to four or
five levels makes code excessively complex.
Multiple Inheritance
A class can inherit from more than one unrelated
class
Hierarchical Inheritance
Constructor A
Constructor B
Constructor C
Case 3: (contd.)
import java.io.*;
class Constructor_A_Revised
{
Constructor_A_Revised()
{
System.out.println("Constructor A Revised");
}
}
class Constructor_B_Revised extends Constructor_A_Revised
{
Constructor_B_Revised()
{
System.out.println("Constructor B");
}
Constructor_B_Revised(int a)
{
a++;
System.out.println("Constructor B Revised "+ a );
}
}
class Constructor_C_Revised extends Constructor_B_Revised
{
Constructor_C_Revised()
{
super(11);
System.out.println("Constructor C Revised");
}
public static void main (String args[])
{
Constructor_C_Revised a=new Constructor_C_Revised();
}
}
The Output
Constructor A Revised
Constructor B Revised 12
Constructor C Revised
final keyword
• Declaring constants (used with variable and
argument declaration)
– E.g. final int MAX=100;
• Disallowing method overriding (used with method
declaration)
– final void show (final int x)
{
// statements
}
• Disallowing inheritance (used with class declaration).
– final class Demo
{
//statements
}
Your Turn
class Demo
{
Demo(int i)
{
System.out.println(“Demo”);
}
}
class InnerClass extends Demo
{
public static void main(String args[])
{
InnerClass s=new InnerClass();
}
void InnerClass()
{
System.out.println(“Inner Class”);
}
} © Oxford University Press 2013. All rights reserved.
a) Compilation and output of string is “Inner Class ” at
run time.
b) Compile time error
c) Compilation and no output at run time.
d) Compilation and output of string is “Demo”