Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding method

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Animal
{

public void eat() throws Exception
{

}
}


class Dog2 extends Animal
{

public void eat()
{

}
public static void main(String args[])

{
Animal a = new Dog2();
Dog2 d = new Dog2();
d.eat();
a.eat(); //COMPILER ERROR
}

}


The above gives me a compiler error. The rules for overriding say that an overriding method doesnt have to declare any exceptions that it will never throw regardless of what the overridden method declares. Is the above program an exception to this rule ? If so can someone please explain. Thanks.
 
Henry Wong
author
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote:The above gives me a compiler error. The rules for overriding say that an overriding method doesnt have to declare any exceptions that it will never throw regardless of what the overridden method declares. Is the above program an exception to this rule ? If so can someone please explain. Thanks.


This error has nothing to do with overridding. The error is caused by trying to call a method, with an Animal reference -- and not catching or declaring that the method will be thrown from the caller method.

Henry
 
Gitahi Ng'ang'a
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is merely aware that 'a' is an object reference of type Animal. It does not know that indeed 'a' is specifically an object of type Dog. Consequently, it enforces the arrest of the potential exception it knows the method eat() of type Animal can potentially throw. For 'd', the compiler is fully aware that not only is 'd', an Animal it is indeed specifically a Dog. Thus the call to eat() will invariably be the overriding one declared in class Dog, and the compiler is smart enough understand that no excetion has been specified as potentially capable of being thrown by eat() defined thus.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gitahi Ng'ang'a wrote:The compiler is merely aware that 'a' is an object reference of type Animal. It does not know that indeed 'a' is specifically an object of type Dog. Consequently, it enforces the arrest of the potential exception it knows the method eat() of type Animal can potentially throw. For 'd', the compiler is fully aware that not only is 'd', an Animal it is indeed specifically a Dog. Thus the call to eat() will invariably be the overriding one declared in class Dog, and the compiler is smart enough understand that no excetion has been specified as potentially capable of being thrown by eat() defined thus.


I think whilst your right, here your answer confuses things a bit, look at Henry's answer, its simply that you must either declare to throw or catch any exceptions. As public void eat() throws Exception is of that guise when you create an animal or call the Animal Eat method you must declare that the initialization code throws an exception or surround it with a try catch block
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But welcome to JavaRanch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic