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
Animal
class 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
Dog
class extends theAnimal
abstract class and provides an implementation for themakeSound()
method. -
The
sleep()
method is inherited from theAnimal
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()
andstop()
. -
The
Car
class implements theVehicle
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 theVehicle
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) andDrawable
(an interface) define abstract methods. -
However,
Shape
can also include concrete methods, whileDrawable
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
extendsAnimal
and implementsFlyable
). - Interfaces enable multiple inheritance of type, whereas abstract classes do not.
Key Takeaways
-
Abstract Class: CAN have both
abstract
andconcrete functions
; used for similarclasses
. -
Interface: Has solely
abstract methods
(before Java 8); common behavior across unrelated classes. -
Similarities: Both structure
abstract methods
and act as blueprints forclasses
. -
Differences:
Abstract classes
can haveconstructors
andfields
, andinterfaces
do not;interfaces
can have multipleinheritance
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.