• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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: 76442
366
  • 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
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic