Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Overridden Method in 1.5

 
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?
 
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

 
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
 
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
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic