Concept of interfaces and Abstract classes

Comprehensive study notes, diagrams, and exam preparation for Concept of interfaces and Abstract classes.

Concept of Interfaces and Abstract Classes

Definition

An abstract class is a class that cannot be instantiated directly and may contain both abstract methods (methods without implementation) and concrete methods (methods with implementation). It is used to provide a common base for related classes.

An interface is a reference type that contains method declarations and constants, and it defines a contract that implementing classes must follow. It specifies what a class should do, but not necessarily how it should do it.


Main Content

1. Abstract Classes

Meaning and purpose

  • An abstract class is a partially implemented class. It is designed to act as a parent class for other classes. It is used when classes share common features, but each subclass may have some unique behavior. For example, if we have classes like Circle, Rectangle, and Triangle, we can create an abstract class Shape with common properties like color or fill(), while leaving the area calculation to each subclass.

Features and behavior

  • An abstract class can contain instance variables, constructors, abstract methods, and normal methods. This makes it useful when we want to provide both a common data structure and some default behavior. For example:
abstract class Shape {
    String color;

    Shape(String color) {
        this.color = color;
    }

    abstract double area();

    void displayColor() {
        System.out.println("Color: " + color);
    }
}

Here, area() is abstract, so every subclass must define it, while displayColor() is already implemented.

Example of inheritance from an abstract class:

class Circle extends Shape {
    double radius;

    Circle(String color, double radius) {
        super(color);
        this.radius = radius;
    }

    double area() {
        return 3.14 * radius * radius;
    }
}

2. Interfaces

Meaning and purpose

  • An interface defines a set of methods that a class must implement. It is mainly used to specify a behavior contract. For example, if different classes like Printer, Scanner, and Camera all need a connect() behavior, an interface can define that behavior without caring about how each class implements it.

Features and behavior

  • Interfaces are very useful when unrelated classes need to share the same behavior. A class can implement multiple interfaces, which supports multiple inheritance of type. Interfaces help achieve complete abstraction because they traditionally define only method signatures, though many languages now also allow default and static methods. For example:
interface Printable {
    void print();
}

Any class implementing Printable must provide its own print() method.

Example of implementation:

class Document implements Printable {
    public void print() {
        System.out.println("Printing document");
    }
}

Interfaces are often used in large systems to make code loosely coupled. For example, a payment system may define a PaymentMethod interface and then implement it using CreditCard, UPI, or PayPal.


3. Difference Between Abstract Classes and Interfaces

Nature and structure

  • An abstract class is a partially complete class, whereas an interface is a pure contract. Abstract classes are used when there is a strong “is-a” relationship and shared code, while interfaces are used when different classes must follow the same behavior pattern.

Inheritance and implementation

  • A class can inherit only one abstract class in most languages such as Java, but it can implement multiple interfaces. This makes interfaces more flexible for designing systems where multiple behaviors are needed. For example, a SmartPhone class may implement Camera, MusicPlayer, and InternetDevice interfaces, while still extending one base class.

Members and usage

  • Abstract classes can have constructors, fields, and fully implemented methods; interfaces usually contain method declarations and constants. Abstract classes are suitable for code reuse, while interfaces are suitable for standardization and abstraction.

Comparison table:

Feature Abstract Class Interface
Instantiation Cannot be instantiated Cannot be instantiated
Methods Abstract and concrete methods Mostly abstract methods, with default/static methods in modern languages
Variables Instance variables allowed Only constants in many languages
Constructors Allowed Not allowed
Inheritance Single inheritance Multiple implementation allowed
Purpose Code reuse and partial abstraction Full abstraction and contract definition

Simple illustration of use:

Shape (abstract class)
   |
   +-- Circle
   +-- Rectangle

Printable (interface)
   |
   +-- Document
   +-- Report

This shows that abstract classes are typically used to build a class hierarchy, while interfaces are used to give common behavior across different classes.


Working / Process

1. Identify the common features and behaviors

  • First, analyze the problem and determine what is common among the classes. If there are shared data and some common methods, an abstract class may be appropriate.
  • If the main requirement is only to ensure that classes follow a certain behavior, then an interface is better.
  • Example: In a vehicle system, Vehicle may be an abstract class because all vehicles have some common features like speed or fuel type, while Drivable may be an interface because many different objects can be driven in different ways.

2. Create the base type

  • For an abstract class, define the common fields, constructors, and methods, and mark methods that must be implemented by subclasses as abstract.
  • For an interface, define the method signatures that any implementing class must provide.
  • Example:
   interface Flyable {
       void fly();
   }

   abstract class Bird {
       String name;
       Bird(String name) {
           this.name = name;
       }
       void eat() {
           System.out.println(name + " is eating");
       }
       abstract void sound();
   }

3. Extend or implement in child classes

  • Subclasses of an abstract class must implement all abstract methods unless the subclass is also abstract.
  • Classes that implement interfaces must define all the interface methods.
  • Example:
   class Sparrow extends Bird implements Flyable {
       Sparrow(String name) {
           super(name);
       }
       void sound() {
           System.out.println("Chirp chirp");
       }
       public void fly() {
           System.out.println(name + " is flying");
       }
   }
  • This process ensures code reuse, standard behavior, and proper abstraction.

Advantages / Applications

Promotes abstraction

  • Both interfaces and abstract classes hide implementation details and show only essential features to the user.

Improves code reuse and flexibility

  • Abstract classes allow common code to be shared, while interfaces allow different classes to support the same behavior without forcing a fixed class relationship.

Useful in real-world software design

  • They are widely used in GUI frameworks, payment systems, device drivers, logging systems, and plugin architectures where standard behavior is required but implementations may differ.

Summary

  • Abstract classes provide partial implementation and are used for shared base behavior.
  • Interfaces define a contract and are used to enforce common behavior across classes.
  • Both are key tools for achieving abstraction in inheritance-based design.
  • Important terms to remember: abstract class, interface, abstraction, inheritance, implementation, contract.