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)
- 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;
}Code Snippet
//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 App {
public static void main(String args[]) {
Son o = new Son();
o.car();
o.house();
}
}