What is generalization and specialization?

4

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

    
asked by anonymous 12.07.2017 / 21:10

2 answers

4

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 compose methods and attributes in common between car and motorcycle.

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

We can then say that a car and a motorcycle can go stop and turn on the flashlights , but only the car can > lock the doors and open the windows .

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

Keywords : inheritance , composition     

12.07.2017 / 21:24
3

We can speak of the term in several contexts, even if we focus on object orientation in at least two situations can be used.

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

Generalization is a general way to define a class, it is to place the state members and behavior that all objects that conform to that specification must have, no matter if it is of a type more specific . That is, there are the essential characteristics of a group of classes that have something in common. By definition what is more general is a base class, a parent class.

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

The Liskov replacement principle explains well 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. You can not have anything special there about a specific fruit. In banana we have information that only concerns the banana.

A class pose is both generalized and specialized.

An animal class may be generalized only, but a mammalian class may be generalized because it has characteristics that all mammals generally have, as well as its base class has characteristics that all animals in general have special characteristics that only mammals have, so it is an animal specialization. We could say that a horse class could be only specialized since it derives from mammal, has its characteristics, plus the ones that are special.

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

Of course there could be new specializations of horse breeds. The only way to ensure that a class will only be specialized is to forbid the class to be derived. Not all languages have this feature. And the only way to ensure that a class will be the generalization of another class is to ensure that it will be abstract, ie it can not be directly instantiated.

But in polymorphism we can use the terms too. 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 normally expected generalized method.

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
}

So it is not only the car that is a vehicle specialization, the locomotion of the car is also a specialization of the locomotion of the vehicle. Even if in another example the vehicle locomotion is just a contract without an implementation leaving it to the car to solve it.

Generalization can be considered an intersection of all specialized classes that it is based on. And this is the problem, We can not always know all the derivatives, so it may be that the derivatives end up having specializations that are basically general, but only then do we find out, too late to fix. Worse still is when we believe that something is general when it can be specific, hurting Liskov's principle. Everything works beautifully when we have full control over the whole code, but it does cause problems when this does not happen. And object orientation is used primarily to manage maintenance. Curiously, the composition ends up being a more flexible mechanism, although it also has its problems. It does not matter, anything that can change can bring a challenge no matter the methodology adopted.

Understand that interfaces and traits are generalizations as well, not just about classes. A contract is a generalization whose implementation is a specialization.

    
14.07.2017 / 11:12