Doubt on Inheritance

15

I have the following case:

Avo.java:

public class Avo {
    public String quemEuSou(){
        return this.getClass().getName();
    }
}

Mae.java:

public class Mae extends Avo{
    @Override
    public String quemEuSou() {
        return getClass().getName();
    }
}

Son.java:

public class Filho  extends Mae{

    public static void main(String[] args) {

        Filho filho = new Filho();
        Mae mae = (Mae)filho;
        Avo avo = (Avo) mae;
        System.out.println(filho.quemEuSou());
        System.out.println(mae.quemEuSou());
        System.out.println(avo.quemEuSou());

    }
}

Result:

  

Son
  Child
  Son

Because when we do the cast the class continues to be the one that was instantiated?

If it is still instantiated, how are the properties of the other classes assigned?

Example:

Filho filho2 = new Filho();
    Mae mae2 = (Mae)filho2;
mae2.algumMetodoDaClasseMae();

If the class of mae2 is Filho , where is the algumMetodoDaClasseMae() method?

    
asked by anonymous 17.02.2016 / 19:21

3 answers

16
  

Because when we cast, the class continues to be instantiated?

First it is important to understand the difference of object and reference variable. When doing:

Filho filho = new Filho();

You have created a reference variable of type Filho , this variable is called filho . This was defined before the assignment operator = .

On the right side of = you create an object of type Filho with the use of the new operator, and with the = operator you assign it the variable filho .

Breaking this line into parts would look like this:

Filho filho; //cria a variável, que não referencia ninguém
filho = new Filho(); //cria o objeto e o atribui a variável filho

That is, there is only one object in your whole code, and it is of type Filho , only you assign it to different types of reference variables, but this does not change the object type.

  

If it is still instantiated, how are the properties of the other classes assigned?

Subclasses inherit the methods and attributes that are visible to it from the superclass, if it is desirable, you can overwrite them, but it is not mandatory, unless they are abstract in the superclass.

  

If the class of mae2 is Son [...]

Correction: The object is of type Filho , since the reference variable is of type Mae .

  

[...] where is the method someDataClasseMae ()?

As explained before, subclasses inherit the attributes and methods of the superclass. As long as they are not private.

    
17.02.2016 / 19:33
12

About the nature of the example

Avô , Pai / Mãe and Filho are bad examples of inheritance and maybe that's what caused confusion.

If a class Gato inherits from class Animal , we say that Gato is Animal and this makes sense.

If I have a Animal inside a box, it makes sense to ask what kind of Animal is there . See, the reference is Animal , but the object does not change, it remains the same as before, either Gato , Cachorro or Papagaio .

On the other hand,% w / w% is Filho or Pai ? If I have a Avô , does it make sense to ask if it's also a Avô or a Filho ?

Inheritance is a relationship of type Pai and not of type é-um . A subclass is one that has everything the superclass has and something else .

Cast does not affect object

When you cast in Java, this does not change the value of the object.

Consider this way, when you assign originou-de to filho like this:

Mae mae = (Mae) filho;

You just took a mama's outfit and put it on the son. Now everyone looks at her son and they think she is the mother, but the person inside is the same, just the look is different.

Just as the child will not learn to cook a lasagna just by wearing the mother's clothing, an object does not gain or lose behavior when it is referenced by another type of variable. All that changes is the way others see that object.

Finally, the result of your program prints mae three times, and the second time it is with the clothes of the mother and the third with the clothes of the grandfather.

    
18.02.2016 / 07:33
8

This is the desirable behavior for having the polymorphism.

When casting, the class is not converted, but it can be understood as a class from which it has inherited. This is advantageous for you to be able to, for example, make an object factory.

Then you can do Car, Computer, Smartphone and have a list of objects. Your factory will be able to call methods that all objects know and each one will treat in its specialized way.

    
17.02.2016 / 19:27