• 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

Which eat method and why ??

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please examine the following code : -

public class Animal111 extends Horse{
public static void main(String[] args) {
Animal111 a = new Animal111();
Horse b= new Animal111();
a.eat();
b.eat();
}
public void eat(){
System.out.println("Inside Animal");
}

}

class Horse{
public void eat(){
System.out.println("I am not being used anytime anywhere");
System.out.println("Inside Horse");
}
}

The output of the foll program is : -

Inside Animal
Inside Animal

My question is that if " eat() " inside Horse class is never used, why I am getting compile time error while commenting out the " eat () " of Horse class i.e

public class Animal111 extends Horse{
public static void main(String[] args) {
Animal111 a = new Animal111();
Horse b= new Animal111();
a.eat();
b.eat();
}
public void eat(){
System.out.println("Inside Animal");
}

}

class Horse{
//public void eat(){
//System.out.println("I am not being used anytime anywhere");
//System.out.println("Inside Horse");
//}
}

The above code gives compile time error at " b.eat(); "
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My question is that if " eat() " inside Horse class is never used, why I am getting compile time error while commenting out the " eat () " of Horse class i.e



How does the compiler know that the eat() method, of the horse class is never used? What if there is another class, that inherits from the horse class, that doesn't override the horse method?

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The above code gives compile time error at " b.eat(); "



But to answer your question, if a horse doesn't have the eat method, and Animal111 does, then it is not overriding. Basically, a horse doesn't eat, and it is an action of animal111 horse type. Since the compiler can't tell that the horse is a type of horse that eats, it will complain. Afterall, without the method, not all horses eat.

Henry
 
sreeprasasd govindankutty
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Henry,

Could you kindly elaborate on that little more..
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sreeprasad,
You are getting complier error at b.eat() when you comment out the eat()method in Horse because b is a Horse reference to an Animal111 object.
When you say
Horse b= new Animal111();
the complier looks only at the reference type only and not of the object type.So when you comment out eat() code in Horse class the compiler complains though it is an Animal111 object at runtime invoking Animal111 overridden methods.The compiler will allow only methods in class Horse to be invoked when using a reference to an Horse.

I hope I helped you understand.
 
sreeprasasd govindankutty
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Swathy,

Thank you for replying. Yes, now it makes sense !!
 
reply
    Bookmark Topic Watch Topic
  • New Topic