Well, I assume you know you pasted your truck class into the email  
twice and right into the middle of your Sedan class.

One minor issue is that you defined the property SalePrice as a  
private type and then don;t use it within the class - this is a  
warning, so not the main root of your problems.

On to the major issues...

The line:

> return (super.getRegularPrice()-ford.getmanufacturerDiscount());


is wrong. you can;t call Ford.getmanufacturerDiscount() unless it's  
defined as a static method, like so:

> public static double getmanufacturerDiscount(){
>    return manufacturerDiscount;
> }


which is something you don;t want to do, nor need to do, in this case.  
Simply calling getmanufacturerDiscount() will run the instance method  
within scope:

>        return (super.getRegularPrice()-getmanufacturerDiscount());


The reason you never see the sale prices, is that you never set the  
regular price for the sedan, ford, and truck objects. Setting the sale  
price for the car object does not transfer over to the sedan, truck  
and ford variables/objects which are completely unrelated to the car  
object, car1.

If you had wanted to transfer the car properties to other classes, you  
would need to pass the car object via the constructor and assign out  
its properties:

Truck truck = new Truck(car1);

In this case, the Truck class would have a constructor which would  
look like this:

public Truck(car carObj)
{
     this.setRegularPrice(carObj.getRegularPrice());
}

Saying that the Truck class inherits the car class, does not mean that  
if you define a car object, all instances of objects that inherit the  
car class will automatically inherit the properties of the car object  
you created. The instances of the objects that inherit the car class  
are independent instances which have nothing to do with a separate  
instance of a car object. In other words, if you create a car object,  
car1, and set its regular price, this price does not transfer to  
anything else unless you transfer it explicitly.  Creating an instance  
of a Truck object, truck, still requires that you set its properties  
since it is an independent object instance regardless of the fact that  
it has inherited its characteristics (not the values) from the car  
class. You would still have to call truck.setRegularPrice(whatever);

On Sep 28, 2009, at 8:49 PM, emman### wrote:

>
> what is the problem in this i got only zero for ans plz explain?
> the overriding method getSalePrice() is not also working what is the
> reason?
>
> MAIN CLASS
>
> package myownautoshopproject;
> public class Main {
>    public static void main(String[] args) {
>        car car1=new car();
>       car1.setSpeed(240);
>       car1.setColor("Red");
>       car1.setRegularPrice(10000);
>       Sedan sedan=new Sedan();
>        sedan.setLength=(25);
>        Truck truck=new Truck();
>        truck.setWeight=(2200);
>        Ford ford=new Ford();
>       ford.setYear(2009);
>       ford.setmanufacturerDiscount(2000);
>       System.out.println("regular price of car:"+car1.getRegularPrice
> ());
>            System.out.println("speed of car:"+car1.getspeed());
>       System.out.println("sale price sedan:"+sedan.getSalePrice());
>       System.out.println("sale price of Ford:"+ford.getSalePrice());
>       System.out.println("sale price of Truck:"+truck.getSalePrice
> ());
>    }
>
> CAR CLASS
>
> package myownautoshopproject;
> public class car {
> private int speed;
> private double SalePrice;
> private String color;
>    private double regularPrice;
>
> public double getSalePrice(){
>    return 0;
> }
> public int getspeed(){
>    return speed;
> }
> public void setSpeed(int speed){
>    this.speed=speed;
> }
> public String getcolor(){
>    return color;
> }
>
> public void setColor(String color){
>    this.color=color;
> }
> public double getRegularPrice() {
>        return regularPrice;
>    }
>    public void setRegularPrice(double regularPrice) {
>        this.regularPrice = regularPrice;
>
> }
> }
>
>
> SEDAN CLASS
>
> package myownautoshopproject;
> public class Sedan extends car {
> public int length;
>    int setLength;
>    public double getSalePrice(){
>       if(length>20){
>        return super.getRegularPrice()*0.05;
> }
>    else
>
>        return super.getRegularPrice()*0.1;
>
> }
> public int getLength(){
>    return length;
> }
>
> package myownautoshopproject;
> public class Truck extends car {
>
>    private double weight;
>    int setWeight;
>
>     public double getSalePrice(){
>        if(weight>2000){
>            return super.getRegularPrice()*0.1;
>        }
>        else
>        return super.getRegularPrice()*0.5;
>    }
>    public double getWeight(){
>        return weight;
>    }
>    public void setWeignt(double weight){
>        this.weight=weight;
>    }
>
>
> }
>
> TRUCK CLASS
>
> package myownautoshopproject;
> public class Truck extends car {
>
>    private double weight;
>    int setWeight;
>
>     public double getSalePrice(){
>        if(weight>2000){
>            return super.getRegularPrice()*0.1;
>        }
>        else
>        return super.getRegularPrice()*0.5;
>    }
>    public double getWeight(){
>        return weight;
>    }
>    public void setWeignt(double weight){
>        this.weight=weight;
>    }
>
>
> }
>
> public void setLength(int length){
>    this.length=length;
> }
> }
> }
>
> FORD CLASS
>
> package myownautoshopproject;
> public class Ford extends car {
> public int year;
> public double manufacturerDiscount;
> public double getSalePrice(){
>        return (super.getRegularPrice()-ford.getmanufacturerDiscount
> ());
> }
> public int getYear(){
>    return year;
> }
> public void setYear(int year){
>    this.year=year;
> }
> public double getmanufacturerDiscount(){
>    return manufacturerDiscount;
> }
> public void setmanufacturerDiscount(double manufacturerDiscount){
>    this.manufacturerDiscount=manufacturerDiscount;
> }
> }
>
> >


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to