Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 2562
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2562
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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





 
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic