Star_89 Star_89 - 1 year ago 73
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;
else System.out.println( + " 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 + ", ");
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) && {
System.out.print( + " is now in between");
System.out.println(from + " and " + stopover);, from, stopover);
else if (flight.from.equals(this.from) && {
System.out.println(from + " och " + to);, from, to);

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


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download