Java Fundamentals: Abstract Class & Interface
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
Animalclass is declared asabstract, 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
Dogclass extends theAnimalabstract class and provides an implementation for themakeSound()method. -
The
sleep()method is inherited from theAnimalclass 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
Vehicleinterface defines a contract with two abstract methods:start()andstop(). -
The
Carclass implements theVehicleinterface 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
Carclass adheres to theVehicleinterface'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) andDrawable(an interface) define abstract methods. -
However,
Shapecan also include concrete methods, whileDrawablecannot.
// 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.,
BirdextendsAnimaland implementsFlyable). - Interfaces enable multiple inheritance of type, whereas abstract classes do not.
Key Takeaways
-
Abstract Class: CAN have both
abstractandconcrete functions; used for similarclasses. -
Interface: Has solely
abstract methods(before Java 8); common behavior across unrelated classes. -
Similarities: Both structure
abstract methodsand act as blueprints forclasses. -
Differences:
Abstract classescan haveconstructorsandfields, andinterfacesdo not;interfacescan have multipleinheritanceof 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.
