Java is a general-purpose programming language that is a concurrent, class-based, and object-oriented language. Java follows the concept of “write once and run anywhere (WORA).” This means that compiled Java code can be run on all different platforms that support Java. There’s no need for recompilation.
Abstract Class and Interface are used to achieve Abstraction where abstract methods are declared. We can’t instantiate Abstract Class and Interface.
Abstract Class
Interface
Abstract Class can have both abstract method and non-abstract methods.
Interface can have only abstract methods.
Abstract Class does not support multiple Inheritance.
Interface supports multiple Inheritance.
Abstract Class can be implemented by using keyword “extends”.
Interface can be implemented by using keyword “implements”.
Abstract class can provide the implementation of interface.
Interface can’t provide the implementation of abstract class.
Abstract Class can have static, non-static, final & non-final variables.
Interface can have only final & static variables.
Abstract Class can have class members as private, protected, etc.
Interface can have class members as public by default.
Example: public abstract class Shape{ public abstract void draw(); }
Example: public interface Drawable{ void draw(); }
An abstract class can extend another Java class and implement multiple Java interfaces.
An interface can extend another Java interface only.
Abstract keyword is used to declared abstract class
Interface keyword is used to declare interface.
Below is an example of Abstract Class. In the below example, there is an abstract class with abstract method. This abstract method is implemented in SubClass.
//Abstract Class
public abstract class Bank {
// Abstract method
abstract void getRateOfInterest();
}
SubClass
//Subclass (inherit from Bank)
public class SBI extends Bank {
// The body of getRateOfInterest is declared here
public void getRateOfInterest() {
System.out.println("Interest Rate of SBI Bank :" + 5.3);
}
}
Test Class
public class AbstractionTest {
public static void main(String[] args) {
Bank bank1 = new SBI();
bank1.getRateOfInterest();
}
}
Below is an example of Interface. In the below example, Interface has two abstract methods and the implementation of abstract methods are declared in Test Class.
public interface MyInterface {
// Abstract methods
public void method1();
public void method2();
}
Test Class
public class InterfaceTest implements MyInterface {
public void method1() {
System.out.println("Implementation of Method 1");
}
public void method2() {
System.out.println("Implementation of Method 2");
}
public static void main(String[] args) {
MyInterface interTest = new InterfaceTest();
interTest.method1();
interTest.method2();
}
}
In this tutorial, I’ll explain about Interfaces in Java.
What is Interface?
An interface is a group of related methods with empty bodies (abstract methods).
All the methods in Interface are public and abstract.
A class can implement more than one interface.
An interface can extends another interface or interfaces
A class that implements interface must implements all the methods in interface. To implement interface use implements keyword.
The variables declared in an interface are public, static & final by default.
Java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance .
Syntax of Interface
interface test {
//declare methods which are abstract by default
//declare variables which are public, static, final by default
}
Relationship between Class and Interfaces
Below is an example of implementation of Interface. I have declared a class as Interface which have two abstract methods.
public interface MyInterface {
// Abstract methods
public void method1();
public void method2();
}
Interface Implementation
public class InterfaceTest implements MyInterface {
public void method1() {
System.out.println("Implementation of Method 1");
}
public void method2() {
System.out.println("Implementation of Method 2");
}
public static void main(String[] args) {
MyInterface interTest = new InterfaceTest();
interTest.method1();
interTest.method2();
}
}
Output
Implementation of Method 1
Implementation of Method 2
Multiple Interface
In the below example, Interface 2 extends Interface 1 and class implements Interface 2.
Interface 1
public interface Infa1 {
public void method1();
}
Interface 2
public interface Infa2 extends Infa1 {
public void method2();
}
MultipleInterfaceTest Class
public class MultipleInterfaceTest implements Infa2 {
public void method1() {
System.out.println("Implementation of Method 1");
}
public void method2() {
System.out.println("Implementation of Method 2");
}
public static void main(String[] args) {
Infa2 obj = new MultipleInterfaceTest();
obj.method1();
obj.method2();
}
}
Output
Implementation of Method 1
Implementation of Method 2
Multiple Inheritance
Interface supports multiple Inheritance. Here, there are two interfaces which are implemented in class MultipleInheritanceTest.
Interface 1
public interface Infa1 {
public void method();
}
Interface 2
public interface Infa2 {
public void method();
}
MultipleInheritanceTest Class
public class MultipleInheritanceTest implements Infa1, Infa2 {
public void method() {
System.out.println("Implementation of Method ");
}
public static void main(String[] args) {
MultipleInheritanceTest test = new MultipleInheritanceTest();
test.method();
}
}