Skip to main content

Practical-21

Write a java program which explains the concept of Single Inheritance.

Introdution

Single Inheritance is a process in which a derived class can acquire the properties of a single base class. The derived class is referred to as the child class and the base class as the parent class. Single Inheritance allows a child class to inherit the methods and variables of the parent class, thus allowing code reuse and improved code readability.

Code

// Java Program to illustrate
// the concept of Single Inheritance

// Base class
class ParentClass
{
void disp()
{
System.out.println("Parent class");
}
}

// Sub Class
class SubClass extends ParentClass
{
void disp2()
{
System.out.println("SubClass");
}

public static void main(String[] args)
{
// creating object of SubClass
SubClass obj = new SubClass();

// calling disp() of Parent
obj.disp();
}
}

Output

Parent class
Explanation

The code above shows an example of single inheritance. The SubClass must extend the ParentClass in order to use the code within the ParentClass. The SubClass can then use the methods from the parent class and also create its own methods.