This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Not to Inherit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Not to Inherit" Watch "Not to Inherit" New topic
Author

Not to Inherit

shruti patel
Ranch Hand

Joined: Mar 10, 2008
Posts: 52
Hi,
Is any to make class not to inherit other than using Final keyword
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You could make all the constructors private (then you would have to use static factory methods to get instances).


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43970
    
  33
But if you only have a private constructor you ought to mark the class "final" to prevent anybody trying to extend it.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
it took me a while to understand the question, so I rewrote it...

paraphrased from shruti patel:
Hi,
Is there any way to make a class not allow another class to inherit from it, other than using the final keyword?


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9404
    
  20

if you make all the constructors private, then no one can create instances of that class. so then you will also have to provide a factory method



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Originally posted by Ankit Garg:
if you make all the constructors private, then no one can create instances of that class. so then you will also have to provide a factory method




You need to make your factory method static and it should also return an instance of Final.

[ August 20, 2008: Message edited by: Joanne Neal ]
[ August 20, 2008: Message edited by: Joanne Neal ]

Joanne
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9404
    
  20

ya thanks...I forgot that....
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Not to Inherit
 
It's not a secret anymore!