• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

class extension error

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings, appreciate if someone could explain why the answer is not option a?

Assuming these classes are in the appropriate files, what is the output of the following program?



a) A Test
b) A Test B Test
c) B Test <--answer
d) B Test A Test
e) an error occurs

 
Randy Smith
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am I right to say that is because object is created as B, that's why constructor of B is called. appreciate if someone could explain what is going on for this statement



b is the reference but it is declared as Type A, created as instance of B? Now, instance B is type of B or A??
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The object is a B. It's then been assigned to a reference of type A (and you're allowed to do this because B extends A, which means a B IS-AN A), but the actual object is still a B. You can't change the type of an object once created, you can only assign it to different types of reference.

The other part to realise is that the B constructor will always call an A constructor. Since it doesn't to this explicitly (with a call to super(...)), it calls the no-arg constructor. And that's why the answer isn't (b). You can explore this example further to understand how constructors call each other:

- Add the line super(msg); at the beginning of the B constructor. Now the output will match (b).

- Comment out the no-arg constructor in A. No change, because the B constructor is calling the A constructor that takes a String.

- Remove the super() line you just added. Now it won't compile. With no call to super(), it'll try and call the no-arg constructor. But there isn't one!
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in B's constructor, a call to its no argument super constructor is inserted by the compiler. The reference b points to the instance of B.

Using an instanceof operator and checking b will return true for both B and A
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic