Inheritance and Interfaces in Java

Listen to this article

We will discuss two important concepts in Java – inheritance and interfaces.

‘Inheritance’:

Inheritance as the name suggests is inheriting the class functionality of a super class. This can be done as follows:

public class Cat extends Animal{
}

This can be illustrated further in the above picture. Here, ‘Cat’ is the sub-class and ‘Animal’ is the super class.

This ‘extends’ keyword is how inheritance is implemented in Java. Here, the ‘Cat’ class is extending from ‘Animal’ class to get all the additional functionality of the Animal class without having to retype it again.

‘Cat’ class gets access to all the methods and variables of its super class.

Inheritance is one of the core points of ‘Object Oriented programming’.

Java only supports ‘single inheritance’. Which means that we can only extend from only one class for a particular base class.

Interfaces:

We know that Java only supports single inheritance. That is a class can extend only from a single class and inherit its functionality. What if a single class needs the functionality of multiple classes? How can we implement it? It can be done by means of ‘interfaces’.

‘Interfaces’ in Java are similar to classes but not particularly the same. In earlier versions of Java, interfaces only contained empty methods, constants, static methods and default methods. As of Java 9, interfaces can also contain

  • constant variables
  • abstract methods
  • private methods
  • static methods
  • private static methods

The classes that implement the interfaces will ensure code re-usability and encapsulation. ‘Encapsulation’ is another Object oriented concept that ensures only required methods are exposed to users.

As an example,

public class Cat extends Animal implements Running{
}

In the above example, ‘Running’ is the interface and it is used in the class by the ‘implements’ keyword.

These are some salient point of interfaces:

Interfaces contain the skeletal system of a class. It can contain constant variables, abstract methods, private methods, static methods, private static methods

They improve code reusability and encapsulation

Interfaces cannot be instantiated

Interfaces can be public or default access

More Java? 🙂 Yes, come on by tomorrow….:)

I’m participating in #BlogchatterA2Z by Blogchatter 

(Visited 131 times, 1 visits today)

Related Posts

6 thoughts on “Inheritance and Interfaces in Java

  1. I learned and teached Java long time back. Sadly, I disposed of the print books. Now I am feeling sad. Even though online stuff is available, nothing can replace print books and materials. However, I still preserve my notes, which is precious.

    1. I have all my books safe with me..even though it has been 22 years!! 🙂 Thanks for reading !! 🙂

  2. Ah the classic animal and cat example that we studied in college. One of most important concepts when it comes to any object oriented programming language.

    1. Yes…this is the example that came to my mind right away… 🙂 and interfaces is a wonderful concept!

Leave a Reply