Skip to content

Classes, Interfaces, and Abstract Classes in Java

In Java, classes, interfaces, and abstract classes are key concepts in object-oriented programming. They provide various ways of defining and organizing behavior, allowing you to model real-world scenarios in code.

Let’s explore Classes, Interfaces, and Abstract Classes in more detail:


1. Classes in Java

A class is a blueprint for creating objects (instances). It defines the properties (attributes) and behaviors (methods) that objects of this class will have.

Syntax:

java
class ClassName {
    // Fields (attributes)
    dataType fieldName;

    // Constructor
    public ClassName() {
        // Constructor code
    }

    // Methods (behaviors)
    public void methodName() {
        // Method code
    }
}

Key Concepts:

  • Attributes (Fields): Variables that define the properties of the object.
  • Constructor: Initializes the object when it is created.
  • Methods: Functions that define the behavior of the object.

Example:

java
class Car {
    String make;
    String model;
    int year;

    // Constructor
    public Car(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    // Method
    public void startEngine() {
        System.out.println("The engine of " + make + " " + model + " is now running.");
    }
}

2. Interfaces in Java

An interface is a reference type in Java, similar to a class but with some important differences. It defines a contract or a set of method signatures (abstract methods) that other classes must implement.

Syntax:

java
interface InterfaceName {
    // Abstract method signatures
    void method1();
    void method2();
}

Key Concepts:

  • Abstract Methods: Interfaces cannot provide method implementations. The classes that implement the interface must provide the method body.
  • Multiple Implementations: A class can implement multiple interfaces.

Example:

java
interface Drivable {
    void start();  // Method signature
    void stop();   // Method signature
}

class Car implements Drivable {
    public void start() {
        System.out.println("Car is starting");
    }

    public void stop() {
        System.out.println("Car is stopping");
    }
}

Explanation:

  • The Drivable interface defines the contract for start and stop methods, but does not provide implementations.
  • The Car class implements Drivable and provides the method implementations for start and stop.

Key Features of Interfaces:

  • No Method Body: Interfaces define only method signatures (without body).
  • Multiple Inheritance: A class can implement multiple interfaces.
  • Default and Static Methods: From Java 8 onwards, interfaces can have default methods (with a body) and static methods.

3. Abstract Classes in Java

An abstract class is a class that cannot be instantiated directly. It is used as a base class for other classes and may contain both abstract methods (methods without implementation) and concrete methods (methods with an implementation).

Syntax:

java
abstract class AbstractClass {
    // Abstract method (no body)
    abstract void abstractMethod();

    // Concrete method (with body)
    void concreteMethod() {
        System.out.println("This is a concrete method");
    }
}

Key Concepts:

  • Abstract Methods: Methods without a body that must be implemented by subclasses.
  • Concrete Methods: Methods with an implementation that can be used by subclasses.
  • Cannot be Instantiated: You cannot create an instance of an abstract class directly.
  • Inheritance: An abstract class is meant to be extended by subclasses that provide implementations for abstract methods.

Example:

java
abstract class Animal {
    // Abstract method (no body)
    abstract void sound();

    // Concrete method (with body)
    public void eat() {
        System.out.println("This animal is eating.");
    }
}

class Dog extends Animal {
    // Implementing the abstract method
    public void sound() {
        System.out.println("The dog barks.");
    }
}

Explanation:

  • The Animal class is abstract and contains both an abstract method (sound()) and a concrete method (eat()).
  • The Dog class extends Animal and provides an implementation for the abstract method sound().

Key Features of Abstract Classes:

  • Abstract Methods: Can define methods that must be implemented by subclasses.
  • Concrete Methods: Can have methods with implementations.
  • Can Have Fields: Unlike interfaces, abstract classes can have fields (instance variables).
  • Can Be Instantiated: Only concrete (non-abstract) subclasses can be instantiated.

4. Key Differences Between Classes, Interfaces, and Abstract Classes

FeatureClassInterfaceAbstract Class
InstantiationCan be instantiated directlyCannot be instantiatedCannot be instantiated directly
MethodsCan have both concrete and abstract methodsOnly abstract methods (no body) unless default/static methods (Java 8+)Can have both abstract and concrete methods
FieldsCan have instance variablesFields are implicitly public, static, and finalCan have instance variables
Multiple InheritanceCan only extend one classCan implement multiple interfacesCan only extend one class
PurposeUsed for creating objects with specific behaviorUsed to define a contract that other classes must followUsed to share code and define a common base for other classes
ConstructorCan have constructorsCannot have constructorsCan have constructors

5. When to Use Each

  • Use a Class when you want to define objects with specific properties and behaviors. Classes are used when the implementation of methods is necessary, and inheritance is required.

  • Use an Interface when you want to define a contract (set of methods) that can be implemented by any class, regardless of the class hierarchy. This is ideal for multiple inheritance of behavior (since a class can implement multiple interfaces).

  • Use an Abstract Class when you want to create a base class that shares common functionality but should not be instantiated directly. Abstract classes are useful when you want to provide some shared method implementations while forcing subclasses to implement other abstract methods.


Conclusion

  • Classes are used to represent real-world objects and provide both data and behavior.
  • Interfaces define a contract that classes must follow, allowing different classes to provide different implementations for the same methods.
  • Abstract Classes provide a common base class with a mix of fully implemented methods and methods that must be implemented by subclasses.

Each of these structures is essential for organizing code in an object-oriented way, and understanding when to use each is crucial to writing efficient and maintainable Java programs.

J2J Institute private limited