Skip to main content

How to create your own enum type with the regular classes?

You will always have the joy of using enums, but have you ever thought that enums demonstrate the common characteristics of Object Oriented Design? I.e inheritance and abstraction. So what stops you from creating your own enum construct with the available constructs, i.e classes. I was just playing around with them and could build one.

Let's first get to know the characteristics of an enum.

Characterics of an enum

  • Enums cannot be instantiated with the 'new' keyword since the constructor is private.
  • Enums can contain only a finite set of states. i.e the value/s an Enum can contain should have been defined already.
  • Enums do behave as regular classes complying with the OOD features like inheritance, polymorphism, encapuslation and abstraction
  • The only exception I have noticed is that it does not allow generics, at least in Java 8.

Those above properties can be achieved with some already available constructs. Good news is additional benefits can also be gained; generics!. Since we make this custom enum type with available constructs we can include generics support. First I will show an example without generics.

  1. Abstract class has been used as the main construct for the LocaleEnum reference- this will prevent instantiation and at the same time allow us to define abstract methods inside it.
  2. Handles to get the Enum states (values) have been represented by public static methods.
  3. Each Enum state has been implemented as a static nested inner class which also inherits from the main enum class- 



                        Using the Enum in the client code as shown below.




       As evident, the LocaleEnum cannot be instantiated but assigned with a preset value. Also, the enum methods can be called just like of any other regular class. 


Further, class-level or method level type parameters can be used with freedom, giving a more flexibility. Below image shows an example of a class level type parameter being used.





      

    

Comments

Popular posts from this blog

Harnessing Computational Power used for Proof-of-Work in Block Chains

Harnessing Computational Power used for Proof-of-Work in Block Chains I t's well known that proof-of-work carried out by miners to add a new block to the block chain requires lot of computation power. This power is used to find a hash value matching to some random value. The purpose is to make the block adding process exhaustive such that fraudulent behavior will be discouraged. I was wondering , perhaps in the future, that there will be a motivation to adapt a new kind of proof-of-work such that, the miner will be required to carry out a useful X amount of data processing tasks and provide the proof. Such a trend would open up the opportunity for the organizations/individuals to outsource their data processing tasks and issue a token of task completion? perhaps. An example to simply this idea is, those organizations wish to get their data processed, publish to a public queue and the miners will pull the jobs off to execute them, which would ultimately be published to a pr...

SHORT TECH NOTE | Are you using 'FInal' keyword in the right way?

 'Final' keyword is a powerful modifier used in Java and in other OOP languages to denote that a given construct is no more extensible. Rather than a mere keyword, I would argue it's a 'key trade off'  decision, by looking at the future and arriving at the conclusion  whether we should really allow further extensibility in a given construct or limiting it.  The final keyword is applied at 3 levels; at class level, at method level and at field level. It's a simple decision to decide whether or not one should apply the 'final' keyword at method or field level, so in this short note, I am focusing on the 'final' at the class level.  By using the 'final' keyword in front of a class, you are essentially sealing the class off and preventing any further 'extensibility' of it, violating one of the SOLID principles, 'open to extension and closed to modification'. This violation means that you cannot create a subclass to add the new...