Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

help with java inheritance

Thread view: 
ros - 09 May 2007 10:05 GMT
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());

       }

   }
Ingo R. Homann - 09 May 2007 10:47 GMT
Hi,

>         public Car(int regularPice, int speed, String colour) {
                                ^^
>             this.regularPrice = regularPrice;

Nice typo! Use an IDE like Eclipse - it would warn you on such errors!

Ciao,
Ingo
JB - 09 May 2007 10:49 GMT
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
ros - 09 May 2007 10:51 GMT
> A typo in the constructors parameter list leads to assigning the
> instance variable to itself.
[quoted text clipped - 4 lines]
> Regards
> Jens

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

Cheers
ros


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.