Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > what are the other ways to prevent a class from being subclassed. one way is to declare the class final.

Reply
Thread Tools

what are the other ways to prevent a class from being subclassed. one way is to declare the class final.

 
 
srinivas.veeranki@gmail.com
Guest
Posts: n/a
 
      02-08-2006
Hi All,

what are the other ways to prevent a class from being subclassed. one
way is to declare the class final.


Thanks

Srinivas

 
Reply With Quote
 
 
 
 
NullBock
Guest
Posts: n/a
 
      02-08-2006
You could throw an exception in the ctor, if it's being called on a
derived class:

class NoExtend {
public NoExtend() {
if (this.getClass() != NoExtend.class) {
throw new RuntimeException();
}
}
}

Or you could have a private ctor, with a public factory method:

class NoExtend {
private NoExtend() {
}

public static NoExtend create() {
return new NoExtend();
}
}

Those are two methods that come to mind...

Walter

----
Walter Gildersleeve
Freiburg, Germany

__________________________________________________ ____
http://linkfrog.net
URL Shortening
Free and easy, small and green.

 
Reply With Quote
 
 
 
 
srinivas.veeranki@gmail.com
Guest
Posts: n/a
 
      02-08-2006
hi...

Thanking you very much for ur help..
Thanks..
Srinivas
NullBock wrote:
> You could throw an exception in the ctor, if it's being called on a
> derived class:
>
> class NoExtend {
> public NoExtend() {
> if (this.getClass() != NoExtend.class) {
> throw new RuntimeException();
> }
> }
> }
>
> Or you could have a private ctor, with a public factory method:
>
> class NoExtend {
> private NoExtend() {
> }
>
> public static NoExtend create() {
> return new NoExtend();
> }
> }
>
> Those are two methods that come to mind...
>
> Walter
>
> ----
> Walter Gildersleeve
> Freiburg, Germany
>
> __________________________________________________ ____
> http://linkfrog.net
> URL Shortening
> Free and easy, small and green.


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-08-2006
On 8 Feb 2006 01:04:12 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

>what are the other ways to prevent a class from being subclassed. one
>way is to declare the class final.


Why do you want to know another method? What is the matter with
declaring a class final?
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
EJP
Guest
Posts: n/a
 
      02-08-2006
(E-Mail Removed) wrote:

> Hi All,
>
> what are the other ways to prevent a class from being subclassed. one
> way is to declare the class final.


Declare its constructors as private.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-09-2006
On Wed, 08 Feb 2006 23:41:01 GMT, EJP
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>> what are the other ways to prevent a class from being subclassed. one
>> way is to declare the class final.

>
>Declare its constructors as private.


Doing that and not declaring the class final is what I call
"tantalising"
..
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-09-2006
On Thu, 09 Feb 2006 00:06:31 GMT, Roedy Green
<(E-Mail Removed) > wrote, quoted or
indirectly quoted someone who said :

>On Wed, 08 Feb 2006 23:41:01 GMT, EJP
><(E-Mail Removed)> wrote, quoted or indirectly quoted
>someone who said :
>
>>> what are the other ways to prevent a class from being subclassed. one
>>> way is to declare the class final.

>>
>>Declare its constructors as private.

>
>Doing that and not declaring the class final is what I call
>"tantalising"
>.

see http://mindprod.com/jgloss/tantalising.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Luc The Perverse
Guest
Posts: n/a
 
      02-09-2006
"Roedy Green" <(E-Mail Removed) > wrote in
message news:(E-Mail Removed)...
> On Thu, 09 Feb 2006 00:06:31 GMT, Roedy Green
> <(E-Mail Removed) > wrote, quoted or
> indirectly quoted someone who said :
>
>>On Wed, 08 Feb 2006 23:41:01 GMT, EJP
>><(E-Mail Removed)> wrote, quoted or indirectly quoted
>>someone who said :
>>
>>>> what are the other ways to prevent a class from being subclassed. one
>>>> way is to declare the class final.
>>>
>>>Declare its constructors as private.

>>
>>Doing that and not declaring the class final is what I call
>>"tantalising"
>>.

> see http://mindprod.com/jgloss/tantalising.html


hehehe.

Silly rabbit, tricks are for kids.

--
LTP




 
Reply With Quote
 
EJP
Guest
Posts: n/a
 
      02-09-2006
Roedy Green wrote:
> On Wed, 08 Feb 2006 23:41:01 GMT, EJP
> <(E-Mail Removed)> wrote, quoted or indirectly quoted
> someone who said :
>
>
>>>what are the other ways to prevent a class from being subclassed. one
>>>way is to declare the class final.

>>
>>Declare its constructors as private.

>
>
> Doing that and not declaring the class final is what I call
> "tantalising"


Not as tantalising as all that. You certainly don't have to wait until
'bebug time' to discover it as your glossary claims. The first
compilation will tell you. More generally you might declare the ctors as
package-private: would you describe that as 'tantalising'? inside a
sealed package? where you do draw the line?
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-09-2006
On Thu, 09 Feb 2006 05:51:59 GMT, EJP
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>Not as tantalising as all that. You certainly don't have to wait until
>'bebug time' to discover it as your glossary claims.



If it is an alternate implementation, you won't find out till run
time, or maybe never. The compile will be quite happy so long as your
method signatures you use match.

If it a totally unrelated class you will find out at compile time, and
you will likely be puzzled all the heck why it does not recognise your
methods.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can one declare more than one signal on one line? Merciadri Luca VHDL 4 11-01-2010 02:00 PM
best way to declare struct to be used in other c and c++ files Tammy C Programming 6 09-08-2008 03:56 AM
ways to declare empty set variable Sun Python 27 02-16-2008 10:12 PM
Is there a way to declare a reference as being a type implementing multiple interfaces? Oliver Wong Java 16 06-28-2006 05:22 PM
Different ways to declare methods/properties of an object Sergey Ilinsky Javascript 2 03-28-2005 03:51 PM



Advertisments