javaoopcoupling

Is it okay to use an instance of class A as a attribute of class B


I am developing a small JAVA game and I have this problem about cohesion and coupling in OOP concept.

I have a class named Locations as below

public class Location {
private int positionX;
private int positionY;

public Location(int positionX, int positionY){
    this.positionX=positionX;
    this.positionY=positionY;
}

public void updateLocation(int positionX, int positionY){
    this.positionX=positionX;
    this.positionY=positionY;
}

public String getLocation(){
    return (positionX+","+positionY);        
}

public int getLocationX(){
    return this.positionX;        
}

public int getLocationY(){
    return positionY;        
}

public void setLocationX(int positionX){
    this.positionX=positionX;        
}

public void setLocationY(int positionY){
    this.positionY=positionY;        
}
}

And I have an interface named locatable as bellow

public interface Locatable {
public Location getPosition();
public void setPosition(Location position);

}

And a abstract class implementing the above interface as bellow

public abstract class GameObject implements Locatable{
protected String name;
protected Location position;

public GameObject(String name, Location position){
    this.name=name;
    this.position=position;

}

//------------- getter & setters for positions----------

@Override
public Location getPosition(){
    return position;
}
@Override
public void setPosition(Location position){
    this.position=position;
}
//------------------------------------------------------
//------------------------------------------------------


//------------- getter & setters for name ---------------
public String getName(){
    return name;
}
public void setName(String name){
    this.name=name;
}
//------------------------------------------------------
//------------------------------------------------------

}

And a class that extends the above class

public class Lotus extends GameObject{
private int no_of_Petals=100;

public Lotus(String name,Location position){
    super(name,position);

}


//-----getters & setters for no of petals --------
public void setNoOfPetals(int no_of_Petals){
    this.no_of_Petals=no_of_Petals;
}

public int getNoOfPetals(){
    return no_of_Petals;
}
//------------------------------------------------
//------------------------------------------------

}

So I am confused about whether the using Location class all over my code is not a good coding practice.Like is it tightly coupling ? or is it okay ? Please let me clarify this question as I am still learning about OOP. if you are going to down vote this question please provide a comment about where I am wrong before doing so..


Solution

  • Yes. Location is tight coupled in your code. However, I believe tight coupling a generic class which would not change much, is still ok if that is how you want it.

    Still if you want to loose couple, you can create an interface that Location class implements and use the interface everywhere in your code instead of the class name.

    You can then create objects of the implementation and pass it to the methods where interface names are formal arguments