File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Force to override method in subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Force to override method in subclass" Watch "Force to override method in subclass" New topic

Force to override method in subclass

Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

Can I have concrete class and force all the subclasses of this class to override a method declared in this concrete class?

Java Experience
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479

I mean, I guess you can test the name of the actual class at run time, and, in your concrete class method, throw an exception if it's a subclass. It sounds hideous, but I suppose that would work. Be aware of the performance hit you will take by executing instanceof every time you run the method.

Can I ask why you want to do this?

If the class is concrete, then you are able to instantiate it; if you do that, the method needs to work.

It sounds perhaps like you want any class that subclasses this one to be forced to override this method instead of using the subclass method. I haven't figured out any other effect this would have. But why do you want to do this? What real-world application allows inheritance but forces a method override?

Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

Hi Ralph,

Thanks for replying man.

This is asked by one of the interviewer today to me.

I also don't know the logic of asking this. Are they falling short of interview questions from Google or what!!

Your suggestion is perfect..

I have written sample code with your suggestion but I need to check for each sub class and can't have a generic if condition because instanceof works with super types also...

I am talking about line1 in case I have two sub classes say Test1 and Test2 and want both to override the test method then I need to check for "this instancdof Test2" also and can't use "!(this instanceof Test)"

The output is:

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46360
Sunny Bhandari wrote: . . .

. . .
No! No! That won't work at all. The superclass cannot know the type of the subclass. What will happen in Test2?

You are going to have to think again.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

I would go back to the interviewer and ask what they mean by 'force'. Regardless of what you do in your code, I don't think that would FORCE anyone to do anything. Sure, it might throw an exception, but you haven't forced them in the same way they are forced to implement every method in an interface.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: Force to override method in subclass
It's not a secret anymore!