File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method overridding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method overridding" Watch "method overridding" New topic

method overridding

Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 632

what will be the output of the followinf code? Please explain it in brief

which of the following methods inserted independently at the indicated line in the following code can cause it to compile correctly?

class SuperBase { }
class Base extends SuperBase { }
class Derived extends Base { }

class CovariantTest1
public Base getIt()
return new Base();

class SubCovariantTest extends CovariantTest1
// insert code here


select any 2 options:
[a] public Derived getIt() { return new Derived(); }
[b] public Base getIt() { return new Derived(); }
[c] public SuperBase getIt() { return new Derived(); }
[d] public SuperBase getIt() { return new Base(); }
[e] public Derived getIt() { return new Base(); }

Life is easy because we write the source code.....
jean-gobert de coster
Ranch Hand

Joined: Dec 04, 2008
Posts: 49
It would be A and B

as of java 5 you may override a method by returning a subclass of it's overriden return type, so A is correct.

B is correct because you can a Derived IS-A Base

C and D are incorrect because the new return type is not a subclass of Base

E is incorrect because you would need an explicit cast from Base to Derived
Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105
Option A and B are the correct answer.
Ankit Garg

Joined: Aug 03, 2008
Posts: 9326

pramod please quote your source...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Its from Whiz Lab trail version

Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
I agree. Here's the link:
subject: method overridding