Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: A question of accessibility in OOP

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    37
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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?


    I would appreciate any explanation.
    Thanks in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: A question of accessibility in OOP

    access Variables of a class A from class B
    Pass a reference to an instance of class A to class B to allow class B to access class A's variables and methods.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: A question of accessibility in OOP

    You can also pass an instance of ChessGame to a Pawn instance using a setter:

    // in a ChessGame instance using a Pawn instance, pawn
    pawn.setChessGame( this );

    Pawn would have the appropriate instance variable and setter:
    // instance variables
    ChessGame chessGame;
     
    // . . .
     
    public void setChessGame( ChessGame chessGame )
    {
        this.chessGame = chessGame;
    }

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

    Pawn pawn = new Pawn( this );

  4. #4
    Member
    Join Date
    Dec 2013
    Posts
    37
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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:

        public Pawn(String pieceColor, String pieceRank, int piecePositionX , int piecePositionY ) 
        {
     
           isAlive = true; // Whether the piece is still on the board
           this.pieceColor = pieceColor; // Color of the piece
           this.pieceRank = pieceRank;  // Type of piece
           this.piecePositionX = piecePositionX;
           this.piecePositionY = piecePositionY;
     
        }

    I'm unsure of what

    Pawn pawn = new Pawn( this );

    does.

    I'm guessing the 'this' keyword in the bracket just passes an instance of the class to the Pawn constructor , so in my case when initializing the Pawn I would do something like :

    pawn1White = new Pawn("WHITE", "PAWN" , 6 , 0 , this );

    and the constructor would be :

     
    ChessGame chessgame;
     
     
     public Pawn(String pieceColor, String pieceRank, int piecePositionX , int piecePositionY , chessgame ) 
        {
            this.chessgame = chessgame; 
           isAlive = true; // Whether the piece is still on the board
           this.pieceColor = pieceColor; // Color of the piece
           this.pieceRank = pieceRank;  // Type of piece
           this.piecePositionX = piecePositionX;
           this.piecePositionY = piecePositionY;
     
        }

    Does that make sense?
    Would the chessgame variable always be the current updated version ? not the initial state which was passed to the constructor?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: A question of accessibility in OOP

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

    the chessgame variable always be the current updated version
    Yes, the reference would be to the current version.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Dec 2013
    Posts
    37
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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"

    Thank you for your answers , they answered my question

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: A question of accessibility in OOP

    When would a Pawn not be a "PAWN"? That String could be in the constructor, not passed. The code would allow:
    pawn1White = new Pawn("WHITE", "Trooper" , 6 , 0 , this );
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Dec 2013
    Posts
    37
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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 .

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: A question of accessibility in OOP

    That constructor allows you to give an individual name to each Pawn.
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    Att1li (December 19th, 2013)

  11. #10
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    My Mood
    Angelic
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A question of accessibility in OOP

    What about initializing both Pawn and ChessGame in another class and pass instances to both classes.

    public class SomeOtherClass{
     
      public someMethodOrConstructor(){
          ChessGame cg = new ChessGame();
          Pawn p = new Pawn();
     
          p.setChessGame(cg);
          cg.setPawn(p);
      }
     
    }

Similar Threads

  1. Simple OOP question- Creating objects
    By TSSF44 in forum Object Oriented Programming
    Replies: 2
    Last Post: February 4th, 2014, 04:43 PM
  2. Not getting the OOP thing!!!
    By AffiliateOwen in forum Java Theory & Questions
    Replies: 7
    Last Post: April 16th, 2013, 09:44 AM
  3. java oop
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 26th, 2013, 06:50 AM
  4. OOP help!
    By imaznumkay in forum Object Oriented Programming
    Replies: 3
    Last Post: July 11th, 2011, 01:43 PM
  5. OOP
    By mgutierrez19 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 29th, 2009, 10:10 PM