This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

replacement of "static abstrac" method?

 
Xiaoquan Wu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A is a class, AA is a subclass of A, AAA and AAB are subclasses of AA.

The important thing to me is the hierarchical relationships of the classes, so when I use AAA , I just want it gives me a static instance with type of AAA while does not create a new instance.
I add an static method "get()"in class AAA, and the same with AAB.
To let every subclass of AA comply with such a convention, I need to define an static abstract method "get()" in class of AA. But java does not allow me to do it.

So how can I organize the classes and the methods?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't add such a constraint to static methods. Abstract methods are meant to be overridden, and there is no overriding with static methods.

But it wouldn't make sense to require static methods, unless you are using reflection. Without reflection you need the class name to call the static method.
 
Bharath Raja
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Xiaoquan Wu wrote:
need to define an static abstract method "get()" in class of AA. But java does not allow me to do it.

abstract modifier cannot co-ordinate with static modifier.
Xiaoquan Wu wrote:
A is a class, AA is a subclass of A, AAA and AAB are subclasses of AA.
So how can I organize the classes and the methods?

As per my understanding, you want to have a one generic method in class AA and, you need to override it in another classes... Am I right....
Because of your class 'AAA' and 'AAB' are subclasses of class 'AA', you can create abstract method in your class 'AA' and you can override them in both subclasses in different way. and...

welcome to the Java Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic