• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Force to override method in subclass

 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I have concrete class and force all the subclasses of this class to override a method declared in this concrete class?
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

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?

rc
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Marshal
Posts: 76856
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic