Inheritance and Abstraction

Inheritance and Abstraction

We know Object Oriented Programming provides security to code, reusability and we can divide our code, etc. But in this blog, we will see how OOPS does it, so basically we will go through the four Pillars of Object Oriented Programming:

  • Inheritance

  • Polymorphism

  • Abstraction

  • Encapsulation

These are the main pillars of OOPs that we use while coding and this is the reason for all benefits of OOPs. In this blog, we will try to understand only Inheritance and Abstraction, the other two we will see in the next blog.

Inheritance

In Object Oriented Programming, inheritance is a feature to create a new class from an existing class. The new class here is known as sub class or derived class, and the class from which is derived is known as the parent class or super class or base class. The new class inherits the properties and method of the existing class.

In java, inheritance is implemented using the "extends" keyword. A class can extend only one class, but a class can be extended by multiple classes. For example, the following code creates a class called "vehicle" as the superclass, and a class called "car" as the subclass:

class Vehicle{
    int vehicleNo;
    String name;

    void display(){
        System.out.println("Vehicle No: "+ vehicleNo);
        System.out.println("Name: " + name);
    }
}

class Car extends Vehicle{
    int seats;

    void display(){
        System.out.println("seats: " + seats);
        super.display();
    }
}

In this example, the "Car" class inherits the properties "vehicleNo" and "name" from the "Vehicle" class, as well as the method "display()". The "Car" also has its own property, "seats", and its own implementation of the "display()" method that calls the method from the superclass using "super" keyword. Please don't scratch your head for what is super keyword, for now just note it is used to call superclass from sub class. we will learn about this in much detail in coming blogs.

Inheritance is one of the fundamental concepts of object-oriented programming, we also have different types of inheritance:

  • Single level Inheritance: A subclass inherits properties and methods from a single superclass.

  • Multilevel Inheritance: A subclass inherits properties and methods from a superclass, which in turn inherits from another superclass.

  • Hierarchical Inheritance: Multiple subclass inherits properties and methods from the same superclass is Hierarchical Inheritance.

  • Hybrid Inheritance: Hybrid Inheritance is nothing but the combination of the above types of Inheritance.

  • Multiple Inheritance: A subclass inherits properties and methods from multiple superclasses. However, multiple inheritance is not supported in Java, but it can be achieved by interfaces.

Abstraction

In OOPs abstraction is used to hide the implementation of a particular class or to give access to only some part of your code to other classes.

It is one of the fundamental concepts in the learning journey of OOPs that we should know.

There are two main ways to achieve abstraction in Java:

  1. Abstract Classes: An Abstract class is a class that cannot be called, but can be subclassed. We can say as it is the blueprint for other classes and can contain both abstract and concrete methods. Abstract methods are methods that have signatures but no implementation. This method must be implemented by the subclass that extends the abstract class.

     abstract class Vehicle {
         abstract void type();
     }
     class Car extends Vehicle {
         void type() {
             System.out.println("This is a Car");
         }
     }
    
  2. Interface Classes: An interface is a collection of abstract methods, which do not have a body implementation. A class that implements an interface should implement all of its methods. Interfaces can be used to achieve multiple inheritance in Java, which is not possible with classes.

     interface Vehicle {
         void type();
     }
    
     class Car implements Vehicle {
         void type() {
             System.out.println("This is a Car");
         }
     }
    

Do note that abstract class is implemented using extends keyword and interface is implemented using implements keyword. The idea behind abstraction is to create user-friendly interface for interacting with complex code base. It helps to hide the implementation of a class or object and allows you to focus on the essential features of the system.

Thank you for reading. Hope you understood the concepts.

Did you find this article valuable?

Support Abhishek Madankar by becoming a sponsor. Any amount is appreciated!