I have a subclass and I need to call it in another method. It is saying class doesn't exist

StackOverflow https://stackoverflow.com/questions/23353367

  •  11-07-2023
  •  | 
  •  

Question

I have created the subclass RaceBoat and I need to call it in the main method which is the Demo program to print the values. Code for program with constructors:

//Values Based on a CAL 29 boat

public class Boat {

private int boatLength; //29 feet
private double boatBeam; //9.25 feet
private int waterlineLength; //24 feet
private double boatDraft; //4.50 feet
private int mastHeight; //41 feet
private String boatType;
private String boatName;
private int boatPrice;

public class RaceBoat extends Boat {

    int boatPrice;
    public RaceBoat(int boatLength, double boatBeam, int waterlineLength,
            double boatDraft, int mastHeight, String boatType,
            String boatName, int boatPrice)
    {
        super(boatLength, boatBeam, waterlineLength, boatDraft, mastHeight, boatType,
                boatName);
        this.boatPrice = boatPrice;
    }
    public RaceBoat()
    {

    }
    public void display()
    {
        System.out.println("The length of the boat is " + boatLength + " feet.");
        System.out.println("The width of the boat is " + boatBeam + " feet.");
        System.out.println("The length of the part of the boat that is under water is " + waterlineLength + "feet.");
        System.out.println("The depth of the hull is " + boatDraft + " feet.");
        System.out.println("The height of the mast is " + mastHeight + " feet.");
        System.out.println("This boat is a " + boatType);
        System.out.println("The name of this boat is " + boatName);
        System.out.println("The price of this boat is $" + boatPrice + "dollars.");
    }

}

public Boat(int boatLength, double boatBeam, int waterlineLength, double boatDraft, int mastHeight, String boatType, String boatName) {
    super();
    this.boatLength = boatLength;
    this.boatBeam = boatBeam;
    this.waterlineLength = waterlineLength;
    this.boatDraft = boatDraft;
    this.mastHeight = mastHeight;
    this.boatType = boatType;
    this.boatName = boatName;
}
public Boat()
{

}

public void setPrice (int pri)
{
    boatPrice = pri;
}

public void setLength (int len)
{
    boatLength = len;
}
public void setWidth (double wid)
{
    boatBeam = wid;
}
public void setWll (int wll)
{
    waterlineLength = wll;
}
public void setDraft (double draft)
{
    boatDraft = draft;
}
public void setMast (int mast)
{
    mastHeight = mast;
}
public void setType (String type)
{
    boatType = type;
}
public void setName (String name)
{
    boatName = name;
}
public int getLength (int len)
{
    return boatLength;
}
public double getWidth (double wid)
{
    return boatBeam;
}
public int getWll (int wll)
{
    return waterlineLength;
}
public double getDraft (double draft)
{
    return boatDraft;
}
public int getMast (int mast)
{
    return mastHeight;
}
public String getType (String type)
{
    return boatType;
}
public String getName (String name)
{
    return boatName;
}
public int getPrice (int pri)
{
    return boatPrice;
}
public void display()
{
    System.out.println("The length of the boat is " + boatLength + " feet.");
    System.out.println("The width of the boat is " + boatBeam + " feet.");
    System.out.println("The length of the part of the boat that is under water is " + waterlineLength + "feet.");
    System.out.println("The depth of the hull is " + boatDraft + " feet.");
    System.out.println("The height of the mast is " + mastHeight + " feet.");
    System.out.println("This boat is a " + boatType);
    System.out.println("The name of this boat is " + boatName);
}

}

Code for Demo is:

public class Demo {

public static void main (String[] args)
{
    Boat boat1 = new Boat();
    Boat boat2 = new Boat(29, 9.25, 24, 4.50, 41, "sailboat", "CAL 29");
    System.out.println("-----Boat1-----");
    System.out.println();
    boat1.display();
    System.out.println();
    System.out.println("-----Boat2-----");
    System.out.println();
    boat2.display();
    RaceBoat boat3 = new RaceBoat(); 

}

}

Was it helpful?

Solution

The problem is that you have Raceboat inside Boat. You should move Raceboat to a new file.

Boat.java

public class Boat {
    public Boat(){

    }

    public void display(){
        System.out.println("I'm a Boat");
    }
}

Raceboat.java

public class Raceboat extends Boat{
    public Raceboat(){

    }

    public void display(){
        System.out.println("I'm a Raceboat");
    }
}

and Main.java

public class main {
     /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Boat boat = new Boat();
        boat.display();

        Boat raceboat = new Raceboat();
        raceboat.display();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top