Trying to understand the inheritance/interface here(java)

  • Context: Comp Sci 
  • Thread starter Thread starter Arnoldjavs3
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on Java inheritance and interfaces, specifically the implementation of the Movable interface by the Organism class and its usage within the Group class. The Group class contains an ArrayList of Movable objects, allowing it to manage multiple Organism instances. The confusion arises from the misconception that the Group class inherits properties from the Organism class, when in fact it merely holds references to Movable objects, which can include Organism instances. The key takeaway is that the Group class does not inherit from Organism, but rather utilizes polymorphism to interact with its Movable members.

PREREQUISITES
  • Understanding of Java interfaces and classes
  • Familiarity with Java collections, specifically ArrayList
  • Knowledge of polymorphism in object-oriented programming
  • Basic understanding of Java method overriding
NEXT STEPS
  • Study Java interface implementation and its role in polymorphism
  • Explore Java collections framework, focusing on ArrayList usage
  • Learn about method overriding and its implications in Java
  • Investigate design patterns that utilize interfaces for better code organization
USEFUL FOR

Java developers, computer science students, and anyone interested in understanding object-oriented programming concepts, particularly inheritance and interface implementation in Java.

Arnoldjavs3
Messages
191
Reaction score
3

Homework Statement


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;
}
}

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;
}
*/
}

Java:
package movable;
public interface Movable {
void move(int dx, int dy);
}

Homework Equations

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?
 
Physics news on Phys.org
Arnoldjavs3 said:
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?
Please be more specific. "Why can this code ... " -- which code are you referring to?
 
  • Like
Likes   Reactions: Arnoldjavs3
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 into 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
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K