Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enums and Template Methods

Tags:

java

enums

Enum is basically a special class type, and can have methods and fields just like any other class. Any one know about the Enums Template Methods. Please give a real example for Template Methods on Enums. And can you explain about Enum Reverse Lookups.

like image 382
Kushan Avatar asked Feb 28 '26 18:02

Kushan


1 Answers

Java 5.0 Enum tricks, specially have a look on the video.

Here is a simple example of a "command" enumeration:

public enum Toy {
     DOLL() {
          @Override public void execute() { 
               System.out.println("I'm a doll."); 
          }
     },
     SOLDIER() {
          @Override public void execute() { 
               System.out.println("I'm a soldier."); 
          }
     };
     //template method
     public abstract void execute();
}

Here objects Doll and Soldier both have a different implementation of the function execute().

like image 120
Amit Avatar answered Mar 03 '26 08:03

Amit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!