0

I trying to learn some Java and I've got stuck creating a subclass. I keep getting a There is no default constructor available in... error.

This is the code:

class Car 
{
    String name;
    int speed;
    int gear;
    int drivetrain;
    String direction;
    String color;
    String fuel;

    public Car(String carName, int carSpeed, String carDirection, String carColor, int carDriveTrain, int carGear)
    {
        name = carName;
        speed = carSpeed;
        gear = carGear;
        drivetrain = carDriveTrain;
        direction = carDirection;
        color = carColor;
        fuel = "Gas";
    }

    void shiftGears(int newGear){gear = newGear; }
    void accelerateSpeed(int acceleration){speed = speed + acceleration;  }
    void applyBrake(int brakingFactor){ speed = speed - brakingFactor;}
    void turnWheel(String newDirection){ direction = newDirection; }

}//end of Car class

class Suv extends Car
{

    void applyBrake(int brakingFactor)
    {
        super.applyBrake(brakingFactor);
        speed = speed - brakingFactor;

    }
}

The issue comes when I try to create the "Suv" subclass. What am I doing wrong? Thanks!

7
  • I think you need a constructor in Suv which gets the same parameters as Car. Commented Nov 22, 2014 at 3:27
  • Long story short, that's not how classes in Java work. Commented Nov 22, 2014 at 3:27
  • You don't need "super.", you have to declare your methods protected. Not having a public/private/protected identifier makes them be visible only at package level. Commented Nov 22, 2014 at 3:29
  • 2
    @MLProgrammer-CiM Most of what you have said is not true. You can have multiple classes per file. Subclasses do not have to be "declared as static". Method privacy is unrelated to anything in the question. Commented Nov 22, 2014 at 3:38
  • @khelwood is right - and it's worth expending your comment: there can be only one public class per file, but there can be multiple non-public classes. Commented Nov 22, 2014 at 3:40

4 Answers 4

2

You probably want to create the following constructor in Suv that initialzies that parameters that the Car constructor has:

public Suv(String carName, int carSpeed, String carDirection, String carColor, int carDriveTrain, int carGear)
{
    super (carName, carSpeed, carDirection, carColor, carDriveTrain,carGear);
}

The alternative is to add the parameterless constructor to Car, in which case the default constructor of Sub would call that constructor :

public Car()
{

}
Sign up to request clarification or add additional context in comments.

Comments

2

Since "Car" has a constructor, any subclass derived from Car needs a constructor as well. So first you need to put a constructor in the Suv class.

Example:

class Suv extends Car {
    public Suv() {
        super( /* here you need to pass arguments to create a car */);
        // any other constructor code
    }
}

Comments

1

Add the following constructor to Car:

public Car(){}

The problem is that Suv cannot be created since in order to run Suv's default constructor the constructor of Car needs to be ran first, and Car has only a constructor that accepts arguments so it can't be used as default-constructor.

Another approach would be, as @Markus suggested, to implement a constructor in Suv that'll call super with all the required arguments. Either way, the main idea is that in order to instantiate Suv we need to be able to instantiate Car first otherwise we'll get a compiler error.

Comments

0

What i would suggest is to not put the SUV class in the same file. Instead, Create another class in your current package, name it SUV, extended it and call the superclass constructor by either of the following two syntax:

super(); //calls the superclass no-argument constructor with no parameter list

or

super(parameter list); //calls the superclass constructor with a matching parameter list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.