sexta-feira, 13 de maio de 2016

About superclass, heritage, constructors and....ONIONS !

This week I was studying constructors, ie the code that runs when we write "new" in a class type. In OO (object oriented), a class will always have a constructor, whether you create or not. If you do not enter a constructor in your class, the compiler - ah, our great friend compiler - deliberately inserts a default constructor, that is, with no arguments.

If you take the trouble to insert a constructor - any constructor - the compiler friend not only will not create the default constructor (no arguments), but also understand that this task is your responsibility.

Ok. So far I could understand what was said about Java constructors, but what does this have to do with onions?

Wait just a little: we will get onions.

First, let's talk about the superclass Object, highest branch of any family tree or inheritance tree of an object. This superclass will always be present in the creation of an object, which means that any object will always have parts inherent to the object and parts on the part of this superclass.
This may seem complicated means, said so directly, but if we understand, in the question inheritance, an object in their DNA (class) is formed by proper methods and methods inherited from its superclass (which can be superimposed or not) we began to mentally draw the layers of our onions.

Onion? Yes, onions. The inheritance tree is perfect to understand polymorphism and interfaces. But the representation of that tree on object level helps us to understand the "chain of constructors" and to realize that this linkage occurs in layers, remembering exactly the figure of an onion.

A Dog object IS-AN Animal object and is A-type Object. If you want to create a Dog object, you must also have to create parts related to Animal and Object.

The onion, the layers can be drawn with its methods or its instance variables. The most central part, ie the core onion, contain the variables and / or methods relating to the part Object (common to all objects existing in OO world). The middle part will house all related to the Animal part. Last but not least, the outer part will contain a layer of onion referring to the part of Dog. Objects reside in the Collection, so the basket of onions is the same Collection.

Finally, we must record what is happening on the distant world of the stack, where the call methods and local variables reside.

When the code of some class uses new Dog (), the Dog () constructor will be inserted at the top of the stack. Dog () calls the superclass constructor that forces the entrance of Animal () constructor at the top of the stack. Animal () calls the superclass constructor to force the entry of the Object () constructor at the top of the stack, since Object is the superclass Animals.

Running Object () is completed and your pointer is removed from the stack. The same goes for Animal (), Dog () until the full implementation of the builders. Wow! It seems that a complex object Dog was born!

Well, perhaps this view of the stack may have helped to understand the layers of the onion, ops!, of the objects. Now it is up to you to make and season your salad. 
Bon apetit!

Leave your comment or question. I'll have a great pleasure to discuss the text with you.

Nenhum comentário:

Postar um comentário