aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method overridding" Watch "method overridding" New topic
Author

method overridding

Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

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
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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


SCJP 6
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: http://aspose.com/file-tools
 
subject: method overridding
 
Similar Threads
can any body explain me output of these program
Return type
confused with Overloading rules
(Solved!) Inheritance, polymorphism and overriding.
Super class method