Win a copy of Pipeline as Code this week in the Cloud/Virtualization 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
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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: 9679
42
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
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic