Inheritance in Java.
Inheritance. Simply put, inheritance refers to using something that belongs to someone else as if it were your own. For example, you might use your father’s car as if it were yours.
In other words, consider your family name or surname. How did you get your family name or surname? From your parents, right? Therefore, you have inherited it from your family, and now you use it as your own, even though you’re not the original owner. You received your family name only because you inherited it from your family.
Let’s look at the concept of inheritance with an example.
In the above diagram, we have two classes: C1
and C2
. C1
is the parent class, and C2
is the child class. We can also say that C1
is the superclass, and C2
is the subclass.
I hope you now have a basic understanding of what inheritance is. With that understanding, let’s look at how we apply this concept in programming.
‘extends’ Keyword in Java
What is the extends
keyword? It is the keyword we use to indicate that C2
is inherited from C1
. When writing code, how do you specify that C2
is inherited from C1
? Take a look at the code snippet below, which shows how to define the C2
class.
class C2 extends C1 {
// additional code
}
However, keep this important point in mind: Java does not support multiple inheritance through classes. But we can achieve multiple inheritance through interfaces. When inheriting interfaces, you need to use the
implements
keyword, not theextends
keyword. However, in this blog post, I will not discuss multiple inheritance and interfaces; those topics will be covered in upcoming posts.
To explain these concepts of inheritance, let’s move on to an example.
Example of Vehicle and Van Classes
As the first step, I created two classes called Vehicle
and Van
, intending for Vehicle
to be my superclass and Van
to be my subclass.
In the Vehicle
class, I define two methods: drive
and horn
. In the Van
class, I define two attributes: brand
and model
.
With the knowledge you have so far, you should know that since I have created two methods in the Vehicle
class, I should be able to call those methods from the main class using an object. Therefore, I create a Vehicle
type object called v1
in the main class and call the two methods I created in the Vehicle
class. Check the code below (Code-01).
// Code-0
class Van {
String brand = "Toyota";
String model = "Hiace";
}
class Vehicle {
public void drive(){
System.out.println("I am driving");
}
public void horn(){
System.out.println("I am honking");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v1 = new Vehicle();
v1.drive();
v1.horn();
}
}
If you run Code-01, the output will be.
I am driving
I am honking
Therefore, the above code has no errors and works perfectly.
Now, let’s try something different. What I am trying to do is create another Van
type object called v2
in the main class and try to call the drive
and horn
methods, which were created in the Vehicle
class. Can I do this? No, I cannot call those two methods.
This is where inheritance comes into the picture.
As I mentioned earlier, when I created the classes, I planned for Vehicle
to be the superclass and Van
to be the subclass. Therefore, if Van
is the subclass, I should be able to call the drive
and horn
methods from the Vehicle
class using inheritance. Now I hope you understand why the concept of inheritance exists.
Implementing Inheritance
How can we inherit the Van
class from the Vehicle
class?
By using the extends
keyword, I inherit the Van
class from the Vehicle
class. Then, after creating the Van
type v2
object in the main class, I should be able to call the drive
and horn
methods, as Van
and Vehicle
classes are now linked through inheritance. Check the code below.
// Code-02
// Subclass Van extends the Vehicle class
class Van extends Vehicle {
// Attributes specific to the Van class
String brand = "Toyota";
String model = "Hiace";
}
// Superclass Vehicle
class Vehicle {
// Method to simulate driving
public void drive(){
System.out.println("I am driving");
}
// Method to simulate honking
public void horn(){
System.out.println("I am honking");
}
}
public class Main {
public static void main(String[] args) {
// Creating an object of the Vehicle class
Vehicle v1 = new Vehicle();
// Creating an object of the Van class
Van v2 = new Van();
// Call drive and horn methods using v1 object (Vehicle object)
System.out.println("Call drive and horn method using v1 object");
v1.drive();
v1.horn();
// Call drive and horn methods using v2 object (Van object)
System.out.println("Call drive and horn method using v2 object");
v2.horn();
v2.drive();
}
}
Outcome of Code-02:
Call drive and horn method using v1 object
I am driving
I am honking
Call drive and horn method using v2 object
I am honking
I am driving
Restricting a Superclass
Now that you understand inheritance and how to work with it, let me ask you a question. Can you restrict a class from being a superclass? Yes, you can. If you use the final
keyword with a class, that class cannot be a superclass.
Check the code below, where I have applied the final
keyword to the Vehicle
class. This makes it impossible for the Vehicle
class to be a superclass. When I try to extend this class, I get an error.
Multilevel Inheritance
I hope you’re clear with all the points discussed so far. So let’s go one step further. Now, I am going to create another class called Car
and inherit the Car
class from the Van
class.
In other words, the Vehicle
class has two methods (drive
and horn
), and the Van
class has two attributes (brand
and model
). The Van
class is inherited from the Vehicle
class, so it has its own two attributes and the two methods inherited from Vehicle
. If I inherit the Car
class from the Van
class, the Car
class should also have the two methods (drive
and horn
) defined in the Vehicle
class and the two attributes declared in the Van
class. Let’s take a look.
// Code-04
// Class Car inherits from Van
class Car extends Van {
// No additional attributes or methods are defined for Car
// Car will inherit all the properties and behaviors of Van
}
// Class Van inherits from Vehicle
class Van extends Vehicle {
// Attributes specific to Van
String brand = "Toyota";
String model = "Hiace";
// Van inherits the methods drive() and horn() from Vehicle
// but doesn't define any additional methods or override them
}
// Base class Vehicle defines common behaviors
class Vehicle {
// Method to simulate driving behavior
public void drive() {
System.out.println("I am driving");
}
// Method to simulate honking behavior
public void horn() {
System.out.println("I am honking");
}
}
// Main class containing the entry point of the program
public class Main {
public static void main(String[] args) {
// Creating an instance of Car
Car car1 = new Car();
// Calling the drive() method inherited from the Vehicle class
car1.drive();
// Calling the horn() method inherited from the Vehicle class
car1.horn();
// Note: car1 can also access the attributes of Van if needed
}
}
Outcome of Code-04:
I am driving
I am honking
Happy cording.