• 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

method invocation problem

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This below code is doing valid override of eat method . I just want to understand what is the problem with invoking the superclass version of method using the superclass object itself.

class Animal{
public void eat() throws Exception
{


}

}
public class dog extends Animal {
public void eat(){

}
public static void main(){

Animal d1=new Animal();
d1.eat(); //this does not work ....why?

Animal d2= new dog();
d2.eat(); //this does not work either.....why?

}
}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is it about this code that doesn't work (aside from your main method lacking an argument and failing to declare or catch the possible exception)?

What are you expecting from these methods?
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At first you must handle or declare the exception which throws by method in super class.

and

with out using String[] a in main method it compiles but JVM says that NoSuchMethodError.


 
Ravi Bansal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i m sorry ......i made a typo while writing main method(missed String args)........

another point....why should i handle an exception in the subclass ..if i m invoking the superclass version of method by using the superclass referece variable which is pointing to superclass object itself?
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because super class method is throwing an exception so you must handle or declare it in main method from which you are calling super class method
[ November 07, 2008: Message edited by: Ganeshkumar cheekati ]
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well you will have to handle it because you are using the super class reference to call the method. So at compile time the compiler will look at the method in the super class.

I have modified your code a bit



Now you might be saying that the method eat in class Animal actually doesn't throws any exception. But in it's throws clause it has a CHECKED exception (i.e. Exception). So it will force you to handle that exception whether it is actually being thrown or not. This is why call 1 will not work

Second thing about call 1. The actual method that will be called will be eat of dog class. And eat in dog doesn't thrown any exception. But the reference used to invoke eat is of type Animal. So the compiler will check the code of eat in Animal class and not dog class. Since it will find throws Exception there, it will force you to handle it.

call 2 will work as you are using dog reference to call it. Since the eat method in dog class doesn't throws any exception, so the call would be just fine and the compiler will not force you to handle any exception...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic