Generalization and Specialization in C++:
In this article, I am going to discuss Generalization and Specialization in C++ with Examples. Please read our previous article where we discussed Modes of Inheritance in C++ with Examples.
Generalization and Specialization in C++
This is a very interesting topic that is generalization vs specialization. This is related to inheritance. So, what does this means? Let us understand. Let’s say we have a class Rectangle. Then, can you create the object of Rectangle? Yes, you can create and you can use it and call the functions of the Rectangle class. Next, we have a class Cuboid which is inherited from the Rectangle.
For Cuboid also we can create the objects and call the functions of Cuboid. These two things i.e. Rectangle and Cuboid really exist in the world. Can you show me a rectangle? Yes, you can show me some shapes that is a rectangular or a paper that is rectangular or windowpane. So, rectangles exist in the real world.
And what about ‘Cuboid’? Yes, it also exists in the real world. Any box shape thing is a cuboid. So, these two things exist in the real world. Now in this. if we say, which was existing first in our example? The Rectangle was existing. From there we have derived a Cuboid. So, the Rectangle class is already existing and we have defined a new class with extra features so we have a specialized class that is Cuboid. This is a specialization in C++.
Let us take another example of cars. We have a class Innova, Innova car from Toyota company. Then there is another class called Fortuner.
If you know about these then you should know that first Innova was launched by Toyota and then a new car Fortuner was launched which is an extension of Innova. As a common man if you see Fortuner as an extension of Innova then it is having same seating capacity and with a lot of extra features in that one. So, it means that the Fortuner car is an extension of the Innova car.
Innova is a base class and Fortuner is a derived class. Innova was already existing and Fortuner is the specialized Innova. This is specialization. Something is already existing then you are deriving something from that existing one and defining a new class. This is specialization. In the real world, we can take a lot of examples of specialization. Now let us take another example.
We have 3 classes that are Square, Rectangle, and Circle. Then for all of these, we are defining one class that is Shape. And Square, Rectangle, and Circle are inheriting from Shape.
So, among these classes which one is existing first? We know all the shapes i.e. square, rectangle and circle really exist in the real world. So, we are calling them shapes.
Do you think Shape really exists in the real world? No. It is just a virtual term. If I asked you to show me some shape then you’ll be drawing a rectangle, a circle, a square, a Rhombus, etc. but you cannot show me just a shape. So, the shape is what you’re drawing. The Shape is a term or generic term or generalized term.
When these child classes that are already existing then to bring them on a common platform, we have given the word Shape which is a logical term, it is not a real term. Can you find the area or perimeter of a rectangle? Yes. Can you find the area or perimeter of the square? Yes. Can you find the area or perimeter of the circle? Yes. Does every shape have an area and perimeter? Yes.
This is a common feature we have brought into shape. Now can you calculate the area and perimeter of the Shape? No, you can’t. There are no dimensions for shape. It’s a generalized term. So, this is an example of Generalization in C++.
Let us take one more example similar to this one. Suppose we have Innova, BMW, and Audi. So, we can start, stop, accelerate, and change gears in any of these. So, what these are all? These all are cars. Innova, BMW, and Audi are physically available in the real world. All these are nothing but cars.
So, they all are inheriting from Cars. What can you do in a car? You can drive, start, stop, etc. all these features are available in these cars. Then what about Cars? Does it really exist? No, it is a general term. Just a general term that is the logical definition of a group of things.
So, we have given a superclass to a group of classes that is base class easy for refereeing something. It is just like if I am looking at Innova so I can say it is a car. So, for many 4-wheelers we use the term, Car.
We are using this term for refereeing them. We don’t have to know the brand name or the product name perfectly. When it looks like a car, we can say that’s a car. So, in the real world or in daily life we define such general terms.
So, we conclude here. This is what inheritance. We are inheriting from a base class. So, we have seen four examples in which 2 are of specialization and 2 are of generalization. In specialization, the parent was exists and the child was defined later. In generalization, the child class was existing then we define the base class. So, specialization is a top-down approach and generalization is a bottom-up approach.
In specialization, the basic class has something to give to the child class whereas, in generalization, the base classes don’t have anything to give to their child classes. Just their purpose is to group them together so that we can easily manage all those things.
The last thing that I have to tell you is why did we achieve generalization? We are using a general term to refereeing a thing. So, this is nothing but polymorphism. Same name but different objects or different actions or different things. The actions are different but the name is the same.
The purpose of generalization is to achieve polymorphism and the purpose of specialization is to share its features with its child classes.
So, there are two purposes of inheritance. One is to share features with child classes and the second one is to achieve polymorphism. So here I have given you a clear idea of what is a specialization and what is a generalization.
What is Generalization?
The process of extracting common characteristics from two or more classes and combining them into a generalized superclass is called Generalization. The common characteristics can be attributes or methods.
What is Specialization?
Specialization is the reverse process of Generalization means creating new subclasses from an existing class. Specialization is the process of dividing a parent-level entity into narrower categories accordingly to all the possible child categories. By having the behavior of the opposite of the generalization process, specialization requires the separation of entities based on certain uncommon attributes.
In the next article, I am going to discuss Base Class Pointer Derived Class Object in C++ with Examples. Here, in this article, I try to explain Generalization and Specialization in C++ with Examples and I hope you enjoy this Generalization and Specialization in C++ article. I would like to have your feedback. Please post your feedback, question, or comments about this article.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.