0

I am extremely new to Java and coding in general. I am working on a project where I have created two interfaces and am now creating a concrete class named VehicleChassis that implements my Chassis interface. First, I created a String named chassisName instance variable. Now, I need to create a default constructor and overloaded constructor with the following value - A String with a parameter value of chassisName. I have searched the Internet on how to do both, but I am so confused. Help!

Below is the code I have so far.

public abstract class VehicleChassis implements Chassis{
public String chassisName;
}
  • 2
    [This link](https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html) from Oracle's tutorials might help you. – Logan Jun 05 '17 at 16:37
  • 1
    please show more context and code. What is `Chassis`, why is it an interface and not a class with members? How do you want to create an instance of `VehicleChassis`, what is the subclass supposed to look like? – luk2302 Jun 05 '17 at 16:38
  • I am working on a project for a class and the instructions start off with creating a Chassis interface. Then, it asked me to create an Engine interface. Now, it is asking me to create a concrete class named VehicleChassis... To be completely honest, I really don't know what I am doing. – Elizabeth Isgro Jun 05 '17 at 16:45
  • Note that if you're using multiple constructors, you usually want to chain them to one main constructor that does all the work (setting fields etc.). Some examples here: https://stackoverflow.com/questions/17171012 https://stackoverflow.com/questions/285177 – Sean Van Gorder Jun 05 '17 at 19:06

4 Answers4

1

You mean this?

public abstract class VehicleChassis implements Chassis{
    public String chassisName;
    VehicleChassis() {
        chassisName = "name";
    }
    VehicleChassis(final String chassisName) {
        this.chassisName = chassisName;
    }
}

You won't be able to instantiate this VehicleChassis because you've declared it as abstract. You can use the constructors if you extend this class, though. Consider declaring the constructors protected if that's what you intend to do.

Michael
  • 41,989
  • 11
  • 82
  • 128
1

If you arre not implement all the methods in Chassis keep VehicleChassis as abstract.

You can overload constructor as many as you want.

public String chassisName;
public VehicleChassis() {
    chassisName = "No";
}
public VehicleChassis(String chassisName) {
    this.chassisName = chassisName;
}

If you have another instance variable:

public VehicleChassis(String chassisName, int price) {
    this.chassisName = chassisName;
    this.price = price;
}

For more about constructor overloading read this question answer.

Blasanka
  • 21,001
  • 12
  • 102
  • 104
0

You can create constructor as below.If the instance variable is more and all are not mandatory then you can use builder pattern to build your object step by step.

 public class VehicleChassis implements Chassis {
        public String chassisName;

        public VehicleChassis() {
            // some code
        }

        public VehicleChassis(String chassisName) {
            this.chassisName = chassisName;
        }

    }
gati sahu
  • 2,576
  • 2
  • 10
  • 16
-3

A class which is labelled as abstract could not be instantiate directly. so we don't need to create constructor and overloaded constructor.

halfer
  • 19,824
  • 17
  • 99
  • 186
Muhammad Waqas
  • 420
  • 4
  • 19
  • 2
    They do have a use: you can call them from a subclass. – Michael Jun 05 '17 at 16:44
  • The only reason I added abstract is the IDE I am using showed an error and 'encouraged' me to use the word abstract. – Elizabeth Isgro Jun 05 '17 at 23:56
  • @ElizabethIsgro I suspect whatever methods were declared in the Chassis interface were missing. Make sure they match exactly. Often an IDE can add these automatically. – Michael Jun 06 '17 at 00:02
  • Please refrain from adding excessive bold/quote formatting to your ordinary paragraph text. That will reduce the amount of repair work needed from volunteer editors in the future. Thank you. – halfer Feb 21 '18 at 12:40