Java Fundamentals: Abstract Class & Interface

Java Fundamentals: Abstract Class & Interface

Java Fundamentals: Abstract Class & Interface

{getToc} $title={Table of Contents} $count={true}

Introduction

Abstract Classes and Interfaces in Java Interfaces are one of the building blocks of O O P ( Object Oriented Programming ) Developers can define a a template for classes, providing uniformity and reusability across the codebase. Both are templates for other classes, but they differ dramatically in their structure and usage.

An Abstract Class cannot be instantiated. be instantiated, and can include abstract methods (without abstract methods (without implementation) and concrete methods (with implementation). It is usually when you want to share code across several complementary classes. On the other hand, an Although Interface is a fully abstract type that describes a contract for what a class can do, but without implementation implementation details. Interfaces are perfect for specifying capabilities you can share out accumulated data across unrelated classes.

Java Fundamentals: Abstract Class & Interface

Abstract Class

// Example 1: Basic Abstract Class
abstract class Animal {
    // Abstract method (no implementation)
    public abstract void makeSound();

    // Concrete method (with implementation)
    public void sleep() {
        System.out.println("This animal is sleeping.");
    }
}

class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("The dog barks.");
    }
}
        

Explanation:

  • The Animal class is declared as abstract, meaning it cannot be instantiated directly.
  • The makeSound() method is abstract, so any subclass must implement it.
  • The sleep() method is concrete and provides default behavior for all subclasses.
// Example 2: Using Abstract Class
public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.makeSound(); // Output: The dog barks.
        myDog.sleep();     // Output: This animal is sleeping.
    }
}
        

Explanation:

  • The Dog class extends the Animal abstract class and provides an implementation for the makeSound() method.
  • The sleep() method is inherited from the Animal class and does not need to be redefined.

Interface

// Example 1: Basic Interface
interface Vehicle {
    void start(); // Abstract method (implicitly public and abstract)
    void stop();
}

class Car implements Vehicle {
    @Override
    public void start() {
        System.out.println("Car started.");
    }

    @Override
    public void stop() {
        System.out.println("Car stopped.");
    }
}
        

Explanation:

  • The Vehicle interface defines a contract with two abstract methods: start() and stop().
  • The Car class implements the Vehicle interface and provides concrete implementations for both methods.
// Example 2: Using Interface
public class Main {
    public static void main(String[] args) {
        Vehicle myCar = new Car();
        myCar.start(); // Output: Car started.
        myCar.stop();  // Output: Car stopped.
    }
}
        

Explanation:

  • The Car class adheres to the Vehicle interface's contract by implementing its methods.
  • Interfaces allow unrelated classes to share common functionality without inheriting from a common base class.

Abstract Class vs. Interface

// Example 1: Similarity - Both can have abstract methods
abstract class Shape {
    public abstract double area();
}

interface Drawable {
    void draw();
}
        

Explanation:

  • Both Shape (an abstract class) and Drawable (an interface) define abstract methods.
  • However, Shape can also include concrete methods, while Drawable cannot.
// Example 2: Difference - Multiple Inheritance
interface Flyable {
    void fly();
}

class Bird extends Animal implements Flyable {
    @Override
    public void makeSound() {
        System.out.println("The bird chirps.");
    }

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

Explanation:

  • A class can extend only one abstract class but can implement multiple interfaces (e.g., Bird extends Animal and implements Flyable).
  • Interfaces enable multiple inheritance of type, whereas abstract classes do not.

Key Takeaways

  • Abstract Class: CAN have both abstract and concrete functions; used for similar classes.
  • Interface: Has solely abstract methods (before Java 8); common behavior across unrelated classes.
  • Similarities: Both structure abstract methods and act as blueprints for classes.
  • Differences: Abstract classes can have constructors and fields, and interfaces do not; interfaces can have multiple inheritance of type.

Conclusion

Understanding Abstract Classes At Interfaces, we are passionate about a greater mastery of Java Object Oriented Programming. This gives these concepts flexibility, Encourage code reuse, and help develop scalable applications. By leveraging Abstract classes can be used for shared behavior, and there can be interfaces for shared This allows developers to build systems that are resilient and easy to work with.

Previous Post Next Post