Single Inheritance in Java


Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. Inheritance is a basic object oriented feature in which one class acquires and extends upon the properties of another class, using the keyword extends.

With the use of the extends keyword among classes, all the properties of the superclass (also known as the Parent Class or Base Class) are present in the subclass (also known as the Child Class or Derived Class)

Types of Inheritance :

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance

Single Inheritance
      Single inheritance is one base class and one derived class. One in which the derived class inherits the one base class either publicly, privately or protected

Syntax:
      class baseclass_Name
      {
            superclass data variables ;
            superclass member functions ;
      }
      class derivedclass_Name extends baseclass _Name
      {
            subclass data variables ;
            subclass member functions ;
      }

Source Code

//Single Inheritance in Java
class Father //Base
{
    public void house()
    {
        System.out.println("Have Own 2BHK House.");
    }
}
class Son extends Father //Derived
{
    public void car()
    {
        System.out.println("Have Own Audi Car.");
    }
}
 
public class single {
    public static void main(String args[])
    {
        Son o =new Son();
        o.car();
        o.house();
    }
}
 

Output

Have Own Audi Car.
Have Own 2BHK House.
To download raw file Click Here

Basic Programs