Object Oriented Programming in Java

Object Oriented Programming in Java

Part 5

Abstract Classes

Sometimes, you want to create a superclass that defines a generalized form shared by all its subclasses, leaving each subclass to fill in the details. This is where abstract classes come in.

An abstract class can have abstract methods, which are methods that are declared but not implemented in the abstract class. Subclasses must provide implementations for these abstract methods, making them essential for the subclass to have any meaning.

Let's look at an example:

abstract class Shape {
    abstract double area();
    abstract double perimeter();
}

// We cant create abstract constructor
//    We cant create abstract static methods
//    Atleast one of the method should be abstract

class Circle extends Shape {
    private double radius;

    Circle(double radius) {
        this.radius = radius;
    }

    @Override
    double area() {
        return Math.PI * radius * radius;
    }

    @Override
    double perimeter() {
        return 2 * Math.PI * radius;
    }
}

class Rectangle extends Shape {
    private double length;
    private double width;

    Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    double area() {
        return length * width;
    }

    @Override
    double perimeter() {
        return 2 * (length + width);
    }
}

In this example, Shape is an abstract class with abstract methods area() and perimeter(). The Circle and Rectangle classes extend Shape and provide implementations for these abstract methods.

Key points about abstract classes:

  • They cannot be instantiated directly.

  • They can contain both abstract and concrete methods.

  • Subclasses must implement all abstract methods or be declared abstract themselves.

Interfaces

Interfaces are similar to abstract classes, but they can only contain abstract methods. They define a contract that classes must adhere to, without specifying how the methods are implemented.

Let's see an example:

interface Animal {
    void eat();
    void sleep();
}

class Dog implements Animal {
    @Override
    public void eat() {
        System.out.println("Dog is eating");
    }

    @Override
    public void sleep() {
        System.out.println("Dog is sleeping");
    }
}

class Cat implements Animal {
    @Override
    public void eat() {
        System.out.println("Cat is eating");
    }

    @Override
    public void sleep() {
        System.out.println("Cat is sleeping");
    }
}

In this example, Animal is an interface with methods eat() and sleep(). Both Dog and Cat classes implement the Animal interface and provide implementations for these methods.

Key points about interfaces:

  • They cannot be instantiated directly.

  • All methods in an interface are implicitly public and abstract.

  • A class can implement multiple interfaces but can only extend one class.

Abstract Class vs Interface

  • An abstract class can have abstract and non-abstract methods, while an interface can only have abstract methods.

  • Variables in an interface are by default final, while an abstract class can have non-final variables.

  • An abstract class can provide implementations for interface methods, but an interface cannot provide implementations for abstract class methods.

  • A class can implement multiple interfaces but can only extend one class.

Annotations

Annotations provide metadata about a program that can be used by the compiler or at runtime. They are a form of syntactic metadata that can be added to Java source code.

Example:

javaCopy codeimport java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
    String value() default "default value";
}

class MyClass {
    @MyAnnotation("annotation value")
    public void myMethod() {
        // Method implementation
    }
}

In this example, @MyAnnotation is a custom annotation that can be applied to methods. The annotation has a single attribute value with a default value of "default value".

Key points about annotations:

  • Annotations can be applied to declarations such as classes, methods, fields, etc.

  • They can be retrieved at runtime using reflection.

  • Annotations can have attributes with default values.

In conclusion, abstract classes and interfaces are powerful tools in Java for creating flexible and modular code. Understanding when to use each can greatly improve the design of your Java applications.