File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding" Watch "overriding" New topic
Author

overriding

sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118
Hi All

Please explain me the following lines from K&B page No 103

even if you added an eat()
method to Horse, it wouldn't be an override of Animal's eat() method.

public class TestAnimals {
public static void main (String [] args) {
Horse h = new Horse();
h.eat(); // Not legal because Horse didn't inherit eat()
}
}
class Animal {
private void eat() {
System.out.println("Generic Animal Eating Generically");
}
}
class Horse extends Animal { }

Why it is not overriding when we add aeat method to horse class???

Thanks in advance
Suma
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Because 'Animal.eat' method is declared as 'private' class making it visible only inside the 'Animal' class. When you add a eat method to 'Horse' class, it becomes a new method inside 'Horse' class and hence not overriding.
Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
Hi sumaraghavi,

remember this : Only inherited methods can be overriden. So as private methods can never been inherited, so they're not overriden

hope this will help


SCJP 5 (76%)
SCWCD 5 (86%)
SCBCD 5(70%)
--------------------
"The greatest glory in living lies not in never falling, but in raising every time we fall.".. Nelson Mandela
sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118
Thanks so much
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding
 
Similar Threads
Whether the derived class method can throw a exception while overriding?
overloaded method call rules
Overloaded question
chapter 2 Object orienation polymorphism
Polymorphism in Overloaded and Overridden Methods