In Java, both abstract classes and interfaces are used to define abstract types. However, there are some differences between these two concepts.
An abstract class is a class that cannot be instantiated on its own, but can be subclassed. It can have both abstract and non-abstract methods. Abstract methods are methods that are declared but not implemented in the abstract class, and must be implemented by any concrete subclass. Abstract classes can also have instance variables, constructors, and other methods with implementations. One key advantage of abstract classes is that they can provide default implementations of some methods, which can be inherited by subclasses.
On the other hand, an interface is a collection of abstract methods and constants. It cannot be instantiated on its own, and any class that implements an interface must implement all of the methods defined in the interface. Interfaces provide a way to define a contract for behavior that a class must implement, regardless of its inheritance hierarchy. Another key advantage of interfaces is that a class can implement multiple interfaces, but it can only extend one superclass.
What is an abstract class in Java?
In Java, an abstract class is a class that is declared with the abstract keyword, and it cannot be instantiated directly. The purpose of an abstract class is to provide a common interface for its subclasses, but it cannot be used on its own.
An abstract class can contain abstract and non-abstract methods. An abstract method is a method that has no implementation and is declared using the abstract keyword. A non-abstract method is a method that has an implementation and can be used as it is.
Here is an example of an abstract class in Java:
abstract class Animal {
String name;
public Animal(String name) {
this.name = name;
}
public abstract void makeSound();
public void eat() {
System.out.println(name + " is eating.");
}
}
In this example, the Animal class is an abstract class that has a constructor and two methods: makeSound()
and eat()
. The makeSound()
method is an abstract method, which means it has no implementation. The eat()
method is a non-abstract method and has an implementation.
Now let's create a subclass of the Animal class:
class Cat extends Animal {
public Cat(String name) {
super(name);
}
public void makeSound() {
System.out.println(name + " meows.");
}
}
In this example, the Cat class extends the Animal class and implements the makeSound() method, which is an abstract method in the Animal class. The Cat class can also use the non-abstract eat() method from the Animal class.
Now, we can create an instance of the Cat class and use it:
public class Main {
public static void main(String[] args) {
Cat cat = new Cat("Fluffy");
cat.makeSound();
cat.eat();
}
}
Output:
Fluffy meows.
Fluffy is eating.
In conclusion, an abstract class is a class that cannot be instantiated directly and provides a common interface for its subclasses.
What is an Interface In Java?
In Java, an interface is a collection of abstract methods (methods without a body) and constants. It is used to define a contract between two different entities, such as a class and its users, or a client and a server. An interface provides a set of methods and constants that any class implementing it must define and implement.
An interface is defined using the interface keyword in Java, followed by the interface name and the abstract methods and constants it contains. For example:
public interface Shape {
double getArea();
double getPerimeter();
String getName();
String COLOR = "red";
}
In this example, the Shape interface contains three abstract methods (getArea(), getPerimeter(), and getName()) and a constant (COLOR). Any class implementing the Shape interface must implement these methods and use the constant COLOR.
To implement an interface, a class must use the implements keyword followed by the interface name. For example:
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
public String getName() {
return "Circle";
}
}
In this example, the Circle class implements the Shape interface and provides implementations for the getArea()
, getPerimeter()
, and getName()
methods.
What are the differences between abstract classes and interfaces in Java?
Both abstract classes and interfaces are used for abstraction in Java, but there are some differences between them:
- Abstract classes can have both abstract and non-abstract methods, while interfaces can only have abstract methods. Abstract methods are methods that are declared but not implemented, while non-abstract methods have an implementation in the class.
- A class can only extend one abstract class, but it can implement multiple interfaces.
- Abstract classes can have constructors, while interfaces cannot.
- Abstract classes can have instance variables, while interfaces cannot have instance variables until Java 8, which introduced default methods that can have instance variables.
- Abstract classes can provide a default implementation for methods, while interfaces cannot. This means that any class that extends an abstract class can inherit the default implementation of a method, but any class that implements an interface must provide its own implementation.
- Abstract classes are used to create a hierarchy of related classes, while interfaces are used to define a contract that classes must adhere to.
- Abstract classes are typically used when there is a need for code reuse and when a default implementation is needed for some methods, while interfaces are typically used when there is a need to define a contract that multiple unrelated classes must adhere to.
Example- Difference between abstract and interface in Java
Let us take an example to see the difference between the abstract class and interface in Java:
public abstract class Animal {
public abstract void makeSound();
public void eat() {
System.out.println("Animal is eating.");
}
}
public interface Mammal {
public void giveBirth();
}
public class Cat extends Animal implements Mammal {
public void makeSound() {
System.out.println("Meow.");
}
public void giveBirth() {
System.out.println("Cat is giving birth.");
}
}
public class Bird implements Animal {
public void makeSound() {
System.out.println("Chirp.");
}
}
In this example, we have an abstract class Animal and an interface Mammal. The Animal class has an abstract method makeSound()
and a non-abstract method eat()
. The Mammal interface has a method giveBirth()
. The Cat class extends the Animal class and implements the Mammal interface. It provides implementations for both makeSound()
and giveBirth()
. The Bird class implements the Animal interface and provides an implementation for makeSound()
.
Note that in this example, the Animal class is used to define a hierarchy of related classes (i.e., Cat and Bird), while the Mammal interface is used to define a contract that the Cat class must adhere to.
Summary
In Java, abstract classes and interfaces are both used to define abstract types. The main difference between the two is that abstract classes can have both abstract and non-abstract methods, while interfaces can only have abstract methods. Additionally, a class can implement multiple interfaces, but it can only extend one abstract class. Abstract classes are useful when you want to provide a base implementation for a set of subclasses, while interfaces are useful when you want to define a contract for behavior that a class must implement. An abstract class can implement an interface, but an interface cannot extend an abstract class. Finally, an interface can have fields, but they must be declared as static and final. Ultimately, the choice between using an abstract class or an interface depends on the specific use case.
Further Reading