Vous êtes sur la page 1sur 4

5/6/13

Marker interface and class extension (Beginning Java forum at JavaRanch)

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Java Beginning Java

Author
nitinram agarwal Ranch Hand Joined: Jan 29, 2009 Posts: 77

Marker interface and class extension


posted 4/25/2013 8:33:20 PM

Hello, I would like to know some details 1. Has anyone used own marker interface for design? 2. If I don't want a class to be subclassed , I can think of the following options a. declare the class as final and/or b. declare the constructor as private. I would like to know if there is any alternate way of stopping a class from being subclassed? Regards,

Mansukhdeep Thind Ranch Hand Joined: Jul 27, 2010 Posts: 1045
I like...

posted 4/25/2013 8:46:19 PM

nitinram agarwal wrote:

Hello, I would like to know some details 1. Has anyone used own marker interface for design?
1/4

www.coderanch.com/t/610331/java/java/Marker-interface-class-extension

5/6/13

Marker interface and class extension (Beginning Java forum at JavaRanch)

What exactly are you asking here?

nitinram agarwal wrote:

2. If I don't want a class to be subclassed , I can think of the following options a. declare the class as final and/or

That is correct.

nitinram agarwal wrote:

b. declare the constructor as private.

How does declaring a constructor of a class private prevent it from being subclassed?
~ Mansukh Winston Gutkowski Bartender Joined: Mar 17, 2011 Posts: 4657

posted 4/25/2013 8:50:28 PM


Mansukhdeep Thind wrote:

How does declaring a constructor of a class private prevent it from being subclassed?

7
I like...

Think about it. What can you do with a class that has only private constructors? Winston

Isn't it funny how there's always time and money enough to do it WRONG?

Jeff Verdegan Bartender Joined: Jan 03, 2004 Posts: 5631

posted 4/25/2013 8:56:29 PM


nitinram agarwal wrote:

2. If I don't want a class to be subclassed , I can think of the following options a. declare the class as final and/or

5
I like...

b. declare the constructor as private.

If you don't want a class to be subclassed, the right way to do it is to declare the class final. That's what final is for. Making your code clear and easy to understand is extremely important.
www.coderanch.com/t/610331/java/java/Marker-interface-class-extension 2/4

5/6/13

Marker interface and class extension (Beginning Java forum at JavaRanch)

There may be another way beside the above, but I can't think of one off the top of my head, and it's something I will never, ever need to to know. If you're being asked for a job interview, then this is a horrible question, unless the interviewer is looking for "There may be another way, but I don't know it, and it will never come up." If you're being asked for school, then the "right answer" in this case is whatever your teacher fed you earlier in the course, but just note that it's not the right answer in the real world.

Winston Gutkowski Bartender Joined: Mar 17, 2011 Posts: 4657

posted 4/25/2013 8:59:56 PM


nitinram agarwal wrote:

Hello, I would like to know some details 1. Has anyone used own marker interface for design?

7
I like...

Yes, but I suspect now that it wasn't such a great idea. Personally, I think that one place it might have been used is for making Exceptions "checked" - but that boat sailed long ago.

2. If I don't want a class to be subclassed , I can think of the following options a. declare the class as final and/or b. declare the constructor as private.

The only thing I would add there is the qualifier "all", ie: b. declare ALL constructors as private.

I would like to know if there is any alternate way of stopping a class from being subclassed?

At all? Short of throwing an exception in every contructor if t h i sis not the class in question, which is kind of silly: No. There are a few ways of restricting subclassing though. Winston
Mansukhdeep Thind Ranch Hand Joined: Jul 27, 2010 Posts: 1045
I like...

posted 4/25/2013 9:11:18 PM

Yes Winston. Even if a single constructor is public, we can sub class the parent.

www.coderanch.com/t/610331/java/java/Marker-interface-class-extension

3/4

5/6/13

Marker interface and class extension (Beginning Java forum at JavaRanch)

I agree. Here's the link: http://aspose.com/file-tools

subject: Marker interface and class extension

Similar Threads Is it possible to extend ActionServlet doubt in serializatiion Marker Interface doubt When to use @annotation instead of marker interface declarations marker interface
All times above are in your local time zone & format.T he current ranch time (not your local time) is May 06, 2013 01:40:36 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/610331/java/java/Marker-interface-class-extension

4/4

Vous aimerez peut-être aussi