• 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

overriding

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
This question if from mock of Sun web:
Which of the following methods would be legal (individually) at line 2 in class Test2?
Consider these classes, defined in separate source files:
1. public class Test1 {
2. public float aMethod(float a, float b)
throws IOException {
3. }
4. }
1. public class Test2 extends Test1 {
2.
3. }

A. float aMethod(float a, float b) { }
B. public int aMethod(int a, int b) throws Exception { }
C. public float aMethod(float a, float b) throws _Exception { }
D. public float aMethod(float p, float q) { }
The correct answer is B and D
I don�t understand why B is correct . Why B don�t return an int type?? Why can an int method override a float method???
Thank you in advance
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please correct me if I am wrong, but I believe this would be overloading, not overriding. With B, you are saying that, in the child class, if the parameters passed are of type int, then this implementation returns an int.

Originally posted by Jordi Marqu�s:
Hi!
This question if from mock of Sun web:
Which of the following methods would be legal (individually) at line 2 in class Test2?
Consider these classes, defined in separate source files:
1. public class Test1 {
2. public float aMethod(float a, float b)
throws IOException {
3. }
4. }
1. public class Test2 extends Test1 {
2.
3. }

A. float aMethod(float a, float b) { }
B. public int aMethod(int a, int b) throws Exception { }
C. public float aMethod(float a, float b) throws _Exception { }
D. public float aMethod(float p, float q) { }
The correct answer is B and D
I don�t understand why B is correct . Why B don�t return an int type?? Why can an int method override a float method???
Thank you in advance


 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OverRiding:
The return type, method name, and number and type of the parameters for the overriding method must match those in the overridden method. The overriding method can have a different throws clause as long as it doesn't declare any types not declared by the throws clause in the overridden method. Also, the access specifier for the overriding method can allow more access than the overridden method, but not less. For example, a protected method in the superclass can be made public but not private.
B is overloading not overriding

Originally posted by Jordi Marqu�s:
Hi!
This question if from mock of Sun web:
Which of the following methods would be legal (individually) at line 2 in class Test2?
Consider these classes, defined in separate source files:
1. public class Test1 {
2. public float aMethod(float a, float b)
throws IOException {
3. }
4. }
1. public class Test2 extends Test1 {
2.
3. }

A. float aMethod(float a, float b) { }
B. public int aMethod(int a, int b) throws Exception { }
C. public float aMethod(float a, float b) throws _Exception { }
D. public float aMethod(float p, float q) { }
The correct answer is B and D
I don�t understand why B is correct . Why B don�t return an int type?? Why can an int method override a float method???
Thank you in advance


 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I try compile but compile time error:
method amethod cannot override method amethod...
why?
I can overload a method in a subclass?
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jordi,
While overriding or overloading you see the signature of the method.The signature includes the name of the method and its parameters.It does not include the return type.
So in the code snipplet you posted, D corresponds to overriding (as signatures of the method is the same).B corresponds to overloading (signature is different since the parameters have changed, but the method name is still the same).
Also remember you can overload a method in the same class, but for overriding, it has to be the subclass.
One more thing about overriding - when you override a method, apart from the signature the return type must also be same.However, while overloading the return type could be different.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED (Oracle Internet Platform)
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just want to know why
C. public float aMethod(float a, float b) throws _Exception { }
is incorrect. _Exception can be a runtime exception or a subclass of runtime exception. AFAIK the overriding method can not throw any checked exception not thrown in overriden. Please correct me if I am wrong.
--Farooq
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Farooq,

Originally posted by Muhammad Farooq:
I just want to know why
C. public float aMethod(float a, float b) throws _Exception { }
is incorrect. _Exception can be a runtime exception or a subclass of runtime exception. AFAIK the overriding method can not throw any checked exception not thrown in overriden. Please correct me if I am wrong.
--Farooq


If _Exception is a subclass of RuntimeException then it is not a problem.But this is not stated anywhere.You would need to add some code to make do this.
In this problem, you are only tested on the fact that when you override, you have to take care to see that the exception thrown by the overridden method is either the same or the subclass of it or is declared with a throws clause.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED (Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited August 30, 2001).]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi! I have gone through your problem but find that you are bit confussed with function overriding and type-casting.You want to know why option B is correct because the function mentioned(sorry donot remmember name)is public so it can be called from another class,very true!.but the java compiler can convert or type-cast the variables or datatypes like int into float because
the bit code generated for int or a float datatype only differ in the size of memory they occupy.If you reqiure futher help mail back
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sandeep, I really appreciate your help.
As you said, "If _Exception is a subclass of RuntimeException then it is not a problem.But this is not stated anywhere.", should I always take it as checked exception if not stated/mentioned.
--Farooq
[This message has been edited by Muhammad Farooq (edited August 30, 2001).]
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Farooq,


Originally posted by Muhammad Farooq:
[..]should I always take it as checked exception if not stated/mentioned.[..]


You may consider this as a Checked Exception (one that has not been defined) or a typo (considering that no such subclass of IOException exists).Also on as-is basis from the API docs you can safely infer that it is not a subclass of java.lang.RuntimeException
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
reply
    Bookmark Topic Watch Topic
  • New Topic