Hi in this article, we will learn inheritance in Java with examples. Inheritance in programming simply means the classes which are extending the parent class can use/reach the parent class’s methods and variables if they are not declared as private access.
It is always nice to explain the theory with some examples to make things more tangible. Let’s do it. Let’s say, we have three nice cars, are Tesla, Mercedes, and Porsche. Inheritance has an IS-A relationship. Tesla IS-A Car, Mercedes IS-A Car, Porsche IS-A Car, right? Yes! :) Then, that means Car is a common thing for all of them because they are all cars. So, we can have a base class or in other terms parent class as Car.
In parent class, we can define variables and methods and if they are defined as protected or public, all child classes can reach and use them.
Also, parent classes can modify these methods by using @override annotation. In this way, they can change the behavior of these methods and it is called polymorphism.
Inheritance in Java with Examples
So, let’s implement our Car class and let say that all cars can speed up and consume energy and they have a name. Below, I also used @Getter and @Setter Lombok annotation, rather than writing these boring get and set methods. I also suggest using Lombok Library in your Java projects.
@Getter @Setter public abstract class Car { private String name = null; public void speedUp() { System.out.println("I make 100 meters in 6 seconds!"); } public void consumeEnergy() { System.out.println("I consume oil energy!"); } }
Now, let’s continue with our child classes. These are Mercedes, Tesla, and Porsche.
Mercedes use all methods of the Car class without any change and it is so simple.
public class Mercedes extends Car { }
Porsche’s speedUp method is unique, so we will change the speedup() method’s behavior by using @override annotation. It is called polymorphism.
public class Porsche extends Car { @Override public void speedUp() { System.out.println("I make 100 meters in 4 seconds!"); } }
Tesla speedUp faster and also consumes electric energy. We should also change these behaviors for Tesla.
public class Tesla extends Car { @Override public void speedUp() { System.out.println("I make 100 meters in 3 seconds!"); } @Override public void consumeEnergy() { System.out.println("I consume electric energy!"); } }
And now, it is time to write some tests to see their behavior.
public class CarTest { Tesla tesla = new Tesla(); //You can also declare like this: Car tesla = new Tesla(); Porsche porsche = new Porsche(); //You can also declare like this: Car porsche = new Porsche() Mercedes mercedes = new Mercedes(); //You can also declare like this: Car mercedes = new Mercedes(); @Test public void cartTest() { tesla.setName("Tesla"); System.out.println("\nTesting " + tesla.getName() + "!\n-------------------"); tesla.speedUp(); tesla.consumeEnergy(); porsche.setName("Porsche"); System.out.println("\nTesting " + porsche.getName() + "!\n-------------------"); porsche.speedUp(); porsche.consumeEnergy(); mercedes.setName("Mercedes"); System.out.println("\nTesting " + mercedes.getName() + "!\n-------------------"); mercedes.speedUp(); mercedes.consumeEnergy(); } }
And here is the output.
GitHub Project
Thanks for reading,
Onur Baskirt

Onur Baskirt is a Software Engineering Leader with international experience in world-class companies. Now, he is a Software Engineering Lead at Emirates Airlines in Dubai.