0

For an assignment I'm supposed to have a constructor that takes name, address, phone, make, model, capacity, and numAxles as arguments. The first five should be passed to the superclass, Vehicle. The UML says that my superclass should take the object "person" as the first argument (which is from another class and takes name, address, and phone). How do I pass as argument the name, address, and phone to the superclass from the Truck constructor?

Here's the UML

The Truck class

public Truck(Person person, String make, String model, int year, int mileage, int capacity, int numAxles){
        super(person, make, model, year, mileage);

    }

    public Truck(String name, String address, String phone, String make, String model, int capacity, int numAxles ){
        super(name, address, phone, make, model);
    }

The Vehicle superclass

public Vehicle(Person person, String make, String model, int year){
        setMileage(0);
    }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Noel
  • 25
  • 5

1 Answers1

0

You can do this by constrcuting a new Person

super(new Person(name, address, phone), make, model, 2020);

You will probably need some default number for the year as you have not provided it.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • 1
    Alternately, `Vehicle` needs a ctor that does not require a `Person`, or you can just pass `null` in for the person. This all depends on requirements so it's a little hard to guess what is actually needed. Many ways to do it. – markspace Oct 29 '20 at 01:58
  • @markspace Yep, it is a bit of a guess, but seeing as there are variables such as `name` `address` etc, I took a wild guess and assumed that these were part of a `Person` – Scary Wombat Oct 29 '20 at 01:59
  • 1
    It's a good guess, I just wanted to provide the OP with some alternatives in case one was actually what they needed. – markspace Oct 29 '20 at 02:00
  • @AbhijitSarkar The link that you gave did not answer the question. If you can find a duplicate then close this question. – Scary Wombat Oct 29 '20 at 02:47
  • [how to call superclass constructor java](https://www.google.com/search?q=how+to+call+superclass+constructor+java&oq=How+to+call+superclass&aqs=chrome.2.0i457j69i57j0l6.5658j0j7&sourceid=chrome&ie=UTF-8). Don't get me wrong, you spent the time answering his question, but he failed to show any effort of having tried. – Abhijit Sarkar Oct 29 '20 at 02:48
  • @AbhijitSarkar I am guessing that you do not understand the question (as vague as it was). The issue is not just with calling `super`, it is creating a new `Object` - normally a Noob may try to create that Object then pass that Object to `super`, but this would be invalid code. - Do you understand now? – Scary Wombat Oct 29 '20 at 02:50