Interface in JAVA.
I will suggest you to read Inheritance in JAVA and Abstract keyword in java before reading this post.In general,Interface is a point where two systems/things/materials will connect. We are using
a phone, the charger will connect to the phone via charging port , that is an interface . That USB/Charging port is interface to the external world. Phone can connect to other devices using that interface.
I will suggest you to read Inheritance in JAVA and Abstract keyword in java before reading this post.In general,Interface is a point where two systems/things/materials will connect. We are using
a phone, the charger will connect to the phone via charging port , that is an interface . That USB/Charging port is interface to the external world. Phone can connect to other devices using that interface.
In Real time world, consider Animal , it is a group and animal doesn't exists real time .Gangaroo and rabbit that exists and belongs to the group Animal.These animals run and makes sound. In the Java world, animal is an interface.Gangaroo and Rabbit are classes . Run and makesSound are unimplemented methods in Animal Interface.
In Java
Animal animal = new Gangaroo();
Here animal is the reference for the Gangaroo object created. We can access the unimplemented methods using the interface Animal. (How phone connects using USB).Hence Animal is interface used to connect to the methods Run and makesSound.
The methods will be implemented in the classes that implements this interface.
Public class Gangaroo implements Animal{
}
Multiple Inheritance in java
In java , If there are many classes present and all classes has to implement the same method , but the logic should be different in different classes.i.e. method functionality is different.
In that scenario, we can define an interface with that method declaration and all classes should implement that interface and define the functionality as per their logic.
Let us consider the Loan class that we have discussed in Abstract example.
Parent class: Loan
Child classes: EducationalLoan and HousingLoan
Interface : InterestRate
In this case, both the methods of EducationalLoan and HousingLoan should be same . i.e. calculateInterest method. But the functionality should be different.In that case because interest rate varies based on the loan.
interface InterestRate{
public double calculateInterest();
}
public class EducationalLoan extends Loan implements InterestRate{
public double calculateInterest(){
//logic to calculate interest based on educational loan interest rate .i.e.14%
}
}
public class HousingLoan extends Loan implements InterestRate {
public double calculateInterest() {
//logic to calculate interest based on Housing loan interest rate .i.e.10%
}
}
I hope the above example will give you an idea on interface and multiple Inheritance.
Now going for multiple inheritance. In java multiple inheritance is not supported i.e. A class cannot extend more than one class.
If a class want to implement the methods in two parent classes, that cannot be achieved by means of java. That can be achieved by using interface.We have discussed that Educational loan and Housing loan classes are subclasses of loan and it implements InterestRate Interface for the implementing the calculate interest method.
In the above example, multiple inheritance is achieved by means of Interface.
Loan Loan InterestRate
| | |
| | |
------------- -------------------------------
| | |
| | |
-------------------------------
Educational Housing | |
Loan Loan Educational Housing
Loan Loan
Multiple Inheritance
Class EducationalLoan extends Loan implements InterestRate{
}
Interface can be achieved by using the keyword implements.
Let us consider the Loan class that we have discussed in Abstract example.
Parent class: Loan
Child classes: EducationalLoan and HousingLoan
Interface : InterestRate
In this case, both the methods of EducationalLoan and HousingLoan should be same . i.e. calculateInterest method. But the functionality should be different.In that case because interest rate varies based on the loan.
interface InterestRate{
public double calculateInterest();
}
public class EducationalLoan extends Loan implements InterestRate{
public double calculateInterest(){
//logic to calculate interest based on educational loan interest rate .i.e.14%
}
}
public class HousingLoan extends Loan implements InterestRate {
public double calculateInterest() {
//logic to calculate interest based on Housing loan interest rate .i.e.10%
}
}
I hope the above example will give you an idea on interface and multiple Inheritance.
Now going for multiple inheritance. In java multiple inheritance is not supported i.e. A class cannot extend more than one class.
If a class want to implement the methods in two parent classes, that cannot be achieved by means of java. That can be achieved by using interface.We have discussed that Educational loan and Housing loan classes are subclasses of loan and it implements InterestRate Interface for the implementing the calculate interest method.
In the above example, multiple inheritance is achieved by means of Interface.
Loan Loan InterestRate
| | |
| | |
------------- -------------------------------
| | |
| | |
-------------------------------
Educational Housing | |
Loan Loan Educational Housing
Loan Loan
Multiple Inheritance
Class EducationalLoan extends Loan implements InterestRate{
}
Interface can be achieved by using the keyword implements.
- All the methods in the interface are public and abstract by default even if the access modifier/abstract keyword is not mentioned.
- class implementing the interface will define all the methods declared in the interface else the class should be marked as abstract.
- The variables defined in the interface are public,final and static.
- Variables of an interface are public static final be default due to the following reasons:public: every class using the interface must be able to access the variables of the interfacestatic: an object for an interface cannot be created, hence every variable of the interface belongs to the interface itselffinal: if variables are not maintained as final, it will not be possible to identify its value at point of time since every class which is using the interface can change its value
- An interface cannot be instantiated like classes. i.e Objects cannot be created for interfaces
- Interface doesnt contain private or protected methods ----- As it should be implemented by other classes which may be in other package.if it is marked with private or protected it cannot be implemented
Sign up here with your email
ConversionConversion EmoticonEmoticon