Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Identifying invalid calls

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Identifying invalid calls

    Hey guys, quick question here - I'm supposed to identify which calls to the constructor are invalid - here's the code. Note this is not compilable, it's just the example that the book gives.

    My first thoughts was down in the main method where it's declaring myCar1 = new Car();, etc. Though I'm not 100% sure.


    class Car {
       public String make;
       protected int weight;
       private String color;
     
       private Car (String make, int weight, String color) {
          this.make = make;
          this.weight = weight;
          this.color = color;
       }
     
    public Car () {
          this ("unknown", -1, "white");
       }
    class ElectricCar extends Car {
       private int rechargeHour;
     
       public ElectricCar() {
          this(10);
       }
     
       private ElectricCar(int charge) {
          super();
          rechargeHour = charge;
       }
    }
     
    class TestMain {
       public static void main (String[] args) {
          Car   myCar1, myCar2;
          ElectricCar  myElec1, myElec2;
     
          myCar1 = new Car();
          myCar2 = new Car("Ford", 1200, "Green");
          myElec1 = new ElectricCar();
          myElec2 = new ElectricCar(15);
      }
    }

    Thanks


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Identifying invalid calls

    Did they indeed put the ElectricCar class inside of the Car class? If that's the case, that's probably the problem. Normally when you extend a class you don't place the extending class inside the original class (actually, it's not often that a class gets placed inside another class). If it's suppose to be placed inside, then the two calls to the ElectricCar constructors are incorrect. They must be referenced from an existing instance of the Car class. For example:
    myElec1 = myCar1.new ElectricCar();

    As for the Car constructors, look at the ones declared inside the Car class and the ones invoked. do the signatures match up?

    Ex., these would match up:
    public SomeClass(String a)
    {
    }
     
    public SomeClass()
    {
    }
     
    // to call 1st constructor
    SomeClass c = new SomeClass("hello");
    Last edited by helloworld922; February 26th, 2011 at 05:41 PM.

Similar Threads

  1. Identifying calling object
    By frogfury in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 25th, 2010, 06:13 PM
  2. main calls Math.cos and myCos twice.....
    By kimchi999 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 5th, 2010, 08:11 AM
  3. Need help understanding method calls and such
    By hackman2007 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 14th, 2010, 09:18 AM
  4. Any way to map method calls?
    By Swiftslide in forum Collections and Generics
    Replies: 1
    Last Post: September 21st, 2009, 05:37 AM
  5. Replies: 6
    Last Post: August 30th, 2009, 05:31 AM