In Java, both abstract classes and interfaces are used to define abstract types. An abstract type is a type that is not meant to be instantiated directly, but rather serves as a template for other classes to extend or implement.
The main difference between abstract classes and interfaces is that abstract classes can contain both abstract and non-abstract methods, while interfaces can only contain abstract methods.
Here’s an example Java program that demonstrates the difference between abstract classes and interfaces:
public abstract class Animal {
private String species;
public Animal(String species) {
this.species = species;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
public abstract void makeSound();
}
public interface Mammal {
public void giveBirth();
}
}
public class Dog extends Animal implements Mammal {
private String breed;
public Dog(String species, String breed) {
super(species);
this.breed = breed;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public void makeSound() {
System.out.println("Woof!");
}
public void giveBirth() {
System.out.println("Giving birth to puppies...");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog("Canine", "Labrador Retriever");
System.out.println("My dog is a " + myDog.getBreed()
+ " " + myDog.getSpecies() + ".");
myDog.makeSound();
myDog.giveBirth();
}
}
In this program, we define an abstract class called Animal that represents an animal. The Animal class has an instance variable (species) and a getter and setter method for the species variable. It also has an abstract method called makeSound, which represents a behavior that all animals should have.
We then define an interface called Mammal that represents a mammal. The Mammal interface has an abstract method called giveBirth, which represents a behavior that all mammals should have.
Finally, we define a subclass called Dog that extends the Animal class and implements the Mammal interface. The Dog class has an additional instance variable (breed) and a getter and setter method for the breed variable. It also provides implementations for the makeSound and giveBirth methods.
When we create an object of the Dog class called myDog, we can call the getBreed, getSpecies, makeSound, and giveBirth methods of the myDog object to print a message to the console.
When we run this program, it outputs the following:
My dog is a Labrador Retriever Canine.
Woof!
Giving birth to puppies...
This shows how we can use both abstract classes and interfaces in Java to define abstract types and provide implementations for them. Note how the Animal class is abstract and has an abstract method, while the Mammal interface only has an abstract method. This demonstrates the difference between abstract classes and interfaces, where abstract classes can contain both abstract and non-abstract methods, while interfaces can only contain abstract methods.