Skip to content

Inheritance-Abstract-GUI
Inheritance Abstract - GUI

Third and last step on the inheritance: here to describe a situation where this mechanism is required, or better forced, to be adopted, in case we need to create object from a class.

What precisely does it mean?
We always have designed and realized classes with the clear intention to be used as direct prototypes for instantiation of objects.

And that's right; but if you remember we, by talking about inheritance in the two last articles, mentioned that when a method in a class is declared as virtual, it means that every child of the class can override that method, but never it will be forced to do.

Let's face the other side of the medal: how to make necessary a redeclaration by every child of a particular class?
But before the practice, what is the idea guiding us?

Try to think of a general class, so general that it cannot be directly turned into reality: no need to concentrate on informatics applications, the physical reality of every day can help us.

...continue reading "Inheritance. The abstract classes"

Inheritance2-GUI
Inheritance 2 - GUI

By approaching with the concept and mechanism of inheritance it is possible to notice how it guides us from more generic classes into specialized ones, based on the project's specifics.

Inheritance is potentially infinite; only our needs (and computer's memory) can put a stop to the chain of children.
In any case what we learned is that the public and private attributes are inherited as they are: respectively public and private.
[But while the former are accessible, the latter aren't!]

The other aspect we talked about relates to necessity of specializing the new classes with new attributes and/or methods: a special setting wants a method to be shared among the father and the children classes, so that it can be declared virtual in the first.
It's required when a method, enough for the father which is initially designed for, becomes a base for the children where to be redeclared.
In the last article we saw the printing method, for the employees and the workers: a two-levels hierarchy, the simplest case of inheritance.

And if you remember (but you read it first? :)) we avoided to add a button, onto the form, bound to the redeclared method in the Worker class, because it would have been redundant; in facts it would have been same as using the direct Worker's printing method.

...continue reading "Inheritance. The protected attributes"

Inheritance1-GUI
Inheritance 1 - GUI

The principle of inheritance involves the encapsulation mechanism too; so that we must pay attention to its usage.

In facts when we design a class and decide which items must be public or not, we bind them to precise ranges of action.

But now we add another degree in decision, because of the hypothetical necessity to design a class which at least to derive another one from.
This means that the inheriting classes receive all the parent's features exactly as they're written: private stuff to private, public stuff to public.

A last choice exists, under the protected zone (see in any class code we publish); but we'll see it next time.

But... why to inherit a class?

Certainly not to have just a copy, because it would be useless.

Surely to specialize the original class, called father, into a new one, called child.

A common example is the human being, which can become man and woman, for example.

...continue reading "Inheritance. A first insight"

Aggregation-GUI
Aggregation GUI

By discovering the beauty of OOP world, in the essential view here offered, we always have underlined how the classes are designed as objects prototypes which implicitly include the possibility to communicate among them, with precise mechanisms.

For the sake of precision, some classes can be written only as general model, without the goal to be transposed in reality as objects.
While this is true, for now let's concentrate only on the fact that a class implies an instantiation, at least.

So the class design at this level requires the knowledge of the relations that its objects must set.

Previous time we saw the composition, between the two involved classes Employee and Document.

To summarize: while an employee object can exist by itself and can have one or more documents, the document object cannot exist by itself and can only belong to an employee.

...continue reading "Aggregation of Classes"

Composition-GUI
Composition GUI

A golden rule in coding is the modularization: the bigger your project is, the more difficult it's to keep it in a mono structure. You're naturally brought to separate logically and even in different files the code portions which are separable.

When you start to design your software in the OOP vision you surely must face these situations, in a different way: to get order inside an initial nebulous idea it's so important to recognize the who/what!

Easy to say, but only the practice and a good eyes can help you: in a big series of data your job is to assign roles and hierarchies, connections and relationships. In facts, as we said in the OOP introduction, over the who/what boxes there are the intercommunications, which are essential parts of them.

Here we show one kind, the so called composition.

Generally a composition means a concurrence of factors bound one each other, whose result is given by the factors themselves and the proportions they express: a typical example is a music composition.
In some contexts a composition defines the manner in which something is composed; not the result but the process behind.
Every meaning you find it includes the concept of relationship between two "actors", at least.

This is the point: which kind of hierarchy does the composition imply?

...continue reading "Composition of Classes"