This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Method Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Method Overriding" Watch "Method Overriding" New topic
Author

Method Overriding

Srinivas Katta
Ranch Hand

Joined: Feb 01, 2007
Posts: 76
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
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

What about, if the super class is abstract?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Srinivas Katta
Ranch Hand

Joined: Feb 01, 2007
Posts: 76
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

Joined: Nov 04, 2009
Posts: 2066

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?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Feb 01, 2007
Posts: 76
Yes, That is true, Did you see any other situations for Forcibly overriding

Thanks a lot
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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.
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 503
    
    4

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.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

Dieter, i am confused

Confusing interviewer is fine.
But could you please clarify with sample code?

SCJP 5, SCWCD 5
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

This is what Dieter is saying (I hope ):



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
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
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Overriding
 
Similar Threads
Private+Final
Doubt in equals()
may be a dumb question
Protected or public
Help in statics