Interesting question about inheritance and polymorphism

3

I found this issue of a public tender interesting:

Class A

public class ClasseA {

 public int metodoX(){
   return 10
 }
 public int metodoX(int n){
   return metodoX() + n
 } 
}

Class B

public class ClasseB extends ClasseA{

  public int metodoX(){
    return 100
  }
  public int metodoX(int n){
    return metodoX() * n
  }
  public int metodoX(int m, int n){
    return metodoX(m) + metodoX()
  }
}

Home

public class Principal {
  public static void main(String[] args) {
    ClasseA obj1 = new ClasseA()
    ClasseA obj2 = new ClasseB()
    ClasseB obj3 = new ClasseB()
    int r = obj1.metodoX() + obj2.metodoX() + obj2.metodoX(5) + 
    obj3.metodoX(10, 100);
    System.out.println(r)
    }
 }

The result was 1710. I confess that I was confused and I would like some colleague to explain in detail the development of this algorithm.

    
asked by anonymous 12.10.2017 / 16:22

3 answers

3

To understand the result, you need to break down the calculation into four parts.

int r = obj1.metodoX() + obj2.metodoX() + obj2.metodoX(5) + 
obj3.metodoX(10, 100);

Part 1: obj1.metodoX()

obj1 matches the ClassA class. Running the method X () returns the value 10 .

Part 2: obj2.metodoX()

The obj2 corresponds to ClasseB . Although obj2 inherits the ClassA methods, the execution of methodX () is that of ClassB , so the value returned is 100 .

3rd Party: obj2.metodoX(5)

This call executes the classB (int n) of ClassB , which in turn returns the result of the expression metodoX() + n . The method (x) that is called in this expression is that of ClassB , which returns a value of 100, that is, the result of this execution is% which is 500 .

4th Part: 100 * 5

In the above call another method of the ClassB method that calls the methods of ClassB is executed, returning 1100 / strong>.

In this way, the final result is 10 + 100 + 500 + 1100 = 1710 .

On the issue of inheritance, note that there are ClasseA methods with the same name in ClasseB . In this example found by you, ClassB inherits methods from ClassA , but methods of ClassA were not used in classB < strong>. For this reason, all calls in ClassB refer to the methods themselves and not to those of the parent class.

In order for a ClassB method to call a ClassA method in ClassB , the call should be preceded by the super pointer.

A brief example would be:

public classe ClasseB extends ClasseA {
   public int metodoX(){
      return 100
   }

   public int metodoX(int n){
      return super.metodoX() * n
   }

   public int metodoX(int m, int n){
      return super.metodoX(m) + metodoX()
   }
}

With the example above, the inheritance would be effectively used in the code, so the end result would be different.

For overloading, this was used when classes were implemented with methods of the same name, but with different signatures.

    
12.10.2017 / 17:38
3

The explanation is as follows:

There is a concept in programming called Method overload , where you create methods with the same name, but with different signatures. Varying the number of parameters and or type of parameters .

Note the methods of ClassA, are methods with the same name methodX but with different parameters. One does not receive X () parameters and the other one receives parameter of type int method X (int n) .

public class ClasseA {

 public int metodoX(){ //assinatura sem parâmetros
   return 10
 }
 public int metodoX(int n){ //assinatura com parâmetro tipo int
   return metodoX() + n
 } 
}

Now let's explain the result:

int r = obj1.metodoX() + obj2.metodoX() + obj2.metodoX(5) + obj3.metodoX(10, 100);
int r = 10 + 100 + (100 * 5) + ((100 * 10) + 100);
int r = 110 + 500 + 1100;
int r = 1710

See the implementation of the classes with comments.

public class ClasseA {

 public int metodoX(){
   return 10
 }
 public int metodoX(int n){
   // return 10 + n
   return metodoX() + n
 } 
}

public class ClasseB extends ClasseA{

  public int metodoX(){
    return 100
  }
  public int metodoX(int n){
    // return 100 * n
    return metodoX() * n
  }
  public int metodoX(int m, int n){
    // return (100 * m) + 100
    return metodoX(m) + metodoX()
  }
}

Now if method overload is one type of polymorphism it is another conversation. link

This link has a nice discussion about it.

    
12.10.2017 / 17:08
2

So I understood the operation in r is:

int r = 10 + 100 + 500 + 1100;

Why: The "obj1.metodoX ()" is the first method of class a, where it returns 10;

The "obj2.metodoX ()" is the first method of class b, where it returns 100;

Now the "obj2.metodoX (5)" is the second method of class b, where we get the argument n = 5, times the first method of class b that returns 100, that of 500;

Finally, "obj3.metodoX (10, 100)" is the third method of class b, where the first argument m = 10 is called, the second method of class b is called, passing argument 10 ), where it returns 1000, is summed with the result of the first method of class b that is 100, result 1100, in that case the second argument is ignored the "n" because it has no application in the third function, as was passed "m "in the third function, it becomes the" n "in the second, methods in java receive the parameter and guardian in the variable the method is called as follows methodX (10);

I hope I have helped you understand.

    
12.10.2017 / 16:47