Chapter Four
INHERITANCE
Tuesday, May 26, 2026
Inheritance
2
Inheritance is a parent-child relationship between
classes
It allows sharing of the behavior of the parent class
into its child classes
One of the major benefits of object-oriented
programming (OOP) is this code sharing between
classes through inheritance
Child class can have new behavior or override
existing behavior from parent
Tuesday, May 26, 2026
Cont.…
3
In the terminology of Java, a class that is inherited is called a
superclass. The class that does the inheriting is called a subclass.
Therefore, a subclass is a specialized version of a superclass. It
inherits all of the instance variables and methods defined by the
superclass and adds its own, unique elements except private
instances.
In Java, each class is allowed to have one direct superclass, and
each superclass has the potential for an unlimited number of
subclasses.
Constructors are not members, so they are not inherited by
subclasses, but the constructor of the superclass can be invoked
from the subclass.
Tuesday, May 26, 2026
Syntax
4
The syntax for creating a subclass, use the extends keyword,
followed by the name of the class to inherit from:
public class SuperClassName{
//fields and methods of the super class
}
public class SubClassName extends SuperClassName{
Declaration_of_Added_InstanceVariables;
Declaration_of_Added_Methods;
// new fields and methods other than super class
}
Tuesday, May 26, 2026
Cont.…
5
Subclass: A class that is derived from another class.
Also called a derived class, extended class, or child
class
Superclass: The class from which the subclass is
derived. Also a base class or a parent class.
Classes can be derived from classes that are derived
from classes, and so on, and ultimately derived from
the topmost class.
Tuesday, May 26, 2026
Example 1
6
Public Class A
{
public static void main(String
public void methodA()
args[])
{
{
[Link]("Base class method");
B obj = new B();
}
[Link](); //calling super
}
class method
Public Class B extends A
[Link](); //calling local
{
method
public void methodB()
}}
{
[Link]("Child class method");
Tuesday, May 26, 2026
Example 2
7
Public class A { class SimpleInheritance {
int i, j; public static void main(String
void showij() { args[]) {
A superOb = new A();
[Link]("i and j: "+ i + " "+j);
}
B subOb = new B();
superOb.i = 10;
} superOb.j = 20;
public class B extends A { [Link]("superOb Contents :");
int k; [Link]();
void showk() { [Link]();
[Link]("k: " + k); subOb.i = 7; subOb.j = 8;
subOb.k = 9;
} [Link](“subOb Contents : ");
void sum() {
[Link]("i+j+k: “+(i+j+k)); [Link](); [Link]();
} [Link]("Sum of i, j
}
and k in subOb:");
[Link]();
}
}
Tuesday, May 26, 2026
Example 3
8
public void dispC(){
public class ClassA [Link]("disp() method of
ClassC");
{
}
public void dispA() public static void main(String args[])
{ {
[Link]("disp() method of ClassA"); //Assigning ClassC object to ClassC
} } reference
public class ClassB extends ClassA ClassC c = new ClassC();
{ //call dispA() method of ClassA
public void dispB()
[Link]();
//call dispB() method of ClassB
{
[Link]();
[Link]("disp() method of ClassB");
//call dispC() method of ClassC
} }
[Link]();
public class ClassC extends ClassB }
{ }
Tuesday, May 26, 2026
Cont...
9
Although a subclass includes all // A's j is not accessible here.
of the members of its class B extends A {
superclass, it cannot access int total;
those members of the void sum() {
superclass that have been total = i + j;
declared as private. For // ERROR, j is not accessible here
example, consider the }
following simple class }
hierarchy:
Example :
class Access {
public static void main(String args[]){
class A {
int i; // public by default
B subOb = new B();
private int j; // private to A [Link](10, 12);
void setij(int x, int y) { [Link]();
i = x; [Link]("Total is " +
j = y; [Link]);
} }
}
}
Tuesday, May 26, 2026
Diagram Representation
10
Tuesday, May 26, 2026
Inheritance Versus Composition
11
Inheritance
➢ It is-a-relationship technique
Composition
➢ Has-a-relationship technique
class Fruit {
class Fruit { //... }
//... } class Apple {
private Fruit fruit ;
class Apple //... }
extends Fruit {
//... }
Tuesday, May 26, 2026
Method Overriding
12
If subclass (child class) has the same method as declared
in the parent class, it is known as method overriding
in Java.
In other words, If a subclass provides the specific
implementation of the method that has been declared by
one of its parent class, it is known as method overriding.
Rules for Java Method Overriding
The method must have the same name as in the parent
class
The method must have the same parameter as in the
parent class.
There must be an IS-A relationship (inheritance).
Tuesday, May 26, 2026
Example
13
class Vehicle{
//defining a method
void run(){ • If the object is
[Link]("Vehicle is running"); created from the
} superclass it invoke
}
the method from
//Creating a child class
class Bike extends Vehicle{
superclass.
//defining the same method as in the parent class • If the object is
void run(){ created from
[Link]("Bike is running safely"); subclass it call both
} } methods
class Test{
public static void main(String args[]){
Bike obj = new Bike();//creating object
[Link]();//calling method
}
}
Tuesday, May 26, 2026
14
The are methods that you can’t override
[Link] Methods
2. Final Methods
[Link] in final class
Tuesday, May 26, 2026
Default Constructor in Inheritance
15
When You create an object from subclass, you are
actually calling at least two constructors.
Constructor of Base Class
Constructor of Subclass
In Java, constructor of base class with no argument
gets automatically called in derived class constructor.
The superclass Constructor is execute First
Tuesday, May 26, 2026
Example
16
class Base {
public Base() { Output
[Link]("Base Class Constructor Called "); Base Class Constructor Called
} Derived Class Constructor Called
}
class Derived extends Base {
public Derived() {
[Link]("Derived Class Constructor Called ");
}
}
public class Demo {
public static void main(String[] args) {
Derived d = new Derived();
}
}
Tuesday, May 26, 2026
Parameterized constructor in inheritance
17
When a superclass contains only constructor that
require arguments you must at least include one
constructor for each subclass you create
If we want to call parameterized constructor of base
class, then we can call it using super(argument).
Use super key word as the name of base class
The superclass constructor must have to be called in
the subclass constructor if it contains an argument
and the call must be on the first line in the subclass
constructors.
Tuesday, May 26, 2026
Example
18
class Base {
int x; public class test{
public Base (int x) { public static void main(String[] args) {
this.x = x; Derived d = new Derived(10, 20);
} [Link]();
} }
}
class Derived extends Base {
int y;
public Derived(int x, int y) {
super(x); Output:
this.y = y; x = 10, y = 20
}
void Display() {
[Link]("x = "+x+", y = "+y);
}}
Tuesday, May 26, 2026
Super for calling Parent Constructor
19
Public class ParentClass Parent Class default Constructor
{
Child Class default Constructor
public ParentClass()
{
[Link]("Parent Class default
Constructor"); }}
public class SubClass extends ParentClass
{
public SubClass() {
super();
[Link]("Child Class default
Constructor"); }
public static void main(String args[])
{
SubClass s = new SubClass();
}}
Tuesday, May 26, 2026
Super for accessing parent class datamember
20
Public class ParentClass
Output:
{
int val=550; Value is : 550
}
public class SubClass extends ParentClass
Value is : 600
{
int val=600;
public void disp()
{
[Link]("Value is : "+[Link]);
[Link]("Value is : "+val); }
public static void main(String args[])
{
SubClass s = new SubClass();
[Link]();
}}
Tuesday, May 26, 2026
Super for calling parent class methods
21
Public class ParentClass { Output:
public void disp()
{ Child Class method
[Link]("Parent Class method");
}} Parent class method
public class SubClass extends ParentClass
{
public void disp()
{
[Link]("Child Class method"); }
public void show(){
disp();
[Link]();
}
public static void main(String args[]){
SubClass s = new SubClass();
[Link](); }}
Tuesday, May 26, 2026
This constructor
22
Sometimes when you want to call one of the other
constructors in the same class use this as constructor
name.
Example
public class person{
public person(){
this(“yohannes”);
}
public person (String n){
name=n;
}
}
Tuesday, May 26, 2026
Example
23
public class Sudden{ class Test{
int a,b;
public void Java(){ public static void main(String[] args) {
[Link](“Answer of Sudden Quiz"); A b=new A();
}
public void display(){ Sudden c=new Sudden();
[Link](a); [Link]("Your Output Must Be Correct!");
[Link](b);
}} [Link]();
[Link]();
class A extends SuddenQuiz{
}}
public void Java(){
super.b=5;
[Link]();
[Link](b);
}
}
Tuesday, May 26, 2026
24
Tuesday, May 26, 2026