Interface vs Abstract Classes
In object-oriented programming, both interfaces and abstract classes are used to achieve abstraction, allowing you to define methods that must be implemented by derived classes. However, they serve different purposes and have distinct characteristics.
1. 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 contain both abstract methods (without implementation) and concrete methods (with implementation). Abstract classes are used to provide a common base for derived classes.
Key Features of Abstract Classes​
- Can have both abstract and concrete methods.
- Can have member variables.
- Can provide a default implementation for some methods.
Example of Abstract Class​
C++ Code
#include <iostream>
using namespace std;
class Animal {
public:
// Abstract method
virtual void sound() = 0; // Pure virtual function
void sleep() {
cout << "Sleeping..." << endl;
}
};
class Dog : public Animal {
public:
void sound() {
cout << "Woof!" << endl;
}
};
int main() {
Dog dog;
dog.sound(); // Calls the sound method
dog.sleep(); // Calls the sleep method from the Animal class
return 0;
}
Java Code
abstract class Animal {
// Abstract method
abstract void sound();
void sleep() {
System.out.println("Sleeping...");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Calls the sound method
dog.sleep(); // Calls the sleep method from the Animal class
}
}
2. What is an Interface?​
An interface is a contract that defines a set of methods that implementing classes must provide. It cannot contain any implementation itself (in languages like Java) and is used to achieve multiple inheritance.
Key Features of Interfaces​
- Can only contain abstract methods (Java 8 and above allows default methods).
- Cannot have member variables (only constants).
- Supports multiple inheritance.
Example of Interface​
C++ Code
#include <iostream>
using namespace std;
class IAnimal {
public:
virtual void sound() = 0; // Pure virtual function
};
class Cat : public IAnimal {
public:
void sound() {
cout << "Meow!" << endl;
}
};
int main() {
Cat cat;
cat.sound(); // Calls the sound method
return 0;
}
Java Code
interface IAnimal {
void sound(); // Abstract method
}
class Cat implements IAnimal {
public void sound() {
System.out.println("Meow!");
}
}
public class Main {
public static void main(String[] args) {
Cat cat = new Cat();
cat.sound(); // Calls the sound method
}
}
3. Key Differences Between Abstract Classes and Interfaces​
Feature | Abstract Class | Interface |
---|---|---|
Instantiation | Cannot be instantiated | Cannot be instantiated |
Method Implementation | Can have both abstract and concrete methods | Can only have abstract methods (until Java 8) |
Inheritance | Can extend only one abstract class | Can implement multiple interfaces |
Access Modifiers | Can use any access modifier | All methods are public by default |
Member Variables | Can have member variables | Cannot have member variables |