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

method overridding

 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(); }
 
jean-gobert de coster
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option A and B are the correct answer.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pramod please quote your source...
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its from Whiz Lab trail version
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic