| Author |
Inheritance and Subclasses
|
ashlan cooper
Greenhorn
Joined: Nov 23, 2010
Posts: 11
|
|
Hello there,
I am trying to solve this exercise I am doing.
Basically I need to create a , class ( I already did) that has 3 instance variables ()
In this class as well I did the following
Then I created a subclass of Vehicle called SportCar (I extended the class)
it has an additional variable in it )
All sportcars have aerodynamics = 0.5
I need to create 3 sportcars which have the following:
SportCar1 : horsepower = 200, weight = 1500, topspeed = 220
SportCar2 : horsepower = 100, weight = 1000, topspeed = 170
SportCar3 : horsepower = 135, weight = 1100.2, topspeed = 173
So I created them in another class called TestConsumption
my problems are:
How do I create 3 instances of sportcar/initialize the horsepowers, weights and topspeeds for all the sportcars?
Is my method of setting aerodynamic =0.5 for all sportcars right?
Are those codes correct. I did get a few cannot find symbol along the way.
Any help is greatly appreciated.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2929
|
|
Your Super class- Vehicle has the constructor which takes- three parameters. But in the sub class constructor- SportsCar you are calling super(<parameter>), but as you would see- the Vehicle would be expecting: super(param1,param2,param3).
Java provides a default- no-arg constructor if the constructor is not overloaded in the class. But if the constructor is overloaded- the programmer would have to specify the no-arg constructor- something like:
Now coming to initializing the fields for the class- You can pass it in the constructor- while creating the instance or use the setters to set those values.
If you want to set a particular value for a particular variable for all the instances of the class- You would have to declare the variable/field as Static- Something like
|
Mohamed Sanaulla | My Blog
|
 |
ashlan cooper
Greenhorn
Joined: Nov 23, 2010
Posts: 11
|
|
|
Thank you, will try my best to change accordingly
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
You should be writingYou are allowed to write super(...) or this(...) in the constructor, but only as the very first statement. So you can't get both in, and you can't use them twice.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Actually, if all sports cars have an aerodynamics value of 0.5, that should be
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Rob Spoor wrote: . . . if all sports cars have an aerodynamics value of 0.5 . . .
I had missed that bit. Sorry.
|
 |
ashlan cooper
Greenhorn
Joined: Nov 23, 2010
Posts: 11
|
|
Hey guys,
thanks. I modified the code
So what do you think?
and what I am trying to do now is that
I want to implement this formula for fuel consumption under the TestConsumption class
(1000+(weight/5))*(topspeed/100)*(aerodynamics*horsepower)/1000
for those 3 new sportcars I created.
How do I pass the values of each sportcar; sportcar1,2,3
into this formula
thanks
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
ashlan cooper wrote: . . . . . .
That method will most definitely not do what you want.
|
 |
 |
|
|
subject: Inheritance and Subclasses
|
|
|