• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

method overridding

 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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(); }
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Option A and B are the correct answer.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pramod please quote your source...
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its from Whiz Lab trail version
 
reply
    Bookmark Topic Watch Topic
  • New Topic