Results 1 to 8 of 8
  1. #1
    kk6axq is offline Member
    Join Date
    Feb 2017
    Posts
    3
    Rep Power
    0

    Default Instantiating classes of different types without changing class type

    Hi,
    I've recently started in Java and I'm working on implementing Checkers. I have a main class that displays the board in JavaFX and uses buttons with images on them to display the board. I have another class that implements the board and related logic for moves. Lastly, I'm working on a class for both a computer player and a human player. Here is where I've hit a snag. I want to be able to swap in and out player configurations, e.g. a human and a computer player, two humans, or two computers. The problem with this is that the main class has the type hard coded into the button callbacks and display code. How can I design the human and computer classes such that they can both be interchanged without changing the type of the class in the code. If needed, I can supply the code from my project or whatever details are necessary.
    Thanks,
    kk6axq

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Instantiating classes of different types without changing class type

    How can I design the human and computer classes such that they can both be interchanged without changing the type of the class in the code.
    One way is to define an interface that both classes can implement. The code would use the interface common to both.
    Another way is to have a base class that both classes extend. The code would use the base class common to both.

    .. ... - .... .. ... .-.. ..- -.- .- ...
    Last edited by Norm; 02-15-2017 at 01:06 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kk6axq is offline Member
    Join Date
    Feb 2017
    Posts
    3
    Rep Power
    0

    Default Re: Instantiating classes of different types without changing class type

    Quote Originally Posted by Norm View Post
    One way is to define an interface that both classes can implement. The code would use the interface common to both.
    Another way is to have a base class that both classes extend. The code would use the base class common to both.

    .. ... - .... .. ... .-.. ..- -.- .- ...
    How would I go about implementing this? I've only been learning Java for a few weeks...
    -.-. .- ..- --. .... - - .... . -.-. .- .-.. .-.. ... .. --. -. --. --- --- -.. .--- --- -...

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Instantiating classes of different types without changing class type

    How would I go about implementing this?
    Depends on what the code needs to do for the human and the computer. What are the methods each class would need to expose for the controlling code to be able to use the methods to make the moves etc to play the game?

    See the tutorial for details:
    What Is an Interface? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)
    What Is Inheritance? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)
    Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
    The Really Big Index
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    kk6axq is offline Member
    Join Date
    Feb 2017
    Posts
    3
    Rep Power
    0

    Default Re: Instantiating classes of different types without changing class type

    Thanks for your help! I think I've found the solution. I created a controller class that exposes a button callback that solved my problem. Thanks for the prompt response!
    kk6axq

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Instantiating classes of different types without changing class type

    .-- .... .- - .. ... .-- .. - .... - .... . -- --- .-. ... . -.-. --- -.. .
    ?
    And of course I typed that freehand...no help at all...not me!
    ;)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Instantiating classes of different types without changing class type

    There is a code for the question mark, you know...
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Instantiating classes of different types without changing class type

    I know, but I hadn't put it in the original and couldn't be bothered to [s]go back to the translation site[/s] edit my post.

    ...cough...

    I wonder if there's a font for flag signalling?
    "England expects..."

    ETA: Doesn't this vBulletin have a strikethrough tag?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 3
    Last Post: 05-23-2016, 04:53 PM
  2. Replies: 7
    Last Post: 05-17-2016, 10:08 AM
  3. Instantiating an array with object of another class
    By AMCNovember in forum New To Java
    Replies: 11
    Last Post: 02-18-2012, 05:29 PM
  4. Replies: 2
    Last Post: 05-05-2008, 10:55 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •