What is an Interface?
An Interface is a reference type in Java (Note: we will discuss more about primitive and reference type in the upcoming blogs). An Interface is blueprint to a class and it contains collections of static variables and abstract methods. An interface will never have concrete methods.
In other words, when we talk about interface we are providing the contract what can the object do.
Interface is defined with the interface keyword, below is the simple example how an interface is defined.
if you observe here
I didn't give any modifiers and abstract keyword, but in an interface all the defined methods are by default abstract and public and the constants used are static, as like abstract classes even interfaces cannot be instantiated.
Since it is said that interface provides the capabilities what an Object can do, We will do a simple POC on this. We will take a real time example of an ATM machine.
Lets create an interface ATMInterface.java
we use implements keyword to inherit an interface to a class.
Now the class which is implementing the ATM interface, should by default implement all the defined methods in the interface,
We can see the ATMClientClass.java when implemented all the abstract methods of the interface should be implemented in the class.
We cannot have a multiple inheritance between classes but we can achieve multiple inheritance using interfaces, a class can implement one or more interfaces.
Will do a simple POC how it is done, lets take two interface which has two abstract methods each
Inteface1.java
Interface2.java
Now lets implements these two interfaces, to a class MultipleInterfaceClass.java
Now here you can see the class now should implement all the abstract methods of both the interfaces.
Can we implement an interface to an Abstract class?
Yes we can implement but the abstract methods in the interface for sure to be implemented in the abstract class.
An Interface is a reference type in Java (Note: we will discuss more about primitive and reference type in the upcoming blogs). An Interface is blueprint to a class and it contains collections of static variables and abstract methods. An interface will never have concrete methods.
In other words, when we talk about interface we are providing the contract what can the object do.
Interface is defined with the interface keyword, below is the simple example how an interface is defined.
package com.learning.interfaces;
public interface SampleInterface {
void getMethod1();
int getMethod2();
}
if you observe here
void getMethod1();
int getMethod2();
I didn't give any modifiers and abstract keyword, but in an interface all the defined methods are by default abstract and public and the constants used are static, as like abstract classes even interfaces cannot be instantiated.
Since it is said that interface provides the capabilities what an Object can do, We will do a simple POC on this. We will take a real time example of an ATM machine.
Lets create an interface ATMInterface.java
package com.learning.interfaces;
public interface ATMInterface {
String getBalance(String username, String Passwords);
void updateBalance(String username, String Passwords);
void moneyDeposit(String username, String Passwords);
void changeLanguage(String username, String Passwords);
void changePin(String username, String Passwords);
}
we use implements keyword to inherit an interface to a class.
Now the class which is implementing the ATM interface, should by default implement all the defined methods in the interface,
package com.learning.interfaces;
public class ATMClientClass implements ATMInterface {
@Override
public String getBalance(String username, String Passwords) {
// TODO Auto-generated method stub
return null;
}
@Override
public void updateBalance(String username, String Passwords) {
// TODO Auto-generated method stub
}
@Override
public void moneyDeposit(String username, String Passwords) {
// TODO Auto-generated method stub
}
@Override
public void changeLanguage(String username, String Passwords) {
// TODO Auto-generated method stub
}
@Override
public void changePin(String username, String Passwords) {
// TODO Auto-generated method stub
}
}
We can see the ATMClientClass.java when implemented all the abstract methods of the interface should be implemented in the class.
We cannot have a multiple inheritance between classes but we can achieve multiple inheritance using interfaces, a class can implement one or more interfaces.
Will do a simple POC how it is done, lets take two interface which has two abstract methods each
Inteface1.java
package com.learning.interfaces;
public interface Interface1 {
void getMethod1OfInterface1();
void getMethod2OfInterface1();
}
Interface2.java
package com.learning.interfaces;
public interface Interface2 {
void getMethod1OfInterface2();
void getMethod2OfInterface2();
}
Now lets implements these two interfaces, to a class MultipleInterfaceClass.java
package com.learning.interfaces;
public class MultipleInterfaceClass implements Interface1,Interface2{
@Override
public void getMethod1OfInterface2() {
}
@Override
public void getMethod2OfInterface2() {
}
@Override
public void getMethod1OfInterface1() {
}
@Override
public void getMethod2OfInterface1() {
}
}
Now here you can see the class now should implement all the abstract methods of both the interfaces.
Can we implement an interface to an Abstract class?
Yes we can implement but the abstract methods in the interface for sure to be implemented in the abstract class.
package com.learning.interfaces;
public abstract class SampleAbstractClass implements Interface1 {
@Override
public void getMethod1OfInterface1() {
}
@Override
public void getMethod2OfInterface1() {
}
public abstract void getSampleAbstractClass();
}
EmoticonEmoticon