Popular Posts

July 11, 2024

Difference between Abstract Class and Interface in Java

 

In Java, both abstract classes and interfaces are used to define abstract types that can be used to specify the behavior that classes must implement. However, they have different features and purposes. Here are the key differences between them:

Abstract Class

  1. Definition: An abstract class is a class that cannot be instantiated on its own and can have abstract methods (methods without a body) as well as concrete methods (methods with a body).
  2. Usage: Abstract classes are used when you want to share code among several closely related classes.
  3. Constructors: An abstract class can have constructors.
  4. Access Modifiers: Methods and fields in an abstract class can have any access modifier (public, protected, private).
  5. Fields: An abstract class can have instance variables (fields).
  6. Inheritance: A class can extend only one abstract class (single inheritance).
  7. Implementation: Abstract classes can provide default implementation for some of the methods.
  8. Static Methods: Abstract classes can have static methods.
  9. State Management: Abstract classes can maintain state (instance variables).

Interface

  1. Definition: An interface is a reference type in Java, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.
  2. Usage: Interfaces are used to define a contract that other classes must adhere to. They are suited for declaring methods that one or more classes are expected to implement.
  3. Constructors: Interfaces cannot have constructors.
  4. Access Modifiers: All methods in an interface are implicitly public and abstract (except default and static methods, which are explicitly defined).
  5. Fields: Interfaces can only have static and final fields (constants).
  6. Inheritance: A class can implement multiple interfaces (multiple inheritance).
  7. Implementation: Interfaces cannot provide any method implementations except for default and static methods.
  8. Static Methods: Interfaces can have static methods.
  9. State Management: Interfaces cannot maintain state, as they cannot have instance variables.

Example:

Abstract Class Example

abstract class Animal {
    String name;

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

    abstract void makeSound();

    void sleep() {
        System.out.println(name + " is sleeping.");
    }
}

class Dog extends Animal {
    Dog(String name) {
        super(name);
    }

    @Override
    void makeSound() {
        System.out.println(name + " says: Woof Woof");
    }
}

Interface Example

interface Animal {
    void makeSound();

    default void sleep() {
        System.out.println("Animal is sleeping.");
    }
}

class Dog implements Animal {
    @Override
    public void makeSound() {
        System.out.println("Dog says: Woof Woof");
    }
}

When to Use What?

  • Abstract Class: Use when you have a base class that should not be instantiated and contains some common code that can be shared among derived classes.
  • Interface: Use when you want to define a contract for what a class can do, without dictating how it should do it. Interfaces are especially useful for defining capabilities that can be added to any class regardless of where it sits in the class hierarchy.

No comments:
Write comments