• 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
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

method overloading and overriding from subclass

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. When a method overloads a method from its superclass I understand that the return type may be same or different. What is important is that the number and type of parameters be different.
IS THE ORDER OF PARAMETERS SIGNIFICANT AS WELL ?
ex:PARENT METHOD
public int modifyIt(int a, long b)
SUBCLASS METHOD
a.public long modifyIt(int a, long b)
b. public long modifyIt(long b, int a)
------------------
are either a or b valid ??, are these either overloading or overriding?
2. An overriding method of subclass can only throw THE CHECKED EXCEPTIONS (OR THEIR SUBCLSSES) thrown by the parent class method.
what about if this overriding method throws an unchecked exception, like ArithmeticException?
is that allowed ??
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sarimraza,
The answer to your first question is , that is a case of overloading.
The position of the parameters also affect the method signature
and if the positions of the same datatypes are different the method is valid and it is a case of overloading.
2. You can very well throw unchecked exceptions in the subclass
or in any class for that matter. You need not throw unchecked exceptions at all, but it is not a error if you wish to do so.
Hope this helps,
A small request , I think you can very well code and look what is the result and it will really help you in remembering the
concepts rather than just taking the tips through theory.

------------------
Regards,
V. Kishan Kumar
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your example b will pass but a will throw an error saying
"The method declared in class sub cannot override the method of the same signature declared in class parent."
The method in parent and (a) method in sub class are overridden methods but for the above error. Replace long with int in the sub for (a) solves the problem.
Just remember the rules of overloading and overriding :
overloading - The number and type of parameters should be different. Overloading is related in the same class. overloading of methods is providing alternate implementation for the overloaded methods.
overriding - The number and type of parameters and also the return type should be same. overriding is applicable in parent-sub class. overriding feature totally replaces the implementation of the parent class. An overriding method of subclass can only throw THE CHECKED EXCEPTIONS (OR THEIR SUBCLASSES) thrown by the parent class method. But the overriding methods can throws unchecked exception.
Hope this helps!
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sarim,
Your basic premise is right: the method signature depends on both the type and order of the arguments; however, in the example you used you could run into a compile error if the compiler cannot tell if a value is an int or a long.
For example:

Where exceptions are involved, an overriding method cannot throw more checked exceptions than those originally declared however they can throw fewer exceptions.
Example:

Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiousity.
-- Dorothy Parker
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic