• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Enforce single level inheritance on Base Class

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

I have an abstract class for which I want to allow only single level of inheritance.
For e.g:




Here I do not want any other class to extend SpecialX. Can I do anything to BaseX to enforce this single level inheritance? I do not have any control over SpecialX so putting restriction on this wont be a solution.

Thanks
 
Sheriff
Posts: 67637
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
No.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is rather contrived, and I fully expect someone to tell me why it doesn't work in all scenarios.I do realize and acknowledge that this would be a terrible design.
 
Prasad prap
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do you mean by "not working in all scenarios"? Can you clarify a bit?
 
Master Rancher
Posts: 4198
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a way to do something kind of similar, that may be what you want: you can prevent anyone from ever extending your classes, except the classes that you have defined in the same file - by using nested classes. Here's how:

The private constructor in BaseX prevents other classes from successfully extending the class (or at least, from extending and instantiating it). Except for other classes declared within the same top-level class.

Now, is this actually useful for anything? Dunno. Why do you want to place this limitation on a class, anyway?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad prap wrote:Here I do not want any other class to extend SpecialX.


Why not?

I think perhaps if you explained what you want, rather than how you want to do it, we might be able to offer more help.

Winston
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad prap wrote:what do you mean by "not working in all scenarios"? Can you clarify a bit?




Basically, Darryl's solution doesn't prevent someone from creating a subclass more than one level from your base class. What is does do is prevent someone from actually instantiating that class at runtime.

Will this work?

Henry


PS... although a terrible design, it is an interesting solution. Darryl, you get a cow...
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Basically, Darryl's solution doesn't prevent someone from creating a subclass more than one level from your base class.


In fact, the code posted does contain such a class (FinalSubSub).

And thank you for the cow!
 
Rancher
Posts: 1043
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A runtime "solution".


 
Mike Simmons
Master Rancher
Posts: 4198
57
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting. But consider:

You code allows instantiation of Sub, and prevents instantiation of SubSub. Good. However, it also prevents instantiation of Container, which should be allowed.
 
I am going to test your electrical conductivity with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic