Abstract Class and Interface
An abstract class and an interface are both fundamental concepts in object-oriented programming that allow for the definition of common behaviors and contracts for classes. They are used to achieve abstraction, encapsulation, and polymorphism in programming languages like Java, C#, and C++.
Let’s explore each concept in more detail
Abstract Class
- An abstract class is a class that cannot be instantiated directly. It serves as a blueprint for derived classes and provides common behavior and characteristics.
- It can contain both abstract and non-abstract methods.
- An abstract method is a method without an implementation, meaning it does not provide any code. It only declares the method signature.
- Any class that extends an abstract class must provide an implementation for all the abstract methods inherited from the abstract class.
- Abstract classes can have constructors, member variables, and concrete methods (methods with an implementation).
- Abstract classes are useful when you want to provide a common interface for a group of related classes and enforce certain behaviors.
Example in Java:
abstract class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public abstract void makeSound(); // Abstract method
public void eat() { // Concrete method
System.out.println(name + " is eating.");
}
}
class Dog extends Animal {
public Dog(String name) {
super(name);
}
public void makeSound() { // Implementation of the abstract method
System.out.println("Woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
dog.makeSound(); // Output: Woof!
dog.eat(); // Output: Buddy is eating.
}
}
Interface
- An interface is a collection of abstract methods. It defines a contract that a class must adhere to.
- Unlike abstract classes, an interface cannot have instance variables or concrete methods. It only contains method declarations (without implementations) and constants (static final variables).
- A class can implement multiple interfaces, allowing it to inherit the abstract methods from all the interfaces it implements.
- Interfaces are useful when you want to define common behaviors that can be shared among unrelated classes.
Example in Java:
interface Printable {
void print();
}
interface Displayable {
void display();
}
class Document implements Printable, Displayable {
public void print() {
System.out.println("Printing document…");
}
public void display() {
System.out.println("Displaying document…");
}
}
public class Main {
public static void main(String[] args) {
Document doc = new Document();
doc.print(); // Output: Printing document…
doc.display(); // Output: Displaying document…
}
}
Conclusion
In summary, both abstract classes and interfaces provide a way to define common behavior and contracts for classes. Abstract classes are used when you want to create a base class with common behavior and characteristics, while interfaces are used when you want to define a contract that multiple unrelated classes can adhere to.