Abstract Classes and Interfaces in Java


In Java, abstract classes and interfaces are used to achieve abstraction, which allows you to define methods that must be implemented by subclasses or implementing classes. However, there are key differences between abstract classes and interfaces in terms of usage and functionality.

1. Abstract Classes in Java

An abstract class is a class that cannot be instantiated on its own. It is meant to be inherited by other classes. An abstract class can contain abstract methods (methods without a body) and concrete methods (methods with a body).

Syntax of an Abstract Class

    abstract class ClassName {
        // Abstract method (does not have a body)
        abstract void abstractMethod();
    
        // Concrete method (with a body)
        void concreteMethod() {
            System.out.println("This is a concrete method.");
        }
    }
        

Example 1: Abstract Class

In this example, we create an abstract class Animal with an abstract method sound() and a concrete method eat().

    abstract class Animal {
        // Abstract method (does not have a body)
        abstract void sound();
    
        // Concrete method
        void eat() {
            System.out.println("This animal is eating.");
        }
    }
    
    class Dog extends Animal {
        // Provide implementation for the abstract method
        void sound() {
            System.out.println("The dog barks.");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Dog dog = new Dog();
            dog.sound();  // Calls the Dog class sound() method
            dog.eat();    // Calls the Animal class eat() method
        }
    }
        

Output:

    The dog barks.
    This animal is eating.
        

In this example, the class Dog extends the abstract class Animal and provides an implementation for the abstract method sound(). The concrete method eat() is inherited directly from the abstract class.

2. Interfaces in Java

An interface in Java is similar to an abstract class but with stricter rules. An interface cannot contain concrete methods (until Java 8, which introduced default methods). A class implements an interface by providing implementations for all the methods declared in the interface.

Syntax of an Interface

    interface InterfaceName {
        // Abstract methods
        void method1();
        void method2();
    }
        

Example 2: Interface

In this example, we create an interface Animal with two abstract methods: sound() and eat(). The Dog class implements the Animal interface.

    interface Animal {
        // Abstract methods
        void sound();
        void eat();
    }
    
    class Dog implements Animal {
        // Implement the methods from the Animal interface
        public void sound() {
            System.out.println("The dog barks.");
        }
    
        public void eat() {
            System.out.println("The dog eats.");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Dog dog = new Dog();
            dog.sound();  // Calls the Dog class sound() method
            dog.eat();    // Calls the Dog class eat() method
        }
    }
        

Output:

    The dog barks.
    The dog eats.
        

In this example, the interface Animal declares two abstract methods, and the class Dog implements the interface and provides definitions for both methods.

3. Key Differences Between Abstract Classes and Interfaces

Feature Abstract Class Interface
Multiple Inheritance Not supported directly (can be achieved through interfaces) Supports multiple inheritance (a class can implement multiple interfaces)
Methods Can have both abstract and concrete methods Can only have abstract methods (until Java 8, default methods are allowed)
Constructor Can have constructors Cannot have constructors
Access Modifiers Can have any access modifiers for methods and variables All methods are implicitly public (cannot have any other access modifiers)
Inheritance A class can extend only one abstract class A class can implement multiple interfaces

4. Conclusion

Abstract classes and interfaces both provide ways to achieve abstraction in Java. Abstract classes allow you to define both abstract and concrete methods, and they support inheritance through the extends keyword. Interfaces, on the other hand, allow you to define only abstract methods (except default methods introduced in Java 8), and a class can implement multiple interfaces. The choice between an abstract class and an interface depends on the specific needs of the program and the design of your application.





Advertisement