What is generalization and specialization?

Asked

Viewed 6,999 times

4

In POO, what is generalization and specialization? Would there be some explanation in some technical way and a not so technical way? Simpler to understand, etc.

2 answers

9


Nothing better to explain this as an example, see below:

Veiculo {
    function locomover()

    function parar()

    function ligarLanternas()
}

Carro extends Veiculo
{
    function travarPortas()

    function abrirVidros()
}

Moto extends Veiculo
{
    //Nenhuma particularidade
}

Vehicle would be a generalization of a class that make up methods and attributes in common between car and bike.

Car would be a specification of vehicle, as well as inheriting the components still specifies new functions and attributes.

We can then say that a car and a motorcycle can be locomover, stop and turn on the lanterns, but only the car can lock the doors and open the windows.

In short, generalization would be a abstraction between classes performing similar functions. While the specification is in fact a specification of that class that has been abstracted (or even of a similar class, which has the same functions).

Keywords: inheritance, composition

4

We can talk about the term in several contexts, even if we stick to object orientation at least in two situations can be used.

In the inheritance the accepted answer already says well what is.

Generalization is a form general to define a class, is to place members of state and behavior that all objects conforming to that specification must have, no matter if it is of a more specific. That is, it contains the essential characteristics of a group of classes that have something in common. By definition what is most general is a base class, a mother class.

Specialization is by definition a derived class, daughter. It possesses characteristics that only pertain to that class. There are members that are special for that class.

The the principle of Liskov’s substitution explains well about the fact that a specialized object has to be the same as a generalized object.

So a banana is a specialization of a fruit. In fruit we have everything we need to know about any fruit. There can be nothing special there about a specific fruit. In banana we have information that only concerns the banana.

A class can be both widespread and specialized.

An animal class may only be generalized, but a mammal class may be: a) generalized because it has characteristics that all mammals in general possess, as well as its base class has characteristics that all animals in general possess, but also has characteristics special which only mammals possess; then b) it is an animal specialization. We could say that a horse class could be only specialized since it derives from mammals, possesses its characteristics, but its own that are special.

In some cases specialization may be expected, but not adequate. No link above show that it is tempting to consider that a rectangle is a specialization of square, but by some detail it is not. There is a situation in the class which cannot be guaranteed that all the general precepts set out in the base class are completely conformed to the derived class, so there is no specialization in that relation. And that’s one of the reasons why inheritance isn’t always a good idea. We often see something as a generalization and specialization relationship when it is not, or one day it may cease to be.

Of course there could be new specializations of horse breeds. The only way to ensure that a class will only specialize is to prohibit the class from being derived. Not all languages have this feature. And the only way to ensure that one class will be the generalization of another class is to ensure that it will be abstract, that is, it cannot be instantiated directly.

But in polymorphism we can use the terms as well. If you have a method in a general class, we can call this method generalized, and the polymorphic method in the specific class is the specialized method. Each will have its own implementation, possibly the specialized method calling the generalized to complement its execution. So in polymorphic situations the specialized method replaces the generalised method normally expected.

class Veiculo {
    locomover() { ... } 
    parar() { ... } 
    ligarLanternas() { ... } 
}

class Carro extends Veiculo {
    travarPortas() { ... } //especialização porque é característica própria

    locomover() { ... } //especialização porque o comportamento é próprio
    parar() { ... } //tudo aqui é diferente do implementado na classe generalizada
    //note que ligarLanternas() não foi especializado, o comportamento é o geral mesmo
}

I put in the Github for future reference.

So it’s not just the car that is a vehicle specialization, the locomotion of the car is also a specialization of the locomotion of the vehicle. It may even be that in another example the vehicle locomotion is only a contract without an implementation leaving in charge of the car solve it.

Generalization can be considered an intersection of all the specialized classes it is based on. And that’s the problem, we can’t always know all derivatives, so it may be that derivatives end up having specializations that are basically general, but only then do we find out, too late to tidy up. Even worse is when we believe that something is general when it can be specific, hurting the Liskov principle. Everything works great when we have total control over all the code, but it brings problems when it doesn’t occur. And object orientation is primarily used to manage maintenance. Interestingly, the composition ends up being a more flexible mechanism, although it also has its problems. No use, anything that can change can bring a challenge no matter the methodology adopted.

Understand which interfaces and traits are still generalizations too, it’s not just about classes. A contract is a generalization whose implementation is a specialization.

Browser other questions tagged

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