• 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

overriden method

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

K&B book, chapter 2 and pg. 104, one exam watch is present. Code
snippet is given below

class Animal
{
public void eat() throws Exception{
System.out.println("I am in Animal Class");
}

}

public class Dog2 extends Animal{
public void eat(){
System.out.println("Dog version of the class");
}

public static void main(String[] args) {
Animal a = new Dog2();
Dog2 d = new Dog2();
d.eat();
a1.eat();
}
}

Here it says the program won't compile because of the following line

a1.eat()

On other hand d.eat() will run fine. Though Dog was extending Animal
and didn't declare the exception but it still ran fine. What's the reason
of d.eat() running fine and not a.eat(). Please clarify.

Ben
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When overriding you can declare the method to be throwing the same or narrower exceptions or avoid declaring any exception (which you think subclass might not throw) declared in super class.

If you want to exceute a.eat() then you should catch the exception or declared to be thrown in the main. b.eat() work fine with your code since Dog class' method not throwing any checked exceptions.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you know overriding concept then at compile time, compiler will check whether class Animal has eat method or not. Now at this time it will find that eat method throws Exception so it will look for try-catch block. If it does not find try-catch block... compiler error will occur.


d.eat() will run fine because d is a Dog2 type variable so compiler will check eat method in Dog2 class and this method is not throwing any checked exception.
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If it does not find try-catch block... compiler error will occur.



A small modification....

If it does not find try-catch block or there is no propgation of Exception...compiler error will occur.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object type determines which overriden method is to be invoked at runtime. Though the code invokes Dog2's eat()..the only reason why your getting a compiler error is because the Superclass Animal has a method eat() which is overriden and throws and Exception ..although we are now invoking the Animal class eat() we need to handle or declare the Exception thrown by it.
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


although we are now invoking the Animal class eat() we need to handle or declare the Exception thrown by it.



I beg your pardon if I misunderstood your statement... Butwe are not invoking Animal`s eat method in any case.
 
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, we are not calling Animal.eat() but there is a compile-time check since we call a.eat() where 'a' is a reference variable of type Animal.
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Animal a = new Dog2();
Dog2 d = new Dog2();
d.eat();
a1.eat();



I think you declared the variable as "a" but you are using the variable "a1" to call the method.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope that it is a.eat() and not a1.eat(). That seems to be typing mistake.

If your program would have been like this then there won't exist any compile time error.

class Animal
{
public void eat() throws RuntimeException{
System.out.println("I am in Animal Class");
}

}

public class Dog2 extends Animal{
public void eat(){
System.out.println("Dog version of the class");
}

public static void main(String[] args) {
Animal a = new Dog2();
Dog2 d = new Dog2();
d.eat();
a.eat();
}
}

Reason being RuntimeException is unchecked exception and compiler does not enforce handling for this. Whereas If it is checked exception ( Exception class in original program ).Compiler enforce handling for same.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just throwing in another opinion that may help:

In the case of



polymorphism is not in use so there is no decision for the run-time to make. You declared a Dog2 reference and used it to call a Dog2 method.

In the case of



this is a polymorphic reference. A superclass type reference is used and while it is CURRENTLY pointing to a Dog2 it COULD just as well point to an Animal which would cause the Animal version of the eat() method to be called. So, even though a is currently pointing to a dog, I suspect that the compiler will want you to handle the exception that the Animal version might throw so that it could, indeed, hold either an Animal or a Dog2 at run-time and run safely.
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

although we are now invoking the Animal class eat() we need to handle or declare the Exception thrown by it.


Well, the word was NOT instead of NOW...
 
reply
    Bookmark Topic Watch Topic
  • New Topic