help with java inheritance

R

ros

Hello,

I have a class called Car and a main class that implements this class.
Both are pasted below.

I expect the implementation to print out the price of the car but it
is giving me price '0'.

Can anyone please advise as to what I am doing wrong.

Would be extremely thankful.

Ros



package InheritanceHW;

public class Car {

int speed;
int regularPrice;
String colour;

int getSalePrice(){
return regularPrice;
}

/** Creates a new instance of Car */
public Car(int regularPice, int speed, String colour) {
this.regularPrice = regularPrice;
this.colour = colour;
this.speed = speed;
}

}


package InheritanceHW;

public class MyOwnAutoShop {

public static void main(String args[]){
Car car1 = new Car(2003, 134, "Red");
//Sedan sedan1 = new Sedan(2134, 100, "blue", 13);
//Ford ford1 = new Ford(1970, 123, "Green", 1998, 400);
//Ford ford2 = new Ford(1980, 156, "White", 2001, 350);

System.out.println("The price of car1 is " +
car1.getSalePrice());
//System.out.println("The price of sedan1 is " +
sedan1.getSalePrice());
//System.out.println("The price of ford1 is " +
sedan1.getSalePrice());
//System.out.println("The price of ford2 is " +
sedan1.getSalePrice());


}

}
 
J

JB

A typo in the constructors parameter list leads to assigning the
instance variable to itself.
public Car(int regularPice, int speed, String colour) {
this.regularPrice = regularPrice;


Regards
Jens
 
R

ros

A typo in the constructors parameter list leads to assigning the
instance variable to itself.


Regards
Jens

Thanks a lot JB and Ingo. I think it's time I went to sleep!!

Cheers
ros
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,178
Latest member
Crypto Tax Software
Top