Hashtag Technophile

Inheritance In Java Ft. Superstar Vs Little Superstar

Rajinikanth, known for his unique style, swag and iconic roles, has set the foundation for modern Tamil cinema. Definitely, we would have tried to resemble / enact his style or behaviour atleast once in our life time. Not only the common audience, even he has left an inheritance of inspiration for newer generations of actors with his unique style and body language. Just as a superclass imparts its characteristics to its subclasses, Rajinikanth’s influence and style have inspired and influenced many actors who have come after him, contributing to the evolution of Tamil cinema.

Simbu, often referred to as “Little Superstar,” can be compared to the subclass in Java inheritance. Just as a subclass inherits attributes and methods from a superclass, Simbu has inherited Rajinikanth’s legacy while also contributing his own unique approach to acting.

So now, just think of Rajinikanth as the superclass, passing down his charismatic style and influential performances to actors who followed, much like a superclass passing down its attributes. Simbu, in this analogy, represents a subclass inheriting Rajinikanth’s influence while adding his own flavour to create a distinctive acting style, just as a subclass modifies or extends superclass behaviours. So, this recognition of these two actors as Superstar and Little Superstar is quite similar to Java’s inheritance feature where the child class tries to inherit properties and methods from the parent class.

So, by this time I hope you can get an overall view of what inheritance in Java is. Now let’s move on to the actual technical definition of inheritance.

Inheritance is a fundamental concept in object-oriented programming, including Java. It allows you to create a new class that inherits properties and behaviours (methods) from an existing class. The class that is being inherited from is called the superclass or base class, and the class that inherits from it is called the subclass or derived class

The extends keyword is used to perform inheritance in Java. For example,

class MobilePhones {
  // methods and fields
}

// use of extends keyword
// to perform inheritance
class Samsung extends MobilePhones {

  // methods and fields of MobilePhones
  // methods and fields of Samsung
}

In the above example, the Samsung class is created by inheriting the methods and fields from the MobilePhones class.

Here, Samsung is the subclass and MobilePhones is the superclass.

Method Overriding in Java Inheritance:

In the above example, we see the object of the subclass can access the method of the superclass.

However, what if the same method is present in both the superclass and subclass? In that case, what happens?

In this case, the method in the subclass overrides the method in the superclass. This concept is known as method overriding in Java.

class Cars {

  public void ride() {
    System.out.println("I can ride");
  }
}

class Audi extends Cars {

  @Override
  public void ride() {
    System.out.println("I ride");
  }

  public void drift() {
    System.out.println("I can drift");
  }
}

class Main {
  public static void main(String[] args) {

    Audi Q5 = new Dog();

    Q5.ride();
    labrador.drift();
  }
}

In the above example, the ride() method is present in both the superclass Animal and the subclass Dog.

Here, we have created an object Q5 of Audi.

Now when we call ride() using the object Q5, the method inside Audi is called. This is because the method inside the derived class overrides the method inside the base class. This concept of overriding is called method overriding.

Why Inheritance?

The significant use of inheritance in Java is code reusability. The code that is present in the parent or superclass can be directly used by the child or derived class. So obviously this reduces the length of the duplicate code.

Remember the points:

Java Inheritance is transitive – so if Audi extends Car and Car extends Bike, then Audi is also inherited from the Bike class. The Bike becomes the superclass of both Cars and Audi.

The method overriding is also known as runtime polymorphism. Hence, we can achieve Polymorphism in Java with the help of inheritance.

For now, this is enough! But remember there are a lot of different types in Java inheritance and a lot of different types of access specifiers for members inside a class which denotes whether a member from a class can be accessed or not. Will try to have a separate article in the future so that we can handle these things in detail.

If you have read this far, I hope you are finding it useful.

Make sure you rate or comment on this article and do share & subscribe to our website because that encourages our team to write more.

Cheers! Until next time…❤️

0 0 votes
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments
Scroll to Top