• 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

overriding

 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which are true about overiding
both overiding and overridden methods must have same arg and name and returntypes//t
overiding method must not limit access more than the ridden//t
overiding must not throw any exceptions that may not be thrown by overidden method//f it can throw RuntimeException??
overiding method may not be private//t
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"overiding must not throw any exceptions that may not be thrown by overidden method"
True. A RuntimeException may be thrown by the overridden method, and also by the overriding method. The statement agrees with this.
The other answers are correct.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question for you Jim......
How is the last statement true???
Shouldn't it be false??? An overriding method CAN be private as long as the overridden method is private too???
class Child{
private void method1(){
System.out.println("From Parent");
}
}
public class Child2 extends Child{
private void method1(){
System.out.println("From Child");
}
public static void main(String args[]){
Child2 obj = new Child2();
obj.method1();
}
}
This compiles and works fine...
Thanks.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
U can't access private methods in the sub class, so no point of overriding them.
Sushma
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A private method is not visible outside the class and for all practical purposes does not exist outside the class. So when a method with the same (or different) signature is defined in the derived class it is a new method which is in no way connected with the original method in the parent class.
Hope this helps!!
Regards,
Milind

[This message has been edited by Milind (edited May 30, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shan & Sushma,
I just went through these questions myself last week.
Please spend the time to review your access modifiers & modifiers.
How they can affect: class's, method's, & variables.
Once you get modifiers down...
Overloading and overriding will make alot more sense.
Enjoy the Day,
ttfn, Monty6


[This message has been edited by monty6 (edited May 30, 2000).]
 
Shafeeq Sheikh
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sushma and Milind.....
Agree with both of you'll but as far as the precisely worded statement goes it will be false.... right???
Practically speaking an 'overridden private method' would not make sense but the compiler allows it and there are no runtime errors.
If we get a question like this in the Exam what should the answer be???
Thanks....
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shafeeq,
There is no such thing as "Overriden Private Method". When you declare a method in a sub class that has the same name as a private method in the base class, the compiler will not complain because it does not consider this as an attempt to override the original method, rather just as a re-declaration.So in this case the issue of overriding does not arise, hence the final answer holds.Note that the compiler does not disallow naming methods with the same name as in other classes if the classes are not related, if they are related then the rules governing overriding must apply.
reply
    Bookmark Topic Watch Topic
  • New Topic