1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Trying to understand the inheritance/interface here(java)

  1. Dec 17, 2016 #1
    1. The problem statement, all variables and given/known data
    Code (Java):

    package movable;
    import java.util.ArrayList;
    import java.util.List;
    /**
    *
    * @author
    */

    public class Group implements Movable {
    private final List<Movable> groups = new ArrayList<Movable>();
    public void addToGroup(Movable movable) {
    groups.add(movable);
    }
    @Override
    public void move(int dx, int dy) {
    for (Movable org : groups) {
    org.move(dx, dy);
    }
    }
    @Override
    public String toString() {
    String group = "";
    for (Movable org : groups) {
    group += org.toString();
    group += '\n';
    }
    return group;
    }
    }
     
    Code (Java):

    package movable;
    /**
    *
    * @author
    */

    public class Organism implements Movable {
    private int x;
    private int y;
    public Organism(int x, int y) {
    this.x = x;
    this.y = y;
    }
    @Override
    public String toString() {
    return "x: " + this.x + "; " + "y: " + this.y;
    }
    @Override
    public void move(int dx, int dy) {
    this.x = this.x + dx;
    this.y = this.y + dy;
    }
    /*
    //return the x value
    public int xValue(){
    return this.x;
    }
    //return y value
    public int yValue(){
    return this.y;
    }
    */

    }
     
    Code (Java):

    package movable;
    public interface Movable {
    void move(int dx, int dy);
    }
     
    2. Relevant equations


    3. The attempt at a solution
    Here in this sample code(not mine) there is this object organism that implements the interface movable here. Object group, is just another arraylist of movable objects(which organism implements). My question is, why can this code access the x and y values of the movable objects inside the group arraylist? The instance variables x and y are defined in the organism class, not the group. Does this suggest that the group object is inheriting out of the organism class?
     
  2. jcsd
  3. Dec 17, 2016 #2

    Mark44

    Staff: Mentor

    Please be more specific. "Why can this code ... " -- which code are you referring to?
     
  4. Dec 17, 2016 #3
    group += org.toString()

    this line will access the x and y positions of the movable objects inside group.

    Edit: made a misunderstanding. Since group is just an arraylist of movable objects, organisms can be stored in to the group object. Essentially I thought any movable object inside group had access to organism's instances varibables through inheritance but this is not the case.

    Solved my own misunderstanding lol
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Trying to understand the inheritance/interface here(java)
  1. Java: Set Interface (Replies: 4)

Loading...