File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding" Watch "overriding" New topic


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();; // 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
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Because '' 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:
subject: overriding
It's not a secret anymore!