Difference Between Interface and Inheritance

Comparison between Interface and Interface

  • Interface and Inheritance are two essential concepts in object-oriented programming.
  • The key Difference Between Interface and Inheritance is that Interface defines a contract that a class must follow, whereas Inheritance allows a class to inherit the properties and behaviors of another class.

Comparison Chart

  • Interface and Inheritance differ in many ways. Let’s have a look at the major differences between these two object-oriented programming concepts.
Interface Inheritance
Declared using the interface keyword. Declared using the extends keyword in Java
Defines a blueprint that a class must follow Allows a child class to inherit properties and behavior from a parent class
Specifies a set of methods that a class must implement without providing implementation details. Provides implementation details for properties and methods in the parent class, which can be reused in the child class.
Members declared as constant. Members declared as constant and variable.
Interface methods are Public by default. Inheritance methods can have different access modifiers. It can be Public, private or protected.
Does not have a constructor Has a default constructor that is called when an object is created

What is Interface?

In OOP, an interface defines a contract that a class must follow. An interface specifies a set of methods that a class must implement, but it does not provide any implementation details. Instead, it defines the signature of each method, including the method name, return type, and parameters. A class that implements an interface must provide an implementation for each method defined in the interface.

Interface in Java

In Java, an interface is declared using the interface keyword. For example:

public interface Drawable {
    void draw();
}

This interface defines a single method, draw(), that any class implementing the Drawable interface must provide.

What is Inheritance?

Inheritance is another mechanism for code reuse in OOP. With inheritance, a class can inherit properties and behavior from a parent class. This means that a child class can reuse code from its parent class, reducing the amount of code duplication and promoting code organization.

There are three types of inheritance:

Single Inheritance

Single inheritance occurs when a child class inherits from a single parent class. For example:

public class Rectangle extends Shape {
    // class implementation
}

Here, Rectangle is a child class that inherits from the Shape class.

Multilevel Inheritance

Multilevel inheritance occurs when a child class inherits from a parent class, and that parent class itself inherits from another parent class. For example:

public class Cube extends Shape3D {
    // class implementation
}

public class Shape3D extends Shape {
    // class implementation
}

public class Shape {
    // class implementation
}
  1. Here, Cube is a child class that inherits from the Shape3D class, which itself inherits from the Shape class.

Conclusion

In summary, interface and inheritance are both useful mechanisms for code reuse and organization in OOP. Interfaces provide a way to define a contract that a class must follow, while inheritance allows a child class to inherit properties and behavior from a parent class. Understanding the differences between these concepts is important for writing clean, maintainable code.

FAQs

  1. What is the main difference between interface and inheritance?
  • Interface defines a contract that a class must follow, while inheritance allows a child class to inherit properties and behavior from a parent class.
  1. When should I use an interface?
  • Interfaces are typically used when you want to enforce a specific contract for a class or when you want to implement multiple inheritance-like behavior in languages that don’t support it.
  1. When should I use inheritance?
  • Inheritance is typically used when you want to reuse properties and behavior from an existing class. However, it should be used sparingly and only when it makes sense for your code design.
  1. Can a class implement multiple interfaces?
  • Yes, a class can implement multiple interfaces.
  1. Can a child class override properties and methods inherited from a parent class?
  • Yes, a child class can override properties and methods inherited from a parent class if needed.

More Differences