File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance and Subclasses

 
ashlan cooper
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
ashlan cooper
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, will try my best to change accordingly
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, if all sports cars have an aerodynamics value of 0.5, that should be
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote: . . . if all sports cars have an aerodynamics value of 0.5 . . .
I had missed that bit. Sorry.
 
ashlan cooper
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ashlan cooper wrote: . . . . . .
That method will most definitely not do what you want.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic