Interesting question about inheritance and polymorphism

Asked

Viewed 121 times

2

I found this question of an open competition 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()
  }
}

Leading

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 presented was 1710. I confess that I was confused and I would like some colleague to explain to me in detail the development of this algorithm.

  • 1

    Could you explain where the confusion went!

  • Did you forget to put in most of the semicolons or was it the people who did the contest that made them?

  • @Victorstafusa I was like this, I just copied the paste.

  • Hahaha, so the correct answer would be "compiler error" and you could file an appeal to dismiss the matter. If you consider that missing semicolons would be added, then you would give 1710 and the answers below already explain why.

  • @Virgilionovic The instantiation "Classea obj2 = new Classeb()", because they did so?

  • 1

    @Deniscaixeta Remember that ClasseB inherits from ClasseA, then a variable of type ClasseA can store an object of the type ClasseB. The methods that can be called are those of the variable type (ClasseA), but the methods that are actually called are those of the object in it (ClasseB). That is to say, obj2.metodoX(1, 2) is a build error, but obj2.metodoX() calls the method metodoX() of ClasseB.

  • Okay, I get it. Thank you.

  • @Deniscaixeta in this specific case ClasseA in the instance of the variable obj2 is the guy, but, its instance is of ClasseB which inherited the behaviour of ClasseA, although in this the expected behavior is the ClasseB because he rewrote the methods metodoX() and metodoX(int n). The method contained in Classeb: metodoX(int m, int n) is not part of the ClasseA, so it doesn’t even show up. It’s a good question many who work with POO doesn’t know it, and it was really cool to post your doubt, congratulations.

Show 3 more comments

3 answers

2

The explanation is as follows:

There is a concept in programming called Overload of methods, 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 Classea, are methods with the same name metodoX but with different parameters. One does not receive parameters metodoX() and the other takes parameter of type int metodoX(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

View 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 overloading methods is a kind of polymorphism is another conversation. http://www.guj.com.br/t/sobrecarga-e-um-exemplo-de-polimorfismo/37028

This link has a nice discussion about.

2


To understand the result it is necessary to dismember the calculation below in four parts.

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

Part 1: obj1.metodoX()

The obj1 corresponds to the class Classea. The execution of metodoX() returns the value 10.

Part 2: obj2.metodoX()

The obj2 corresponds to Classeb. Despite the obj2 inherit the methods of Classea, the execution of metodoX() is that of Classeb, so that the returned value is 100.

Part 3: obj2.metodoX(5)

This call executes the metodoX(int n) of Classeb, which in turn returns the result of the expression metodoX() + n. The metodoX() called in that expression is that of Classeb, that returns the value 100, that is, the result of this execution is 100 * 5 that is 500.

4th part: obj3.metodoX(10, 100)

In the above call another method of the Classeb calling to the methods of Classeb, returning the value 1100.

So the end result is 10 + 100 + 500 + 1100 = 1710.

On the issue of inheritance, note that there are methods of Classea with the same name in Classeb. In this example found by you, the Classeb inherits the methods of Classea, but the methods of Classea were not used in the classeB. For this reason, all calls on Classeb refer to the methods themselves and not to those of the parent class.

So that, in the classeB a call to a method "metodoX()" of Classea, the call should be preceded by the pointer super.

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 above example, the inheritance would be effectively used in the code, so that the final result would be different.

Regarding the overload, it was used when the classes were implemented with methods of the same name, but with different signatures.

  • The doubt was really about the inheritance, the example he gave is very good.

1

From what I understand the operation in r is:

int r = 10 + 100 + 500 + 1100;

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

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

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

Lastly "obj3.metodoX(10, 100)" is the third method of class b, where the first argument m=10 is taken, the second method of class b is called, passing argument 10 (I explain it already), where it returns 1000, is added with the result of the first method of class b which is 100, result 1100, in this case the second argument "n" is ignored because it has no application in the third function, as was passed the argument "m" in the third function, it becomes the "n" in the second, methods in java receive the parameter and save in the variable the method is called in the following way methodoX(10);

I hope I helped you understand.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.