Files
2025-03-16 18:59:42 +00:00

3.9 KiB
Executable File

What is an Interface in Object-oriented Programming and how Does it Differ from a Class?

What Is an Interface in Object-Oriented Programming?

An interface is a contract in object-oriented programming (OOP) that defines a set of methods (and sometimes properties) that a class must implement. It specifies what a class should do, but not how it should do it.

Key characteristics of an interface:

  1. Method Declaration: An interface only contains method signatures (names, return types, and parameters) without implementation.
  2. Multiple Inheritance: Many OOP languages, like Java, allow a class to implement multiple interfaces, enabling multiple inheritance of behavior.
  3. Abstraction: Interfaces are entirely abstract, meaning they cannot have any concrete (implemented) methods in many languages (e.g., Java before version 8).
  4. No State: Interfaces do not contain instance variables or state but may include constants.

Example in Java:

// Define an interface
public interface Animal {
    void eat();  // Abstract method
    void sleep();  // Abstract method
}

// Class implementing the interface
public class Dog implements Animal {
    @Override
    public void eat() {
        System.out.println("Dog is eating.");
    }

    @Override
    public void sleep() {
        System.out.println("Dog is sleeping.");
    }
}

What Is a Class?

A class is a blueprint for creating objects in OOP. It defines the structure (fields or attributes) and behavior (methods) of objects. Unlike interfaces, classes can have implementations, state, and behavior.

Key characteristics of a class:

  1. State and Behavior: Classes define fields to hold data (state) and methods to operate on that data (behavior).
  2. Concrete Methods: Methods in a class can have concrete implementations.
  3. Inheritance: A class can inherit from another class (single inheritance in Java) to reuse or extend functionality.
  4. Constructors: Classes can have constructors to initialize objects.

Example in Java:

// Define a class
public class Dog {
    private String name;  // Field (state)

    // Constructor
    public Dog(String name) {
        this.name = name;
    }

    // Method (behavior)
    public void bark() {
        System.out.println(name + " is barking.");
    }
}

Key Differences Between an Interface and a Class

Aspect Interface Class
Purpose Defines a contract for behavior. Defines both state (fields) and behavior (methods).
Implementation Does not provide method implementations (in most cases). Can provide both concrete (implemented) and abstract methods.
Fields/State Cannot contain instance variables (only constants). Can contain fields (instance and static variables).
Inheritance A class can implement multiple interfaces. A class can inherit from only one superclass in languages like Java.
Constructors Cannot have constructors. Can have constructors to initialize objects.
Abstract Interfaces are entirely abstract (unless extended in modern versions). Can be abstract or concrete.
Default Methods Starting from Java 8, interfaces can have default methods (methods with a body). Classes always support method implementation.

When To Use an Interface?

  • Polymorphism: When you want different classes to provide their specific implementation of the same behavior.
  • Decoupling: To decouple code by relying on abstractions rather than concrete classes.
  • Multiple Inheritance: When you need a class to inherit behavior from multiple sources.

When To Use a Class?

  • When you need to define an object with both state and behavior.
  • When you need concrete implementations of methods.
  • When abstraction is not required.

Summary:

An interface defines the "what" (a contract) without defining the "how," while a class provides the actual implementation and can store both state and behavior.