Java enum Inheritance and Interface

enum-string

What is Java enums?

In Java, an enumeration is a special kind of class that represents a fixed set of values. Each value in an enum is called an enumeration constant, and it’s defined as a static final instance of the enum type. Enums are used to define a fixed set of values that a variable of that type can take on.

  • Enums are inherently final classes and cannot be subclassed. They cannot extend other enums or classes.

To know more about Java enum in java read the complete article.

Java enum inheritance and Interface:

In Java, enums are a special kind of class that represent a fixed set of values, and they cannot be inherited by another class in the traditional sense. This means that an enum cannot extend another class. However, enum class can inherit another enum class.

In Java, an interface is a set of method signatures that a class can implement. An enum, just like any other class, can implement one or more interfaces. When an enum implements an interface, it must provide an implementation for all of the interface’s methods. The enum can also define additional methods of its own.

Let’s look at a Java enum where the Java enum constructor is used to perform an operation on the given code.

Example 1 : Inheriting a Class from an enum Class

Run
enum ParentEnum {
    CONSTANT1, CONSTANT2, CONSTANT3;
}

 enum ChildEnum {
    CONSTANT4(ParentEnum.CONSTANT1), CONSTANT5(ParentEnum.CONSTANT2);

    private final ParentEnum parentEnum;

    ChildEnum(ParentEnum parentEnum) {
        this.parentEnum = parentEnum;
    }

    public ParentEnum getParentEnum() {
        return parentEnum;
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("Parent Enum constants: ");
        for (ParentEnum constant : ParentEnum.values()) {
            System.out.println(constant);
        }
        System.out.println("Child Enum constants: ");
        for (ChildEnum constant : ChildEnum.values()) {
            System.out.println(constant + " - Parent Enum : " + constant.getParentEnum());
        }
    }
}

Output

Parent Enum constants: 
CONSTANT1
CONSTANT2
CONSTANT3
Child Enum constants: 
CONSTANT4 - Parent Enum : CONSTANT1
CONSTANT5 - Parent Enum : CONSTANT2

Example 2 : Java enum interface Method

Run
public interface Shape {
    double getArea();
}
enum Square implements Shape {
    SMALL(1), MEDIUM(2), LARGE(3);
    private double side;
    Square(double side) {
        this.side = side;
    }
    public double getArea() {
        return side * side;
    }
}
public class Main {
    public static void main(String[] args) {
        Shape shape = Square.SMALL;
        System.out.println("Area of square: " + shape.getArea());
    }
}

Output

Area of square: 1.0

Example 3: Java enum inheritence

Run

public interface Shape {
    double getArea();
}

public enum Square implements Shape {
    SMALL(1), MEDIUM(2), LARGE(3);

    private double side;

    Square(double side) {
        this.side = side;
    }

    public double getArea() {
        return side * side;
    }
}

public enum Circle implements Shape {
    SMALL(1), MEDIUM(2), LARGE(3);

    private double radius;
    private final double PI = 3.14;

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

    public double getArea() {
        return PI * radius * radius;
    }
}

public class Main {
    public static void main(String[] args) {
        Shape[] shapes = new Shape[]{Square.SMALL, Square.MEDIUM, Square.LARGE, Circle.SMALL, Circle.MEDIUM, Circle.LARGE};
        for (Shape shape : shapes) {
            System.out.println(shape + " area: " + shape.getArea());
        }
    }
}

Output

SMALL area: 1.0
MEDIUM area: 4.0
LARGE area: 9.0
SMALL area: 3.14
MEDIUM area: 12.56
LARGE area: 28.26

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription