14. Compare abstract class and interface

Summary: 

 

Abstract Class

Interface
Expression of abstraction < 100% 100% Abstraction
An abstract class can have abstract and non-abstract methods Java version < 8, Interface can only have abstract method.

Java 8 version can add  default and static methods .

Java 9 version can add private methods

Abstract class does not support multiple inheritance Interface supports  multiple inheritance
Abstract class can have  final, non-final, static and non-static variables Interface has only static final variables
The abstract keyword is used to declare an abstract class The interface keyword is used to declare Interface
Use Abstract class when we can only complete a few standard functions (method/function) of the system, some of the remaining functions extend classes have to complete. These finished features are still usable as usual, these are general features. Use Interface when you want to build a standard framework of functions (method/function) that all modules/projects need. Modules must  implement all defined functionality.

 

Example 1: For Abstract Class

Java

// Abstract class Sunstar
abstract class Sunstar {
    // Abstract method printInfo
    abstract void printInfo();
}

// Employee class that extends the abstract class Sunstar
class Employee extends Sunstar {
    // Implementation of the abstract method printInfo
    void printInfo() {
        String name = "Avinash"; // Employee name
        int age = 21; // Employee age
        float salary = 222.2F; // Employee salary

        System.out.println(name); // Print name
        System.out.println(age); // Print age
        System.out.println(salary); // Print salary
    }
}

// Base class to test the implementation
class Base {
    public static void main(String args[]) {
        Sunstar s = new Employee(); // Create an Employee object
        s.printInfo(); // Call the printInfo method
    }
}
Output

avinash
21
222.2

 

Example: For Interface

Java

// Java Program to Illustrate Concept of Interface

// Importing I/O classes
import java.io.*;

// Interface Shape
interface Shape {
    // Abstract method draw
    void draw();
    // Abstract method area
    double area();
}

// Rectangle class that implements the Shape interface
class Rectangle implements Shape {
    int length, width; // Length and width of the rectangle

    // Constructor for Rectangle
    Rectangle(int length, int width) {
        this.length = length;
        this.width = width;
    }

    // Implementation of the abstract method draw
    @Override
    public void draw() {
        System.out.println("Rectangle has been drawn");
    }

    // Implementation of the abstract method area
    @Override
    public double area() {
        return length * width; // Calculate the area of the rectangle
    }
}

// Circle class that implements the Shape interface
class Circle implements Shape {
    double pi = 3.14; // Value of pi
    int radius; // Radius of the circle

    // Constructor for Circle
    Circle(int radius) {
        this.radius = radius;
    }

    // Implementation of the abstract method draw
    @Override
    public void draw() {
        System.out.println("Circle has been drawn");
    }

    // Implementation of the abstract method area
    @Override
    public double area() {
        return pi * radius * radius; // Calculate the area of the circle
    }
}

// Main class to test the implementation
public class GFG {
    public static void main(String[] args) {
        Shape rect = new Rectangle(2, 3); // Create a Rectangle object
        System.out.println("Area of rectangle: " + rect.area()); // Print the area of the rectangle

        Shape circle = new Circle(2); // Create a Circle object
        System.out.println("Area of circle: " + circle.area()); // Print the area of the circle
    }
}
Output

Area of rectangle: 6.0
Area of circle: 12.56

Leave a Reply

Your email address will not be published. Required fields are marked *