Académique Documents
Professionnel Documents
Culture Documents
Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Java FAQ
Recent Topics
Register / Login
Author
nitinram agarwal Ranch Hand Joined: Jan 29, 2009 Posts: 77
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...
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
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.
How does declaring a constructor of a class private prevent it from being subclassed?
~ Mansukh Winston Gutkowski Bartender Joined: Mar 17, 2011 Posts: 4657
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?
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...
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
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.
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...
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
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 .
www.coderanch.com/t/610331/java/java/Marker-interface-class-extension
4/4