OOP(s): The Inheritance

OOP(s): The Inheritance

Reuse, Reuse, and Reuse.

Scenario

inh_start.jpeg

The capacity to talk, breathe, eat and drink are just a few of the traits we get from the class Parent to Children.

The example of automobiles is another option. The class "Car" derives some of its attributes from the class "Automobiles," which in turn derives some of its attributes from a different class called "Vehicles."

inheritance-in-java.webp

The object-oriented languages express this inheritance relationship by allowing one class to inherit from another. Thus a model of these languages is much closer to the real world.

The principle behind this kind of division is that each subclass (child-class) shares common characteristics with the class from which it is derived.

Inheritance: The Definition

Inheritance is a critical feature in which one object acquires the properties of the parent class. It is a crucial aspect of OOPs (Object Oriented programming systems). Before understanding inheritance, let’s briefly understand Java. Inheritance in Java is the core feature of object-oriented programming. It facilitates a derived class to inherit the features from the parent class, through this hierarchical process the classes share various features, attributes, methods, etc.

Inheritance: The Syntax

Super Class

class Parent
{
    //Attributes & Behaviour
}

Child Class

class Child extends Parent{
    //Attributes & Behaviour from Super Class
    //Derived class Attributes & Behaviour 
}

Terminologies to be remembered

  • Class: In Java, a class is a user-defined datatype that is essentially a collection of objects. It serves as a model or blueprint from which we may construct items.

  • Super Class: The class whose features and functionalities are being inherited or used is known as the superclass or a base class or a parent class.

  • Sub Class: A subclass, derived class, extended class, or child class is a class that derives its characteristics from another class. In addition to the fields and methods of its superclass or parent class, the subclass may also add new features and functionalities.

  • The extends keyword: Child classes that inherit from their parents utilize the extends keyword.

Inheritance: The Example

Assume that we have a parent class called "Person", and the derived class "Student" extends from the Person class.

Person class

class Person
{
  String name = "Yolo";
  int age =898989;
  String city = "Yolo World";
  public void show()
  {
    System.out.println("Student inheriting properties from Person:\n");
  }
}

Student class

class Student extends Person
{
   int marks = 78;
  public static void main(String args[])
  {
    Student obj = new Student();
    obj.show();
    System.out.println("Name of the student is: " + obj.name);
    System.out.println("Age of the student is: " + obj.age);
    System.out.println("Student lives in: " + obj.city);
    System.out.println("Student learns from: " + obj.tutorial);
    System.out.println("Marks obtained by the student is: " + obj.marks);
  }
}

Inheritance: The Types

  • Single Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

But however, Java doesn't support the Multiple Inheritance by classes.

Single Inheritance

Single.png In single inheritance, there is a single child class that inherits properties from one parent class.

//Super Class
Class A
{
   public void methodA()
   {
     System.out.println("Base class method");
   }
}

//Derived Class
Class B extends A
{
   public void methodB()
   {
     System.out.println("Child class method");
   }
   public static void main(String args[])
   {
     B obj = new B();
     obj.methodA(); 
     obj.methodB(); 
  }
}

Multilevel Inheritance

multilevel.jpg

In this type of inheritance, the child or derived class inherits the features of the superclass, and simultaneously this child class acts as a superclass for another derived class.

Class X
{
   public void methodX()
   {
     System.out.println("Class X method");
   }
}
Class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
}
}
Class Z extends Y
{
   public void methodZ()
   {
     System.out.println("class Z method");
   }
   public static void main(String args[])
   {
     Z obj = new Z();
     obj.methodX(); 
     obj.methodY(); 
     obj.methodZ(); 
  }
}

Hybrid Inheritance with Interface

hybrid.png

Hybrid inheritance is a combination of single and multiple inheritances. Java does not support multiple inheritances; hence Hybrid Inheritance can be achieved through interfaces.

public class ClassA 
{
    public void dispA()
    {
        System.out.println("disp() method of ClassA");
    }
}

//Interface1
public interface InterfaceB 
{
    public void show();
}

//Interface2
public interface InterfaceC 
{
    public void show();
}

public class ClassD extends ClassA implements InterfaceB,InterfaceC
{
    public void show()
    {
        System.out.println("show() method implementation");
    }
    public void dispD()
    {
        System.out.println("disp() method of ClassD");
    }
    public static void main(String args[])
    {
        ClassD d = new ClassD();
        d.dispD();
        d.show();
    }
}

Hierarchical Inheritance

2d7ec797-b57a-419a-b029-f828685f17d8.png The type of inheritance in which more than one derived class inherits the properties of the same base class is called hierarchical inheritance. There are multiple child classes and a single parent class.

class A
{
   public void methodA()
   {
      System.out.println("method of Class A");
   }
}
class B extends A
{
   public void methodB()
   {
      System.out.println("method of Class B");
   }
}
class C extends A
{
  public void methodC()
  {
     System.out.println("method of Class C");
  }
}
class D extends A
{
  public void methodD()
  {
     System.out.println("method of Class D");
  }
}
class JavaExample
{
  public static void main(String args[])
  {
     B obj1 = new B();
     C obj2 = new C();
     D obj3 = new D();
     //All classes can access the method of class A
     obj1.methodA();
     obj2.methodA();
     obj3.methodA();
  }
}

Multiple Inheritance with Interface

multiple-inheritance.png

An interface contains variables and methods like a class but the methods in an interface are abstract by default, unlike a class. Multiple inheritance by interface occurs if a class implements multiple interfaces or also if an interface itself extends multiple interfaces.

interface AnimalEat {
   void eat();
}
interface AnimalTravel {
   void travel();
}
class Animal implements AnimalEat, AnimalTravel {
   public void eat() {
      System.out.println("Animal is eating");
   }
   public void travel() {
      System.out.println("Animal is travelling");
   }
}
public class Demo {
   public static void main(String args[]) {
      Animal a = new Animal();
      a.eat();
      a.travel();
   }
}