Object Oriented Class Relationship

I have a Java grade that is marked by a robot. Whenever I upload my task, it displays such a screen.

  A good object-oriented design places each method into the most appropriate
 class.  The most appropriate class for a method should be the same class as
 the data fields that that method needs to access.  If you don't place a method
 into the right class, then this will most likely increase the amount of
 communication that is required between your classes.

 The score below measures the amount of communication between your classes.  A
 lower score is better.

 19 method invocations between classes
 7 arguments passed between classes
 15 results returned between classes

 Amount of communication = invocations + 2 * inputs + 2 * outputs = 63

Now, what does “method calls between classes”, “arguments passed between classes”, and “results returned between classes” mean?

+4
source share
2 answers

Method call between classes

Since your class contains its own methods, if you want to call a method from another class, you must use an instance of this class.

For instance:

class A{ public void methodA(){  } } class B{ public void methodB(){ } } 

If I want to call methodA() from class B , I have to use this:

 public void methodB(){ A a = new A(); a.methodA(); // method invocation between classes } 

Argument passed between classes

This time methodA() will need an argument, and B as a field that can be used as an argument.

 class A{ public void methodA(int argument){ } } class B{ private int fieldB = 42; public void methodB(){ } } 

To call methodA() from B , you pass the argument from the class to another.

 public void methodB(){ A a= new A(); a.methodA(fieldB); //Argument passed between classes } 

Results returned between classes

And now methodA() returns the result, this is the code.

 class A{ public int methodA(){ return 42; } } class B{ private int fieldB; public void methodB(){ } } 

To use / handle the return value of methodA() from class B , you will need to do this:

 public void methodB(){ A a= new A(); fieldB = a.methodA(); //Result returned between classes } 
+9
source

I would say:

method calls between classes
Suppose you have classes X and Y It will be any class of time X calls some method in class Y

eg.

 class Y { public void foo() { } } class X { public void someMethod() { Y y = new Y(); y.foo(); } } 

arguments passed between classes
Perhaps this could mean one of two things.

Or you directly access the field of another class.

 class Y { public int number; } class X { public void someMethod() { Y y = new Y(); int yNum = y.number; } } 

Or a method was called where the arguments are given. (most likely case)

 class Y { public void foo(int arg) { } } class X { public void someMethod() { Y y = new Y(); y.foo(56); } } 

results returned between classes
Got the value from the method of another class that returned the value. for example getters or other methods.

 class Y { public int foo() { return 42; } private int number; public int getNumber() { return number; } } class X { public void someMethod() { Y y = new Y(); int yFoo = y.foo(); int yNumber = y.getNumber(); } } 
+1
source

All Articles