File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Inheritance and Subclasses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inheritance and Subclasses" Watch "Inheritance and Subclasses" New topic
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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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: 38497
    
  23
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: 19682
    
  20

Actually, if all sports cars have an aerodynamics value of 0.5, that should be


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38497
    
  23
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: 38497
    
  23
ashlan cooper wrote: . . . . . .
That method will most definitely not do what you want.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inheritance and Subclasses