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?
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)"
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