Inheritance in Java Example

Inheritance in java is one of the core concepts of Object Oriented Programming. Inheritance is used when we have is-a relationship between objects. Inheritance in Java is implemented using extends keyword.

Inheritance in Java

Inheritance in java is the method to create a hierarchy between classes by inheriting from other classes.

Inheritance in java is transitive – so if Sedan extends Car and Car extends Vehicle, then Sedan is also inherited from Vehicle class. Vehicle becomes the superclass of both Car and Sedan.

Inheritance is widely used in java applications, for example extending Exception class to create an application specific Exception class that contains more information like error codes. For example NullPointerException.

Java Inheritance Example

In java, every class implicitly extends java.lang.Object class, so Object class is at the top level of inheritance hierarchy in java.

Java Inheritance Program Output

As you can see that Cat class doesn’t have getEats() method but still it works because it’s inherited from Animal class.

Important Points for Inheritance in Java

Most important benefit of inheritance is code reuse because subclasses inherits the variables and methods of superclass.

Private members of superclass are not directly accessible to subclass. As in this example, Animal variable noOfLegs is not accessible to Cat class but it can be indirectly accessible via getter and setter methods.

Superclass members with default access is accessible to subclass ONLY if they are in same package.

Superclass constructors are not inherited by subclass.

If superclass doesn’t have default constructor, then subclass also needs to have an explicit constructor defined. Else it will throw compile time exception. In the subclass constructor, call to superclass constructor is mandatory in this case and it should be the first statement in the subclass constructor.

Java doesn’t support multiple inheritance, a subclass can extends only one class. So here Animal is implicitly extending Object class and Cat is extending Animal class but due to java inheritance transitive nature, Cat class also extends Object class.

We can create an instance of subclass and then assign it to superclass variable, this is called upcasting. Below is a simple example of upcasting:

Cat c = new Cat(); //subclass instance
Animal a = c; //upcasting, it's fine since Cat is also an Animal

When an instance of Superclass is assigned to a Subclass variable, then it’s called downcasting. We need to explicitly cast this to Subclass. For example;

We can override the method of Superclass in the Subclass. However we should always annotate overridden method with @Override annotation so that Compiler knows that we are overriding a method and if something changes in the superclass method, we get to know at compile time rather than getting unwanted results at the runtime.

We can call the superclass methods and access superclass variables using super keyword. It comes handy when we have a same name variable/method in the subclass but we want to access the superclass variable/method. This is also used when Constructors are defined in the superclass and subclass and we have to explicitly call the superclass constructor.

We can use instanceof instruction to check the inheritance between objects, let’s see this with below example.

If you are not going to use Superclass in the code i.e your Superclass is just a base to keep reusable code then you can keep them as Abstract class to avoid unnecessary instantiation by client classes. It will also restrict the instance creation of base class.

Java Inheritance Videos

I have recently published two videos in YouTube explaining Inheritance in detail with sample programs, you should watch them below.

About Pankaj

If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Google Plus, Facebook or Twitter. I would love to hear your thoughts and opinions on my articles directly.

Recently I started creating video tutorials too, so do check out my videos on Youtube.

Comments

“If superclass doesn’t have default constructor, then subclass also needs to have an explicit constructor defined. Else it will throw compile time exception. In the subclass constructor, call to superclass constructor is mandatory in this case and it should be the first statement in the subclass constructor.”

As per my understanding ,Java compiler inserts a default constructor into the code during compilation and exists in .class file . Could you please explain me how can we create a super class without a default constructor.

You can always create a class without default constructor and then extend it. In that scenario you will have to explicitly write a constructor in child class. This is common if you are extending a class from third party jars and you have no control on them.

Thank you Pankaj. got it
So if a class contain No Arg and Parameterised Constructor then Default constructor is removed. In this case there is no default constructor.In that case we have to explicitly write a constructor in Child class.