File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Overriding method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overriding method" Watch "Overriding method" New topic
Author

Overriding method

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
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
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Gitahi Ng'ang'a
Greenhorn

Joined: Oct 30, 2009
Posts: 8
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

Joined: Jul 23, 2008
Posts: 352
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


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
But welcome to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding method