Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java allows to declare abstract methods in an enum type but it's a final class not abstract class

Tags:

java

in java, all enum types that we created are final classes that inherits Enum class. so why does java allow us to declare an abstract method in our enum types ? because an enum type is a final class, and java doesn't allow us to declare an abstract method in final classes.

Thanks.

like image 742
1 JustOnly 1 Avatar asked Mar 07 '23 13:03

1 JustOnly 1


1 Answers

You are not expected to extend them, but you could certainly implement an abstract method many times:

public enum Animal {

    COW {
        public String talk() {
            return "moo";
        }
    },
    FROG {
        public String talk() {
            return "croak";
        }
    };

    public abstract String talk();
}
like image 87
Idos Avatar answered Mar 25 '23 22:03

Idos