In this post we will learn difference between Abstract Class and Interface in Java with examples.
Each of the above difference between Abstract class vs Interface is explained with an example below –
Abstract Class vs Interface in Java
Introduction
In Java, both abstract classes and interfaces are used to achieve abstraction, which allows you to define methods that must be implemented by derived classes or classes that implement the interface. However, they serve different purposes and have distinct characteristics.
Abstract Class
What is an Abstract Class?
An abstract class is a class that cannot be instantiated on its own and is meant to be subclassed. It can have both abstract methods (methods without a body) and concrete methods (methods with a body).
Key Features of Abstract Class
- Can contain both abstract and concrete methods.
- Can have instance variables and constructors.
- Can provide a common base with shared code for subclasses.
- Can extend only one class.
Syntax
abstract class AbstractClassName {
// Abstract method
abstract void abstractMethod();
// Concrete method
void concreteMethod() {
// implementation
}
}
Example: Abstract Class
This example demonstrates an abstract class with both abstract and concrete methods.
abstract class Animal {
// Abstract method
abstract void makeSound();
// Concrete method
void sleep() {
System.out.println("This animal is sleeping.");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Woof Woof");
}
}
public class AbstractClassExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // Output: Woof Woof
dog.sleep(); // Output: This animal is sleeping.
}
}
Explanation
Animal
is an abstract class with an abstract methodmakeSound()
and a concrete methodsleep()
.Dog
is a subclass that provides an implementation for themakeSound()
method.- The
sleep()
method is inherited and can be used as is.
Interface
What is an Interface?
An interface in Java is a reference type, similar to a class, that can contain only abstract methods (until Java 8). From Java 8 onwards, interfaces can have default and static methods as well. Interfaces cannot have instance variables.
Key Features of Interface
- All methods are abstract by default (until Java 8).
- Can contain constants, default methods, and static methods (from Java 8 onwards).
- Cannot have instance variables or constructors.
- A class can implement multiple interfaces.
Syntax
interface InterfaceName {
// Abstract method
void abstractMethod();
// Default method (Java 8 and above)
default void defaultMethod() {
// implementation
}
// Static method (Java 8 and above)
static void staticMethod() {
// implementation
}
}
Example: Interface
This example demonstrates an interface with an abstract method, a default method, and a static method.
interface Animal {
// Abstract method
void makeSound();
// Default method
default void sleep() {
System.out.println("This animal is sleeping.");
}
// Static method
static void staticMethod() {
System.out.println("This is a static method in an interface.");
}
}
class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("Woof Woof");
}
}
public class InterfaceExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // Output: Woof Woof
dog.sleep(); // Output: This animal is sleeping.
Animal.staticMethod(); // Output: This is a static method in an interface.
}
}
Explanation
Animal
is an interface with an abstract methodmakeSound()
, a default methodsleep()
, and a static methodstaticMethod()
.Dog
implements theAnimal
interface and provides an implementation for themakeSound()
method.- The
sleep()
method is inherited from the interface and can be used as is. - The
staticMethod()
is called on the interface itself.
Abstract Class vs Interface: Key Differences
Abstract Class | Interface | |
1 | abstract class can extend only one class or one abstract class at a time | interface can extend any number of interfaces at a time |
2 | abstract class can extend from a class or from an abstract class | interface can extend only from an interface |
3 | abstract class can have both abstract and concrete methods | interface can have only abstract methods |
4 | A class can extend only one abstract class | A class can implement any number of interfaces |
5 | In abstract class keyword ‘abstract’ is mandatory to declare a method as an abstract | In an interface keyword ‘abstract’ is optional to declare a method as an abstract |
6 | abstract class can have protected , public and public abstract methods | Interface can have only public abstract methods i.e. by default |
7 | abstract class can have static, final or static final variable with any access specifier | interface can have only static final (constant) variable i.e. by default |
- Methods
- Abstract Class: Can have both abstract and concrete methods.
- Interface: Can have abstract methods, default methods, and static methods (Java 8 and above).
- Variables
- Abstract Class: Can have instance variables.
- Interface: Can have constants (public, static, and final) but no instance variables.
- Constructors
- Abstract Class: Can have constructors.
- Interface: Cannot have constructors.
- Inheritance
- Abstract Class: A class can extend only one abstract class.
- Interface: A class can implement multiple interfaces.
- Use Case
- Abstract Class: Used when classes share a common base and some implementation code.
- Interface: Used to define a contract that multiple classes can implement, ensuring they follow the same set of methods.
Combining Abstract Class and Interface
In some scenarios, you may need to use both abstract classes and interfaces to leverage the advantages of both.
Example: Combining Abstract Class and Interface
interface Animal {
void makeSound();
}
abstract class Mammal implements Animal {
// Abstract method specific to mammals
abstract void walk();
// Concrete method
void breathe() {
System.out.println("Breathing...");
}
}
class Dog extends Mammal {
@Override
public void makeSound() {
System.out.println("Woof Woof");
}
@Override
void walk() {
System.out.println("Dog is walking.");
}
}
public class CombinedExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // Output: Woof Woof
dog.walk(); // Output: Dog is walking.
dog.breathe(); // Output: Breathing...
}
}
Explanation
Animal
is an interface with the methodmakeSound()
.Mammal
is an abstract class that implements theAnimal
interface, adding an abstract methodwalk()
and a concrete methodbreathe()
.Dog
extendsMammal
and provides implementations formakeSound()
andwalk()
.
Conclusion
Abstract classes and interfaces are fundamental concepts in Java that provide different ways to achieve abstraction. Understanding their differences and how to use them appropriately can help you design flexible and maintainable code. Practice using abstract classes and interfaces in various scenarios to gain a deeper understanding of their applications.