OOPC - Inheritance (class – class)


In everyday speech, inheritance means receiving assets, property, or characteristics from someone else, typically from a family member (such as a parent). The inheritor (such as a child) can use these as they are or modify them as needed.

For example, imagine a child inherits a car from a parent. The child can use the car as it was inherited or modify its features as needed. However, once the car has been modified, it is no longer possible to revert to the exact original state of the car.

In programming, when we modify inherited features and behaviors, the original structure remains unchanged. We can always access the original version of the inherited class without affecting its original state.


This concept is analogous to inheritance in Object-Oriented Programming (OOP). In Java, inheritance can occur in several scenarios:

  1. class – class (extends)
  2. class – abstract class (extends)
  3. class – interface (implements)
  4. abstract class – interface (implements)
  5. interface – interface (extends)
more on class, abstract class, and interface : here

An important thing to remember is that if a method or variable of the parent class contains the private  modifier then those are not inherited directly in the sense that the child class cannot access them. Even though all the members of the parent class exist in the child class, the child class has no direct access to them. To allow access, you would need to use protected or public access modifiers, or provide getter and setter methods.

Another important thing is, if a method in the parent class is marked with the final keyword, it cannot be overridden by the child class. The final keyword prevents a method from being modified in child class.

1. class – class (extends)

Lets take the previous example. When we represent this inheritance type in java first we need to develop 2 separate classes for each entity associated with this scenario (parent and child).  Lets say the parent has the following properties (variables) and behaviors (methods).


So, the parent need to have a behavior called driveCar() and 2 properties called carName and carModel




We can develop parent class as follows:

package com;

public class Parent {

    String name = "John";
    String carModel = "Toyota Corolla";

    void driveCar() {
        System.out.println(name + " is driving a " + carModel);
    }
}

Now, let's assume that the child class needs to have the same details as the parent class, such as the ability to drive a car and the associated properties (name and carModel). Instead of rewriting the same code, we can use inheritance. The child class will extend the parent class, which means it will automatically inherit the properties with relevant  values and behaviors of the parent class.

We can develop child class as follows:

package com;

public class Child extends Parent{

    @Override
    void driveCar() {
        System.out.println(name + " is driving the modified " + carModel);
    }
}

By using the extends keyword, the Child class automatically inherits the name, carModel, and driveCar() method from the Parent class. We have also overridden the driveCar() method to provide a customized message when the child drives the car.

Now, let’s see how this inheritance structure works in practice by creating objects of both the Parent and Child classes:

package com;

public class Main {
    public static void main(String[] args) {
       
        Parent parent = new Parent();
        parent.driveCar();  

       
        Child child = new Child();
        child.driveCar();  
    }
}

The Parent object uses the driveCar() method from the Parent class. So, the output is: John is driving a Toyota Corolla.
The Child object also inherits the driveCar() method from the Parent class but uses its overridden version, resulting in the output: John is driving the Modified Toyota Corolla.

To Summerize:

Inheritance:
  1. Reuse code without duplicating it.
  2. Modify or extend behavior without changing the original class.

With inheritance, the child class gets all the properties and methods from the parent class. In the case of the child class, it can use the inherited methods and variables as it is or can modify them as needed without affecting parent class code.

Comments

Popular posts from this blog

Inheritance [ class – abstract class (extends) ]

OOP