

B
Discuss the differences between interfaces and abstract classes in Java. What are their respective use cases? How do they impact code design and flexibility?
An interface defines a contract that implementing classes must fulfill, allowing for multiple inheritance and promoting loose coupling. It can only contain method signatures and static final variables.
interface Animal {
void makeSound();
}
An abstract class can provide some method implementations while declaring others as abstract. It allows for shared state and behavior among subclasses, but a class can only inherit from one abstract class.
abstract class Shape {
abstract void draw();
void display() { System.out.println("Displaying shape"); }
}
Interfaces support multiple inheritance, allowing a class to implement multiple interfaces. Abstract classes do not, enforcing a single inheritance hierarchy.
class Dog implements Animal, Pet {}
Use interfaces when you need to define a contract for disparate classes. Use abstract classes when you have a common base with shared code or state.
interface Comparable { int compareTo(Object o); }