Star_89 Star_89 - 12 days ago 7
Java Question

Overlapping classes under the same package in java

I am learning Java on my own, i came a cross this interesting example and i found it difficult to understand it.

What i want to know is, what is this called in Java when you have several classes under the same package all of them are Overlapping. Please take a look at the example below ? Note that none of the classes use implements, interface, abstract, extends etc...

And is it possible to find more of these examples ?

class Flightplan

public class Flightplan {
String type;
int seat;
String from;
String to;
// Other local variables, style captain ...
Person[] passenger;
int counter = 0;

Flightplan (String t, int s, String startPlace, String d) {
type = t;
seat = s;
passenger = new Person [s-1]; // kapten tar ett s├Ąte
// Captain takes a seat
from = startPlace;
to = d;
}

void book (Person p, String f, String t) {
if (f.equals(from) && t.equals(to)) {
passenger[counter] = p;
to = t;
counter++;
}
else System.out.println(p.name + " try to book a wrong flight !");
}

void flyg() {
System.out.println("On the plane " + this.typ + " reser");
for (int i = 0; i < passenger.length && passenger[i] != null; i++) {
System.out.println(passenger[i].name + ", ");
}
System.out.println("\n");
from = to;
}

}


class Person

public class Person {
String name;
String from;
String to;
String stopover;
int bags;
Flightplan flight;

Person (String n, String f, String m, String t, int v) {
name = n;
from = f;
stopover = m; // Only one stopover is approved, otherwise we would enter this as an array
to = t;
bags = v;
}

void boardNextLeg(Flightplan plan) {
flight = plan;

// Function bar for a stopover due. if-kit building
if (!stopover.equals(null) && flight.from.equals(this.from) && flight.to.equals(this.stopover)) {
System.out.print(this.name + " is now in between");
System.out.println(from + " and " + stopover);
flight.book(this, from, stopover);
}
else if (flight.from.equals(this.from) && flight.to.equals(this.to)) {
System.out.println(from + " och " + to);
flight.book(this, from, to);
}

else System.out.println(this.name + " could not be booked on a flight");
}


}

Answer

What is this called in Java when you have several classes under the same package all of them are Overlapping ?

This is NOT overlapping, rather this is called circular dependency because your Flightplan and Person are dependent on each other, which is bad design and poorly developed.

Basically Circular Dependencies cause lots of issues (like OutofMemoryError) if not properly used, so they should be avoided in between the classes/packages.

You can look here for more details on circular dependencies.