In this tutorial, we will deep dive into what is inheritance in Java. We will focus on why it is important and how to implement Inheritance in Java.
What is Inheritance in Java?
Inheritance is a fundamental concept of Object-Oriented Programming due to which a class can inherit the features (not limited to methods and variables) of its parent class.
In Java, we use the “extends” keyword to mention the parent class. The properties and features of the mentioned parent class will be inherited into the class that is inheriting (also known as the sub-class).
Syntax:
class SubClass extends ParentClass { // Now this class has access to features of both SubClass and ParentClass }
Why do we need Inheritance in Java programming?
Inheritance saves us time and enables us to use code that already exists. Consider a scenario where you want to develop a class that will take input from the console that is being provided by the user using a keyboard. You will have to understand the technicality of writing in such a class. It might take a long time for a new developer or some amount of time for an experienced developer to develop such a class because all this will be done from scratch.
On the other hand, by the concept of inheritance, we can inherit a class that takes input from the console which is already developed by someone, and just add some extra functionality to it. You will also be able to modify some parts of the code if you want it. This process will take considerably very less time because inheriting a pre-built class will take 2-3 lines and nothing much. And the rest will be on want you want to add extra.
Where can I find pre-built Java classes that I can inherit into my sub-class?
Java has so many packages and each package has so many classes and methods that are pre-built for developers to use in their programs. These packages, classes, methods, etc are well tested and maintained by the Oracle Corporation. Follow the link to know more.
(https://docs.oracle.com/en/java/javase/index.html)
What are the types of Inheritance in Java?
In Java, a class can extend only one parent class at a time. Due to which there are only 3 types of inheritance supported in Java.
- Single-Level Inheritance
- Multi-Level Inheritance
- Hierarchical Inheritance
Single-Level Inheritance in Java
A class when extends another class is known as Single-Level inheritance. In this case, there is a sub-class that uses the “extends” keyword to extend another class.
Let us understand this with an example.
Consider a class named Cat which has a variable and a method in it.
class Cat { boolean canWalk = true; void saySomething() { System.out.println("Meow!"); } }
Let’s take another class named Kitty which will now extend the Cat class.
class Kitty extends Cat { // This class has all the features of the parent class Cat public static void main (String gts[]) { Kitty kitty = new Kitty(); System.out.println("Kitty can walk: " + kitty.canWalk); kitty.saySomething(); } } /* Output Kitty can walk: true Meow! */
This way, all the features of the Cat class are now in the Kitty class.
Multi-Level Inheritance in Java
Consider a class C which extends B and at the same time, B extends another class A. In this scenario, a class is inheriting its parent class and is also being a parent class to some other class.
Consider a parent class LandAnimal. This class has some common features that any land animal will have.
class LandAnimal { boolean canSee = true; boolean canFly = false; void canEatFood() { System.out.println("Yes, I can eat food!"); } }
Let’s take another class Cat from the previous example, A cat is also a land animal so it can also have all properties of a typical land animal. We can use the “extend” keyword to inherit all the features and properties of the LandAnimal class.
class Cat extends LandAnimal{ boolean canWalk = true; void saySomething() { System.out.println("Meow!"); } }
Now the Cat class has access to variables and methods of the LandAnimal class. We just extended it and didn’t have to write existing code from scratch.
Another class Kitty is introduced in the scenario which can extend the Cat class. The Cat class will now have all the features of the LandAnimal class as well as all the features of the Cat class.
class Kitty extends Cat { // This class has all the features of the parent class Cat public static void main (String gts[]) { Kitty kitty = new Kitty(); System.out.println("Kitty can walk: " + kitty.canWalk); System.out.println("Kitty can Fly: " + kitty.canFly); kitty.saySomething(); kitty.canEatFood(); } } /* Output Kitty can walk: true Kitty can Fly: false Meow! Yes, I can eat food! */
Hierarchical Inheritance in Java
A class can be inherited by many sub-classes. This means each sub-class can have a common parent class.
Let us understand with an example.
Consider a common parent class named LandAnimal.
class LandAnimal { boolean canSee = true; boolean canFly = false; boolean canWalk = true; void canEatFood() { System.out.println("Yes, I can eat food!"); } }
Consider two other classes named Cat and Dog. These two classes can inherit from the same parent class LandAnimal. By doing this, we are not only reusing an existing class but also saving time by not writing some already existing code.
class Cat extends LandAnimal{ void identity() { System.out.println("I am a Cat"); } void saySomething() { System.out.println("Meow!"); } }
class Dog extends Cat { void identity() { System.out.println("I am a Dog"); } void saySomething() { System.out.println("Bark!"); } public static void main (String gts[]) { Cat cat = new Cat(); System.out.println("Cat can walk: " + cat.canWalk); System.out.println("Cat can Fly: " + cat.canFly); cat.canEatFood(); cat.saySomething(); Dog dog = new Dog(); System.out.println("Dog can walk: " + dog.canWalk); System.out.println("Dog can Fly: " + dog.canFly); dog.canEatFood(); dog.saySomething(); } } /* Output Cat can walk: true Cat can Fly: false Yes, I can eat food! Meow! Dog can walk: true Dog can Fly: false Yes, I can eat food! Bark! */
I hope you have a better knowledge of Inheritance in Java by now, Thank you for reading.