Java Interview Questions

30 Questions
Java

Java

Web DevelopmentBackend

Question 4

What is the difference between an abstract class and an interface?

Answer:

In Java, both abstract classes and interfaces are used to achieve abstraction, but they have significant differences in terms of design and usage. Here’s a detailed comparison:

Abstract Class

  1. Definition: An abstract class is a class that cannot be instantiated on its own and is meant to be subclassed. It can contain both abstract methods (without implementation) and concrete methods (with implementation).

  2. Methods: Can have a mix of fully implemented methods and abstract methods.

  3. Fields: Can have instance variables (fields) with any access modifiers (private, protected, public).

  4. Constructors: Can have constructors.

  5. Inheritance: A class can inherit only one abstract class due to single inheritance.

  6. Use Case: Used when classes share a common base class and some common behavior that can be inherited.

    Example:

    public abstract class Animal {
        private String name;
    
        public Animal(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public void sleep() {
            System.out.println(name + " is sleeping.");
        }
    
        public abstract void makeSound();  // Abstract method
    }
    
    public class Dog extends Animal {
        public Dog(String name) {
            super(name);
        }
    
        @Override
        public void makeSound() {
            System.out.println(getName() + " barks.");
        }
    }

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. Interfaces cannot contain instance variables or constructors.

  2. Methods: All methods in an interface are implicitly abstract (except default and static methods). Prior to Java 8, methods could not have bodies. Since Java 8, interfaces can have default and static methods with implementations.

  3. Fields: Fields in an interface are implicitly public, static, and final (constants).

  4. Constructors: Interfaces cannot have constructors.

  5. Inheritance: A class can implement multiple interfaces, overcoming the limitation of single inheritance.

  6. Use Case: Used to define a contract that multiple classes can implement, promoting a more flexible and decoupled design.

    Example:

    public interface Animal {
        void makeSound();  // Abstract method
    
        default void sleep() {  // Default method
            System.out.println("The animal is sleeping.");
        }
    }
    
    public class Dog implements Animal {
        @Override
        public void makeSound() {
            System.out.println("Dog barks.");
        }
    }
    
    public class Cat implements Animal {
        @Override
        public void makeSound() {
            System.out.println("Cat meows.");
        }
    }

Key Differences

  1. Implementation:

    • Abstract Class: Can provide some methods with implementation. Subclasses can use these methods directly.
    • Interface: Cannot provide implementations for methods (except default and static methods since Java 8).
  2. Multiple Inheritance:

    • Abstract Class: A class can inherit only one abstract class.
    • Interface: A class can implement multiple interfaces.
  3. State (Fields):

    • Abstract Class: Can have instance variables.
    • Interface: Cannot have instance variables, only constants (public static final).
  4. Access Modifiers:

    • Abstract Class: Can have methods with different access modifiers (private, protected, public).
    • Interface: Methods are implicitly public (prior to Java 9). In Java 9 and later, private methods are allowed in interfaces to support default and static methods.
  5. Constructors:

    • Abstract Class: Can have constructors to initialize the state of an object.
    • Interface: Cannot have constructors.

When to Use Which

  • Abstract Class: Use when you have a base class that should not be instantiated and has some common behavior that subclasses can inherit. It's suitable when the classes are closely related.
  • Interface: Use when you want to define a contract for what a class can do, without dictating how it should do it. It's suitable for defining capabilities that can be shared across unrelated classes.

By understanding these differences, you can make informed decisions on when to use abstract classes versus interfaces in your Java programs.

Recent job openings