The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

A question of accessibility in OOP

Hello

I have a question regarding a concept of object oriented programming. This is probably going to be an easy one to answer for you guys.

Lets say I have 2 classes : ChessGame and Pawn

Now if I create an instance of Pawn inside the ChessGame , I can then access methods/variables of Pawn using getters/setters and public methods because I have created and
initialized the instances of Pawn from within ChessGame , so I can pass variables from ChessGame to the public methods in Pawn and then use them in the methods .That makes sense.

But what If I wanted to access variables of an instance of ChessGame from the class Pawn ? It doesn't allow me to do that . It doesn't recognise the instance of ChessGame because it was
initialised from another class. So can I access Variables of a class A from class B without having initialised A in B? Or is it conceptually done so you can't do that?

Re: A question of accessibility in OOP

Pass a reference to an instance of class A to class B to allow class B to access class A's variables and methods.

That makes sense to me .. so any method in Pawn that requires access to variables /methods from ChessGame can simply have the instance of ChessGame passed into it and then I can
access what I need.

Or the ChessGame instance could have been passed through Pawn's constructor:

Pawn pawn = new Pawn( this );

Hmm this sounds interesting , but a few questions.

This one might sound silly:
If I pass an instance of the ChessGame into the constructor of Pawn at the very beginning , then when I use variables of the ChessGame instance in Pawn will it be the current instance of ChessGame or the initial state of the instance of ChessGame that was passed to the constructor of Pawn? I'm guessing it will be the current instance... (I only create 1 instance of ChessGame throughout the whole program which basically stores the position of the pieces and some other stuff)

Second:
My pawn constructor has other variables passed into it aswell:

Re: A question of accessibility in OOP

Question: Why pass "PAWN" to a Pawn class object? When would a Pawn not be a "PAWN"?

Well because in ChessGame I have an array of type ChessPiece (which is a superclass of Pawn and the other pieces) and when I click on the array , I need to know what color the piece I clicked is along with some other parameters. I could send those parameters to a method in pawn .. that's not a problem . But as I continue to write the program I end up adding a few variables here and there which means I have to change the methods in all the pieces classes every time so I thought that maybe I could just pass the whole instance and when I need to do something Piece-specific then I can just extract it from the passed instance.

Analogically , imagine you are making trade with a merchant and you ask him for parameter A of the product , and then you realize that you also need parameter B and C of the product so you add that to the list of things
to ask him until you say.... "Just give me the entire spec sheet of the product so I will get whatever info I might need in the future from it and I won't have to bother you anymore"

Re: A question of accessibility in OOP

When would a Pawn not be a "PAWN"?

The reason for passing the instance of the pawn to a method in the pawn class is because there are many instances of Pawn so I need to know which one I want to work with .
I don't know if there is a better way of doing this but adding
pawn1White = new Pawn("WHITE", "Trooper" , 6 , 0 , this );
to the initialization and the respective parameter to the constructor makes sense to me .