Doubt about Inheritance

Asked

Viewed 590 times

15

I have the following case:

Avo.:

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

Mae.java:

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

Java son.:

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());

    }
}

Upshot:

Son
Son
Son

Because when we do cast the class remains the one that was instantiated?

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

Example:

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

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

3 answers

17


Because when we cast the class remains the one that was instantiated?

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

Filho filho = new Filho();

You created a reference variable of type Filho, this variable is called filho. That was what was set before the allocation operator =.

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

Breaking that line into pieces 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

I mean, there’s only one object in his whole code, and he’s like, Filho, only that you assign it to different types of reference variables, but this does not change the type of the object.

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

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

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

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

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

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

  • 1

    Excellent explanation @Math♦

13

On the nature of the example

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

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

If I have a Animal inside a box, it makes perfect sense for someone 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, be Gato, Cachorro or Papagaio.

On the other hand, a Filho is a Pai or Avô? If I have a Avô, it makes sense for me to ask if he’s also a Filho or a Pai?

Inheritance is a relationship of the kind é-um and not the type originou-de. A subclass is the one who has everything the superclass has and anything else.

Cast does not affect the object

When you cast in Java, it doesn’t change the value of the object at all.

Think as follows, when you assign filho for mae thus:

Mae mae = (Mae) filho;

You just took a little dress from Mom and put it on her son. Now everyone looks at their child and thinks she’s the mother, but the person inside is the same, only the look is different.

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

Finally, the result of your program prints Filho three times, being that the second time he is with his mother’s clothes and the third with his grandfather’s clothes.

  • 1

    +1 for lasagna, kkkkkk

  • 1

    The expression "inheritance" is somewhat confusing. I prefer the term "extension" in the sense that A is an extension of the concept of B (after all, classes represent concepts), but it’s a little late to change the name of things. :)

  • 1

    @Pablo Makes sense. I think the term inheritance was created to make an analogy with a known concept and the result is not very good. And I believe that you can replace in general by "extension without much harm, after all, even Java uses the keyword "extends". It seems to me that the creator of language knew well what we are dealing with here.

8

This is the desirable behavior to have polymorphism.

When casting, the class is not converted, but it can be understood as a class from which it inherited. This is advantageous so that you can, 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 their own specialized way.

Browser other questions tagged

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