β˜•οΈ Inheritance

5 min read > Topic Docs > Anchal Bhardwaj > 4/2/22


WHAT IS INHERITANCE?

Inheritance is where classes adopt a hierarchical structure, inheriting properties from another class. It is often used in Object Oriented programming, which Java happens to be. Inheritance is especially key for code reusability which means we can have code from a base class that we use for other classes without copy pasting values and methods. Let’s go into more detail.

We use a super class which has methods and variables that are inherited by the sub class. These fields and methods can be used in the subclass method directly, as if they were part of the class itself. In addition to the inherited properities, we can also create new fields and instance methods that have the same method signature (overriding) or create completely new methods that are not in the super class at all. Lastly, we can also make a subclass constructor which invokes the constructor in the superclass (keyword super) or make a unique constructor.

If we want to think of it in terms of real life examples, think of a Car superclass and subclasses which are different types of cars. Each of the subclasses (types of Car) have their own properties but inherit common properties from the super class.

BASIC TERMINOLOGY

Super Class: The class whose features are inherited is known as superclass(or a base class or a parent class).

Sub Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.

Extends: keyword used to inherit the properties of a class

Super: a keyword that differentiates the members of superclass from the members of subclass, if they have the same names and are also used to invoke the super class constructor in the super class.

OVERLOADING AND OVERRIDING

Overloading and overriding are used when we want to inherit or ignore methods used in the subclass and superclass. Overloading is used for methods that are in the same class but have the same name. In order to allow the compiler to differentiate between the two, we have to have either a different return type or different arguments being passed in as indicated in the method signature.

Overriding is where it gets fun. Overriding is when we have a method that is already defined in the super class but we want to change it up with the subclass so we put an override statement on top of the method we are changing and then redefine the method in the subclass to do something different.

SINGLE INHERITANCE VS MULTILEVEL INHERITANCE

Single Inheritance is the base version of Inheritance which means that subclasses are inheriting from one super class. In the class definition, the subclass would extend from the superclass and inherit all of the public methods and variables from the superclass.

Multilevel Inheritance is where a subclass will inherit from another superclass while also behaving as a super class for another class. For example, A is a superclass for B and B is a superclass for C. It sounds confusing but with a visual representation, it starts to clear up.



There are other types of inheritance including Hierarchical Inheritance, Multiple Inheritance, and Hybrid Inheritance which work through Java interfaces but they are not as commonly used in FIRST programming. More information can be found here.

EXAMPLES

Here are some code examples and walkthroughs that you can look at which demonstrate inheritance with real life objects. (1, 2, 3)