• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it fine for many classes to extend a class which has only constructor and no method.  RSS feed

 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have many java classes which need to implement an interface (from the lib jar).The implementation of the interface method need some properties to be available. To make these properties available to these classes,I am extending a common base class which has only a constructor which sets all these properties. This base class has no methods, only this constructor.This works fine. I want to know whether this is a correct way of coding?

thanks.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's hard to say for sure without seeing actual code. Just judging from your description, however, I'm already detecting a smell of inappropriate behavior.  In particular, this:
The implementation of the interface method need some properties to be available. To make these properties available to these classes,I am extending a common base class which has only a constructor which sets all these properties.

That just doesn't sound right to me. Again, you need to show some code so we can get a better sense of whether or not it smells.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The implementation of the interface method need some properties to be available.

As Junilu said, this doesn't sound right. An interface only declares functions that the implementer must provide. It can have no variables.
It sounds like you don't need an interface at all - you just need your (abstract) base class that declares the pertinent variables.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the face of it I would have no problem with the setup.
The class as described IS more than just a constructor:  It is obviously defining the properties common to all subclasses - this is exactly what inheritance is for.
Using an interface and an abstract class together is a common design pattern for just this scenario.

On the other hand, you might want to consider if Inheritance is the best pattern to use.  It might be better off with composition. The "Is a" vs "Has a" relationship.
Again, that depends entirely on your use case.  The general guideline:  Is the following statement true? : [subclass] is a [parent class] ?
e.g. Dog is an Animal,  Square is a Shape - but 
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is as below:






}












The class MyBaseClass has nothing except for constructor which sets the variables to be used in classes A , B.
I is not an interface I have created. It comes from the lib jar.


thanks





 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is meaningless as we still have no idea what you are trying to model.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not only is it meaningless, it won't even compile. I can tell just from what you provided because none of the constructor calls in main() match up with any of the class declarations and the base class does not have a default constructor either.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that result is the function required for implementing the interface, your MyBaseClass could implement the interface and provided the implementation of result, since both of the subclasses you show implement it the same way. That way you could remove the implementations in the subclasses. The subclasses should include an override of that function only if hey compute the result differently than in the base class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!