• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method Overriding

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I was asked in an Interview

How to Override the Super class Method forcibly in sub class? mean to say is there any situation whre we must Override the method in sub class (Access visibility of the method is not an issue here)

Thanks
Srinivas
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about, if the super class is abstract?
 
Srinivas Katta
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YeahI answered this but he did not agree with me. I feel if this situation can be really encountered in a Real time. I am not sure
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Srinivas Katta wrote:YeahI answered this but he did not agree with me. I feel if this situation can be really encountered in a Real time. I am not sure



What are you talking about?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Making the class abstract doesn't force you to override a method, but making a method abstract does -- at least, if you're trying to create a non-abstract, instantiable class.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:Making the class abstract doesn't force you to override a method, but making a method abstract does -- at least, if you're trying to create a non-abstract, instantiable class.



Correct, I made a mistake there!
 
Srinivas Katta
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, That is true, Did you see any other situations for Forcibly overriding

Thanks a lot
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Srinivas Katta wrote:Yes, That is true, Did you see any other situations for Forcibly overriding


What about implementing interface? But it's not a class.
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what you could do, if you still want to instantiate that particular class, is set its accessibility to default, and extend two classes from it; A public final one, and a public abstract one that overrides the method you want overriden with an abstract method.

That way you ensure that no one outside the package can access your superclass, but it can Access the public final one that can't be subclassed, or it can access the public abstract one, which you can subclass, IF you override that method you want overriden.
 
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dieter, i am confused

Confusing interviewer is fine.
But could you please clarify with sample code?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what Dieter is saying (I hope ):


 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah.. I understand.

But how does it makes difference in normal overriding functionality discussed above?
I dont find anything special, like child class must have to over ride.
It is again child class wish to over ride the method or declare abstract.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's an interview question. Interview questions are designed to see how you handle problems, not to find out what your actual solutions are. The first response I would have made would be to ask what "forcibly" means.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The example I posted forces classes outside of package A to use the final version of the class or to extend the class and implement the method.
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic