Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overridden Method in 1.5

 
Mvvsatyanarayana Murty
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following statements are true?

1) A method cannot be overloaded to be less public in a child class
2) To be overridden a method only needs the same name and parameter types
3) To be overridden a method must have the same name, parameter and return types
4) An overridden method must have the same name, parameter names and parameter types

As of Java 1.4 the Answer is 3 (To be overridden a method must have the same name, parameter and return types). But in 1.5 the return type may be a different (Ex: SubClass return type ...) So Can I know the which answer i need to go?
 
Ryan Anderson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 and 2 are true

But the return value must be either

a) same exact thing for primitives
b) subclass of the return type if its an object
c) implementing class of the return type if its an interface

Note: if you change the param types --> automatically an overload

This wont compile right away --> notice the Honda subclass where it tries to return a byte instead of int

 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mvvsatyanarayana Murty wrote:
1) A method cannot be overloaded to be less public in a child class

Why is this true? For overriddin methods that's true, but for overloading? Actually I don't understand what this means. If this is not a typo, can anybody explains this to me? As far as I know, overloading only occur in one class, not in its child class.

cheers
Bob
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mvvsatyanarayana Murty wrote:Which of the following statements are true?

1) A method cannot be overloaded to be less public in a child class
2) To be overridden a method only needs the same name and parameter types
3) To be overridden a method must have the same name, parameter and return types
4) An overridden method must have the same name, parameter names and parameter types


The 4 is the correct answer.

So Can I know the which answer i need to go?


You can decide it from the version of SCJP exam. As the exam is different for Jave 1.4, 1.5 and 1.6
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Wheeler wrote:

overloading only occur in one class, not in its child class.

Bob


Method can also be overloaded in child class.

Hope it helps
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Amirr. I forgot about the inheritance part
But I disagree with you about anwer 4. Overridding has nothing to do with parameter names. It's all about the argument list (types), the method name and the return type (same or subtype). So it's tricky. Answer 2 is wrong because it also needs the return type for overriding. But answer 3 is too strickt, because it says it must be the same return type. But it's the closest, so I'll go for answer 3.
Answer 1 is wrong because overloading doesn't care about access modifier.

cheers
Bob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic