Extending class with enum - java

Lets say i have two classes A and B, which have theirs internal state represented by enum State. Class B extends A to add new functionality, but it also adds new internal state. To be precise, it adds fine-grained state support, by dividing one of the A`s state into two new states.

I also need to use PropertyChangeSupport to monitor state changes.

Currently I have all states defined in class A in enum State. I have protected methods setState() and getState(). To check on state from the outside, I wrote methods isAvaible(), isComplete(), etc. These methods are present only in appropriate class, depending on their hierarchy and state support.

I realy dont like the idea, having all states defined in parent class. How do I implement this correctly in Java? As far as I know, it`s not possible to use inheritance with enums.

Answers


Delegation is preferred to inheritance.

enum StateA
{
       AVAILABLE , COMPLETE ;
}

class A
{
      private StateA state ;
}

enum StateB
{
     AVAILABLE_TO_PUBLIC ( StateA . AVAILABLE ) , AVAILABLE_INTERNALLY ( StateA . AVAILABLE ) , COMPLETE ( COMPLETE ) ;

     StateB ( StateA sa )
     {
          this . sa = sa ;
     }

     private StateA sa ;
}

class B
{
      private StateB state ;
}

Need Your Help

What's wrong with my MySQL syntax?

mysql syntax

I've followed the syntax guide for creating tables exactly how it should be but I keep getting a syntax error. Can someone help me out?

Cannot change version of project facet Dynamic Web Module to 3.0?

java eclipse maven

I am using maven to create a dynamic webapp in Eclipse. I added some folders like src/test/java and src/test/resources. Also I changed the library in Java Build Path to obtain the JavaSE-1.7. It's ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.