multiple inheritance using interface

Interface in Java

This post explains Interface in java.

What is Interface?
Interface is a blue print of class. Unlike class it contains only public & abstract methods and public, static & final fields.

Interface Syntax:


Difference between Interface and Class
1. Interface: Have only abstract methods
Class: Cannot have abstract method

2. Interface: Multiple Inheritance possible
Class: Multiple Inheritance not possible

3. Interface: Can not be instantiated
Class: Can be instantiated

4. Interface: Do not have constructors
Class: Have constructor

Some facts about Interface
1. Interface is use to achieve loose coupling
See Example

2. Multiple inheritance can be achieve by using interface
See Example

3. Interface provides full abstraction

4. Interface can extends multiple Interfaces

5. An interface does not contain any constructors, that is why Interface can not be instantiated

—InterfaceClass —

—MainClass —

6. JVM itself writes the public static final before the data members and public abstract before methods in Interface if programmer do not write it

For Example:

After Compilation

loose coupling

Loose coupling example using Interface

Loose coupling example using Interface

Loose coupling has advantage over tight coupling.

Lets understand it with example
In below example we have two classes A and B

Class B implements Interface i.e. InterfaceClass.

InterfaceClass defines a Contract for B class as InterfaceClass have abstract methods of B class that can be access by any other class for example A.

In Class A we have display method which can except object of class which implements InterfaceClass (in our case it is B class). And on that object method of class A is calling display() and getVar() of class B

In MainClass we have created object of Class A and B. And calling display method of A by passing object of B class i.e. objb. Display method of A will be called with object of B class.

Now talking about loose coupling. Suppose in future you have to change the name of Class B to ABC then you do not have to change its name in display method of class B, just make the object of new (ABC class) and pass it to the display method in MailClass. You do not have to change anything in Class A





loose coupling

Multiple Inheritance In Java

Multiple Inheritance in Java has always been a topic of debate among people.

Based on the Oracle’s Java tutorial ( inheritance is a concept reserved to classes only, not Interfaces, as what is inherited is the state and behavior which interfaces don’t have.

So, java does not support multiple inheritance but this questions can be asked in other sense also, like:
If the question was: Can we extend more than one class, the answer is no.
If the question was: Can we inherit state and behavior from different classes, the answer is yes with polymorphism.

There are two methods explained in this post of doing multiple inheritance in java.

1. Using interface (see Method 1).
2. Also there is another method do to it (see Method 2)

Method 1:




multiple inheritance using interface

Method 2:





multiple inheritance

Method Overloading v/s Method Overriding – Java

The capability of more than one objects to respond to the same message in a different way can be broadly categorized into two types:

1. Method Overloading
2. Method Overriding

If we have methods with the same name but having different signatures or behaviors in the same scope than the methods are known as overloaded methods and the process is known as method overloading.

a. A Method can be overloaded in the same class as well as in its sub-class.
b. Constructors can also be overloaded. In this case we call the process as Constructor overloading.

Example-1: Method Overloading –

Method having different return types but not having different signatures cannot be said as overloaded methods because it will result in error stating “signature should be different”.
void display(){
int display(){

e.g. of overloaded methods in System class are print() and println()

Now studying about method overloading in Java rises a question in our minds.
Do you know what is that question?
Read below:
Question – Can We overload main method of Java?
Answer – The simple answer is Yes. This is how we will prove it:


Method overriding means having a different implementation of the same method in the inherited or derived class. These two methods would have the same signature, but different implementation. One of these would exist in the base or the super class and other in the derived class. These methods cannot exist in the same class.

Example-2: Method Overriding –


We use @overrride annotation just before the method to denote an overriden method.


Q: What is inheritance in java?

Q: Different level of inheritance in java?

Q: How to implement inheritance in java?

Q: What is hybrid inheritance?

Q: What is multiple inheritance?

Inheritance is a mechanism by which we can inherits some existing features of another class.

Inheritance consist of 2 or more classes.

One is super class and another is subclass Super class is that class from which we inherits some features. It is also called as parent class or root class. whereas Subclass is that which inherit feature from super class. It is also called as child class Inheritance is a relationship between subclass and super class.

Consider a example of vehicles , a 4 wheeler vehicle, cars, maruti and alto. 4 wheeler vehicle is extended from vehicle then vehicle is super class whereas 4 wheeler vehicle is subclass. Similarly cars is extended from 4 wheeler vehicle, and maruti and alto is extended from cars. Vehicles is super class of all of this whereas , a 4 wheeler vehicle, cars, maruti and alto are subclass. As we all have the same ancestors similarly inheritance works.

We can inherit a class by simply writing following code: class Alto extends cars


Single Inheritance: If only one subclass inherit a super class then it is called as superclass.