• Post Reply Bookmark Topic Watch Topic
  • New Topic

Marker interface and class extension  RSS feed

 
nitinram agarwal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nitinram agarwal wrote:Hello,
I would like to know some details
1. Has anyone used own marker interface for design?


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?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:How does declaring a constructor of a class private prevent it from being subclassed?

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

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
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.

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
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nitinram agarwal wrote:Hello,
I would like to know some details
1. Has anyone used own marker interface for design?

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 this is 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
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Winston. Even if a single constructor is public, we can sub class the parent.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!