The interface is a blueprint that can be used to implement a class. The interface does not contain any concrete methods (methods that have code). All the methods of an interface are abstract methods.
An interface cannot be instantiated. However, classes that implement interfaces can be instantiated. Interfaces never contain instance variables but, they can contain public static final variables (i.e., constant class variables)
What Is Abstract Class?
A class which has the abstract keyword in its declaration is called abstract class. Abstract classes should have at least one abstract method. , i.e., methods without a body. It can have multiple concrete methods.
Abstract classes allow you to create blueprints for concrete classes. But the inheriting class should implement the abstract method.
Abstract classes cannot be instantiated.
Important Reasons For Using Interfaces
Interfaces are used to achieve abstraction.
Designed to support dynamic method resolution at run time
It helps you to achieve loose coupling.
Allows you to separate the definition of a method from the inheritance hierarchy
Important Reasons For Using Abstract Class
Abstract classes offer default functionality for the subclasses.
Provides a template for future specific classes
Helps you to define a common interface for its subclasses
Abstract class allows code reusability.
Interface Vs. Abstract Class
An abstract class permits you to make functionality that subclasses can implement or override whereas an interface only permits you to state functionality but not to implement it. A class can extend only one abstract class while a class can implement multiple interfaces.
//Interface Syntax
interface name{
//methods
}
//Java Interface Example:
interface Pet {
public void test();
}
class Dog implements Pet {
public void test() {
System.out.println("Interface Method Implemented");
}
public static void main(String args[]) {
Pet p = new Dog();
p.test();
}
}
//Abstract Class Syntax
abstract class name{
// code
}
//Abstract class example:
abstract class Shape {
int b = 20;
abstract public void calculateArea();
}
public class Rectangle extends Shape {
public static void main(String args[]) {
Rectangle obj = new Rectangle();
obj.b = 200;
obj.calculateArea();
}
public void calculateArea() {
System.out.println("Area is " + (obj.b * obj.b));
}
}
What is Interface?
The interface is a blueprint that can be used to implement a class. The interface does not contain any concrete methods (methods that have code). All the methods of an interface are abstract methods.
An interface cannot be instantiated. However, classes that implement interfaces can be instantiated. Interfaces never contain instance variables but, they can contain public static final variables (i.e., constant class variables)
What Is Abstract Class?
A class which has the abstract keyword in its declaration is called abstract class. Abstract classes should have at least one abstract method. , i.e., methods without a body. It can have multiple concrete methods.
Abstract classes allow you to create blueprints for concrete classes. But the inheriting class should implement the abstract method.
Abstract classes cannot be instantiated.
Important Reasons For Using Interfaces
Abstract classes offer default functionality for the subclasses.
Interface Vs. Abstract Class
An abstract class permits you to make functionality that subclasses can implement or override whereas an interface only permits you to state functionality but not to implement it. A class can extend only one abstract class while a class can implement multiple interfaces.