Java Inheritance and the super Keyword

Welcome! In this part, we'll focus on inheritance in object-oriented programming (OOP) with Java. Inheritance allows us to share code across classes, thereby enhancing readability and efficiency.

In this lesson, we'll clarify attribute and method inheritance in Java through practical examples. Our lesson's outline includes defining inheritance, exploring attribute inheritance, understanding method inheritance, and decoding the super keyword in Java. Ready? Let's get started!

Defining Inheritance

Inheritance in Java involves creating a subclass that inherits attributes and methods from a superclass. In Java, many scenarios allow classes to share common attributes or methods, making inheritance very useful.

The extends keyword is used to set up inheritance, allowing one class to inherit properties and methods from another class. Here's an example featuring a superclass named Vehicle and a subclass named Car:

In the Car class, the super() constructor within its constructor calls the Vehicle class's constructor, ensuring that inherited properties are initialized correctly. The extends keyword indicates that Car is a subclass of Vehicle.

Our focus in this lesson is primarily on single inheritance, where one superclass is extended by a single subclass.

Attribute Inheritance

Attribute inheritance allows a subclass to inherit the attributes of its superclass, except for private fields, which are not directly accessible in subclasses.

Consider this example featuring a superclass named Artist, and a subclass named Musician:

The Musician class inherits the name attribute from the Artist class and adds its own unique attribute, instrument. Access to the name attribute is provided via a getter method since it is private.

Method Inheritance

Similar to attributes, method inheritance allows a subclass to inherit the methods of its superclass.

In the example below, the Car class can invoke the start method from the Vehicle class:

In the absence of a specific constructor in the Car class, Java provides a default constructor that internally calls super() to ensure the superclass's constructor is invoked. This initialization allows the Car class to inherit the brand attribute from the Vehicle class seamlessly.

Understanding the super Keyword in Method Overriding

The super keyword is essential in inheritance for calling superclass methods from a subclass, especially useful in method overriding. It enables a subclass to extend or utilize the functionality of a superclass without directly modifying it.

Method overriding in Java allows a subclass to provide a specific implementation of a method already defined in its superclass. This is achieved by using the @Override annotation in the subclass.

For instance, when overriding a method to add or alter its behavior, super can be used to call the original method from the superclass and integrate its functionality with new enhancements:

In this way, super allows subclasses to build upon or adapt the functionality of their superclasses efficiently and cleanly.

Lesson Summary

We've successfully explored attribute and method inheritance in Java and practiced using several examples. Mastering these concepts in real-life programming can enhance both efficiency and readability. Remember, practice is essential for proficiency!

On that note, are you ready for some practice exercises? They will solidify your understanding and prepare you for more complex programming tasks. Programming is all about experimenting, learning, and problem-solving. Enjoy the journey!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal