Win a copy of Functional Design and Architecture this week in the Functional programming forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Overloading methods?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

One small doubt is that when we overload a method is a subclass and suppose the method in the superclass throws a checked exception, does the method in the subclass also have to throw it??

Help me ranchers.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jothi Shankar,

There are two cases,

Case 1: When you Override a method in child class...
import java.io.*;

class Parent{

public void m1() throws IOException {}

}

class Child extends Parent{
public void m1() {} // You no need to handle any exception (or) not suppose to declare any exception wider than IOException
}

------------------------------------------------------------------------
Case 2: When you Overload a method in child class...
import java.io.*;

class Parent{

public void m1() throws IOException {}

}

class Child extends Parent{
public void m1(int i) {} // You can leave it blank (or) You can declare any exception wider than IOException
}


Hope this will be useful for you...
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An overloaded method in the subclass is free to throw any exceptions
(because it is a new method altogether with the same name)
But an overriding method should not throw any execptions other than that
declared by the superclass's method
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would just like to add a small point to what annapurna said , When you override a method, you can throw only the exceptions that have been specified in the base-class version of the method. This is a useful restriction, since it means that code that works with the base class will automatically work with any object derived from the base class (a fundamental OOP concept, of course), including exceptions.

It�s useful to realize that although exception specifications are enforced by the compiler during inheritance, the exception specifications are not part of the type of a method, which comprises only the method name and argument types. Therefore, you cannot overload methods based on exception specifications. In addition, just because an exception specification exists in a base-class version of a method doesn�t mean that it must exist in the derived-class version of the method. This is quite different from inheritance rules, where a method in the base class must also exist in the derived class. Put another way, the �exception specification interface� for a particular method may narrow during inheritance and overriding, but it may not widen�this is precisely the opposite of the rule for the class interface during inheritance
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is about overloading, not overriding. Why are we discussing overriding? Please keep to the topic. If you want to discuss overriding methods and exceptions, then please start a new topic.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, to be clear: an overloading method does not have to throw the same exceptions as the overloaded method. It does not matter if the overloaded method is in the same class as the overloading method or the overloaded method is inherited by the overloading method's class. Writing a simple program should convince you of this.

Whether or not it is practical to have overloaded methods with different sets of throws clauses is a different matter. Think about the calling code that has to be written when calling several of these overloaded methods with different throws clauses. The try/catch stuff can get rather messy.
[ December 12, 2006: Message edited by: Barry Gaunt ]
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried it Barry and that helped me. Thanks anyways for the explanation and thanks other ranchers who also brushed up a little on overriding as well.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arpit Khandelwal:
I would just like to add a small point to what annapurna said , When you override a method, you can throw only the exceptions that have been specified in the base-class version of the method. This is a useful restriction, since it means that code that works with the base class will automatically work with any object derived from the base class (a fundamental OOP concept, of course), including exceptions.

It�s useful to realize that although exception specifications are enforced by the compiler during inheritance, the exception specifications are not part of the type of a method, which comprises only the method name and argument types. Therefore, you cannot overload methods based on exception specifications. In addition, just because an exception specification exists in a base-class version of a method doesn�t mean that it must exist in the derived-class version of the method. This is quite different from inheritance rules, where a method in the base class must also exist in the derived class. Put another way, the �exception specification interface� for a particular method may narrow during inheritance and overriding, but it may not widen�this is precisely the opposite of the rule for the class interface during inheritance




Interesting - but have I not seen exactly some of that prose before in a book somewhere?

Bruce...?
[ December 12, 2006: Message edited by: Barry Gaunt ]
 
Arpit Khandelwal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
------------------------------------------------
Interesting - but have I not seen exactly some of that prose before in a book somewhere?

Bruce...?
-----------------------------------------------

Hello Mr.Gaunt,

I appreciate your observation skills
.
After reading the question even i was little confused so i thought its better to give a fact than an assumption. Thats why you find my answer as a probably read article.
But i guess the main objective of this forum is to give the concrete solution and thats what i did.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then please quote exactly from where you cut and paste your information. If you found in in Bruce Eckel's Thinking in Java, then state that fact, and not present the information as if it is your own contribution. Please help keep up JavaRanch's reputation for a professional approach to certification and learning. Only if we do this will we continue to have reputable authors contributing to this site.
 
reply
    Bookmark Topic Watch Topic
  • New Topic