Reputation: 213
I am new to inheritance in java and I have the folowing problem. My base class is Plane, its child class is PlaneComponent and PlaneComponent's child class is PassengerCompartment. My program consists of 11 classes, when I ignore the PassengerCompartment class everythig's right. But when I run the whole program I get this message: at Plane.<init>(Plane.java:14)
at PlaneComponent.<init>(PlaneComponent.java:1)
at PassengerCompartment.<init>(PassengerCompartment.java:11)
which is printed repeatedly for so many times that I cannot see the top line of the error. The lines includes in error messages are the bold lines (here printed between ** **).
Plane:
import java.util.*;
public class Plane
{
int cap;
int pl;
public Plane()
{
String desc = "Plane Description";
String title = "Boeing 747";
Random rand = new Random();
cap = rand.nextInt(100) + 51; //initialize cap with 50<value<100
**PassengerCompartment a8 = new PassengerCompartment();**
pl = cap / a8.cap2; // pl = sum of Passenger Compartments
if (cap % a8.cap2 != 0)
{
pl = pl - (cap % a8.cap2) + 1;
}
}
public boolean ready_check()
{
CargoBay a6 = new CargoBay();
a6.ready_check();
for (int i = 0 ; i < 3 ; i++)
{
EquipmentCompartment a7 = new EquipmentCompartment();
a7.ready_check();
}
for(int i = 0; i < pl; i++)
{
PassengerCompartment a8 = new PassengerCompartment();
a8.ready_check();
}
System.out.println("Plane OK!");
return true;
}
public void process(String e)
{
if(e == "SecurityEmployee")
{
SecurityEmployee a14 = new SecurityEmployee();
a14.workOn();
}
if(e == "MaintenanceEmployee")
{
MaintenanceEmployee a15 = new MaintenanceEmployee();
a15.workOn();
}
if(e == "CleaningEmployee")
{
CleaningEmployee a16 = new CleaningEmployee();
a16.workOn();
}
}
public void values()
{
System.out.println("Would you like more info about the plane's capacity? type Y or N");
Scanner input = new Scanner(System.in);
String inp = input.nextLine();
while (!(inp.equals("Y")) && !(inp.equals("N")))
{
System.out.println("Wrong input, please type again");
inp = input.nextLine();
}
if ( inp.equals("Y"))
{
PassengerCompartment a8 = new PassengerCompartment();
System.out.println("Plane's capacity: " + cap);
System.out.println("PassComp's capacity: " + a8.cap2);
System.out.println("Number ofPassenger Compartments " + pl);
}
}
}
PlaneComponent:
**public class PlaneComponent extends Plane**
{
public boolean ready_check()
{
return true;
}
public void process(String desc)
{
Employee.workOn(desc);
}
}
PassengerCompartment:
import java.util.*;
public class PassengerCompartment extends PlaneComponent
{
Random rand = new Random();
boolean inner = rand.nextBoolean();
int cap2;
String desc;
public PassengerCompartment()
**{**
desc = "Passenger Compartment";
cap2 = rand.nextInt(50) + 21; //initialize cap with 20<value<50
}
public boolean ready_check()
{
System.out.println(desc);
if (super.ready_check() == true)
{
System.out.println("Passenger Compartment OK!");
if (inner == true)
{
desc = "Inner Compartment";
System.out.println(desc);
if (super.ready_check() == true)
{
System.out.println("Inner Compartment OK!");
}
}
}
return true;
}
public void process(String desc)
{
super.process(desc);
}
}
Upvotes: 1
Views: 94
Reputation: 7867
Your issue is with your inheritance structure.
In Plane
s constructor, you create an instance of PassengerCompartment
.
PassengerCompartment
extends PlaneCompartment
. PlaneCompartment
extends `Plane'. So, you are in a circle.
I don't think it makes sense for PlaneCompartment to extends Plane.
What you have is a 'has-a' relationship. So, Plane
has a PlaneCompartment
but neither extends each other.
Upvotes: 0
Reputation: 15310
You have a circular-inheritance-kind-of-situation (which probably creates a StackOverflow exception):
You instantiate PassengerCompartment a8 = new PassengerCompartment();
in the constructor of Plane
.
PassengerCompartment
extends
PlaneComponent
, so the constructor of PlaneComponent
is called implicitly (super()
), which inherit from Plane
. In Plane
's constructor you have the mentioned instantiation of PassengerCompartment
and so on... So I would strongly advise to not instantiate classes that inherit from your class in the constructor of said class.
I would recommend reading my Q/A on this very topic here.
Upvotes: 2