• 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

constructor of subclass call to super

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose i have two classes Vehicle and subclass Car.
In car i have this copy constructor which takes Car as parameter.



Can i call super with c or does it need cast to Vehicle? I would like to know details of this behinds the scenes.
Thanks.
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First class 'Car' should extends class 'Vehicle'. Also you need to have constructor which takes 'Vehicle' type variable in super class.

After extending the class you can have constructor of any type ('Car' or 'Vehicle').
 
Chris Yates
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




I can call super here with c right? No need for cast of Car c to Vehicle, iam i right?
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. This code doesn't compiles. We don't extend this way. Here is the example:





Output:

 
Marshal
Posts: 79265
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:. . .. . .

That bit is misleading since you don't have a default constructor.

Surely manufacturer and number of passengers are attributes of the Vehicle class not specifically the Car class. You may wish to make passengers default to 0. Or, if I read the post correctly, it reads doors. Again you may wish set a default number of doors. 0 is permissible. I can see no point in passing a superclass object to a subclass' copy constructor, except under the following rubric:-

I just wanted to see what happens if …

 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell.. I suppose sub class passing its own reference also doesn't make sense...
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Yates wrote:I can call super here with c right? No need for cast of Car c to Vehicle, iam i right?


Other than what Tashar and Campbell pointed out: No.

Car is a subclass of Vehicle, therefore every Car is a Vehicle - hence: no cast needed.

Winston
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic