• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Inheritance, need help

 
Ranch Hand
Posts: 159
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone help me in solving this question ?? Please explain the concept how to check ??

Consider the following classes:


Thanks
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A super class reference variable can refer to a subclass instance but a subclass reference cannot point to a super class instance.

I will leave it for you to compile and check which leads to the issue and why.

This might help - http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
 
Ranch Hand
Posts: 47
PHP C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
" Any superclass reference variable can refer to the subclass object "

In this case...
this is the inheritance tree.

Vehicle----->Car----->Suv

Vehicle is the superclass of Car and Suv => 1 and 6 correct
Car is the superclass of Suv = > 5 correct
Suv is the subclass => So only 3 point is correct.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this campfire story:
http://www.javaranch.com/campfire/StoryPoly.jsp
 
shivang sarawagi
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vehicle v = new SUV();
Vehicle v = new Car();
SUV s = new SUV();
Car c = new SUV();
 
Marshal
Posts: 74004
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Yes, those four are legal declarations and initialisations.
 
Abdulkader Al Othmani
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shivang sarawagi wrote:Can anyone help me in solving this question ?? Please explain the concept how to check ??

Consider the following classes:


Thanks



answer

Vehicle v = new SUV();
Vehicle v = new Car();
SUV s = new SUV();
Car c = new SUV();
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic