aspose file tools*
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: 3953
    
  17

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: 36514
    
  16
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: 9280
    
  17

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: 3169
    
  10
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: 9280
    
  17

ya thanks...I forgot that....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not to Inherit
 
Similar Threads
Commons ConfigurationException
How we get inheritence wih interfaces
Inheritance
Tricky inheritance question
WA #1.....word association