Interfaces and inheritance in Java Programming



Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order. The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

Types of Inheritance

There are various types of inheritance as demonstrated below.

Types of Inheritance

A very important fact to remember is that Java does not support multiple inheritance. This means that a class cannot extend more than one class. Therefore following is illegal −

Example

public class extends Animal, Mammal{} 

However, a class can implement one or more interfaces, which has helped Java get rid of the impossibility of multiple inheritance. And an interface can extends one or more interfaces. The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.

For example, if the Hockey interface extended both Sports and Event, it would be declared as -

Example

public interface Hockey extends Sports, Event

Advertisements