Trying to understand the inheritance/interface here(java)

  • Comp Sci
  • Thread starter Arnoldjavs3
  • Start date
In summary, the code provided shows an example of a Group class that implements the Movable interface and has a method for adding Movable objects to a list. The Organism class also implements the Movable interface and has methods for setting and getting x and y values. The code is able to access the x and y values of the Movable objects inside the Group by using the toString() method of the Organism class, which returns the x and y values of the object. This does not suggest inheritance, but rather the ability to access and manipulate data within classes that implement the same interface.
  • #1
Arnoldjavs3
191
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
  • #2
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 Arnoldjavs3
  • #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 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
 

What is inheritance in Java?

Inheritance in Java is a mechanism where one class inherits the properties and methods of another class. The class that inherits is called a child or subclass, and the class that is inherited from is called a parent or superclass. This allows for code reuse and promotes a hierarchical structure in object-oriented programming.

How does inheritance work in Java?

Inheritance in Java follows the "is-a" relationship, meaning that a child class is a type of the parent class. The child class inherits all the public and protected properties and methods of the parent class, and can also have its own unique properties and methods. This allows for code reuse and promotes a hierarchical structure in object-oriented programming.

What is an interface in Java?

In Java, an interface is a blueprint of a class that contains only abstract methods and static final variables. It can be seen as a contract that a class must adhere to if it implements the interface. Interfaces are used to achieve abstraction and provide a way for classes to share common behavior without being tied to each other's implementation.

How is an interface different from a class in Java?

An interface in Java cannot be instantiated, meaning you cannot create objects of an interface. It only provides a template for classes to implement. A class, on the other hand, can be instantiated and can have both abstract and concrete methods. Additionally, a class can only extend one superclass, but it can implement multiple interfaces.

Why is understanding inheritance and interfaces important in Java programming?

Understanding inheritance and interfaces in Java is crucial for creating efficient and maintainable code. It allows for code reuse, promotes a hierarchical structure, and supports the principle of abstraction. This leads to cleaner and more organized code, making it easier to understand and modify in the future.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
939
Back
Top