difference between abstract class and interface

What is difference between abstract class and interface

Abstract classes and interfaces are both mechanisms in Java for achieving abstraction, but they have some key differences in terms of their purpose, usage, and capabilities.

difference between abstract class and interface

Abstract Class:

1. Purpose: An abstract class is used to provide a common base for a group of related classes. It represents a common concept or functionality shared among multiple classes.

2. Method Implementation: Abstract classes can have both abstract methods (methods without a body, declared using the abstract keyword) and concrete methods (methods with an implementation).

3. Fields (Variables): Abstract classes can have instance variables (fields) that can be inherited by their subclasses.

4. Constructors: Abstract classes can have constructors, and they are typically used to initialize the fields of the abstract class.

5. Access Modifiers: Abstract classes can have access modifiers for their methods, allowing for control over the visibility of methods in subclasses.

6. Multiple Inheritance: Java supports single inheritance, so a class can extend only one abstract class.

Interface:

1. Purpose: An interface is used to define a contract for a set of classes. It represents a set of abstract methods that any class implementing the interface must provide.

2. Method Declaration: Interfaces can only declare abstract methods (methods without a body). They do not contain method implementations.

3. Fields (Variables): Interfaces cannot have instance variables (fields). They can only have constant (static final) variables.

4. Constructors: Interfaces cannot have constructors, as they cannot be instantiated.

5. Access Modifiers: All methods in an interface are implicitly public and abstract. Fields are implicitly public, static, and final.

6. Multiple Inheritance: Java supports multiple inheritance through interfaces. A class can implement multiple interfaces.

Here’s a tabular representation of the differences between abstract classes and interfaces in Java:

FeatureAbstract ClassInterface
PurposeProvides a common base for related classes.Defines a contract for a set of classes.
Method ImplementationCan have both abstract and concrete methods.Contains only abstract methods (no body).
Fields (Variables)Can have instance variables (fields).Cannot have instance variables, only constants (static final).
ConstructorsCan have constructors.Cannot have constructors.
Access ModifiersCan have access modifiers for methods and fields.All methods are implicitly public and abstract. Fields are implicitly public, static, and final.
Multiple InheritanceSupports single inheritance.Supports multiple inheritance.
UsageUse when you want to provide common behavior and share code among related classes.Use when you want to define a contract for a set of unrelated classes or provide multiple inheritance.
InstantiationCannot be instantiated. Must be subclassed.Cannot be instantiated directly.
Use of super KeywordCan use the super keyword to call the superclass’s methods and constructor.N/A
Keyword Usedabstract class ClassName { ... }interface InterfaceName { ... }

This table summarizes the key distinctions between abstract classes and interfaces in terms of their usage, features, and capabilities. Understanding these differences helps in choosing the appropriate approach based on the requirements of your design.

When to Use Abstract Class vs. Interface:

Abstract Class:

  • Use when you want to provide a common base class with some default behavior.
  • When there is a need for fields (variables) that are shared among subclasses.
  • When you need constructors in your abstraction.

Interface:

  • Use when you want to define a contract for a set of classes, irrespective of their inheritance hierarchy.
  • When multiple inheritance is needed, as a class can implement multiple interfaces.
  • When you want to achieve a form of “pure” abstraction without providing any default behavior.

Example:

Example
// Abstract class example
abstract class Animal {
    String name;

    public Animal(String name) {
        this.name = name;
    }

    abstract void makeSound();

    void eat() {
        System.out.println(name + " is eating.");
    }
}

// Interface example
interface Flyable {
    void fly();
}

class Bird extends Animal implements Flyable {
    public Bird(String name) {
        super(name);
    }

    @Override
    void makeSound() {
        System.out.println("Chirp!");
    }

    @Override
    public void fly() {
        System.out.println(name + " is flying.");
    }
}

In this example, Animal is an abstract class with a mix of abstract and concrete methods, while Flyable is an interface with only abstract methods. The Bird class extends Animal and implements Flyable, showcasing the differences between abstract classes and interfaces.

Leave a Reply

Your email address will not be published. Required fields are marked *