Generalization is the process of taking out common properties and functionalities from two or more classes and combining them together into another class which acts as the parent class of those classes or what we may say the generalized class of those specialized classes. All the subclasses are a type of superclass. So we can say that subclass “is-A” superclass. Therefore Generalization is termed as “is-A relationship”
Here is an example of generalization:
In this figure, we see that there are two types of flights so we made a flight class which will contain common properties and then we has an international and domestic class which are an extension of flight class and will have properties of flight as well as their own. Here flight is the parent/superclass and the other two are child/subclass. International Flight “is-a” flight as well as the domestic flight.
Example 1: Generalization as extension
Explanation: We had two classes Teacher and Student which have common properties like name, age, and gender so we made another class Person which has these properties and then extended these two from it. Now when we are creating an object of teacher class then we have all the properties of person class also. Here we see that the teacher class has its own value of count so this will hide parent’s value. We have not given “name” property in teacher class but still, we are using it. This is because all the attributes and methods of parent class get inherited into the child class. We have given additional properties in teacher class which belong to teacher class only. These additional properties show the extension that generalization provides to the child class.
Example 2: Generalization as restriction
Output: 3 Errors –
- Doctor is not abstract and doesn’t override abstract method fillForm(String, String) in User
class Doctor extends User ^
- User is abstract; cannot be instantiated
User u = new User(); ^
- cannot find symbol
u.experience = 10; ^
symbol: variable experience
location: variable u of type User
Explanation: User is an abstract class which has an abstract methodfillForm() because the form will be different for a patient and for doctor so filling form will be different. Now doctor class either has to provide its body or has to declare itself abstract. This is the restriction that superclass poses on its child class that the child class has to inherit all its properties and functionalities and cannot skip any. They may provide another implementation by overriding the existing characteristics but cannot skip it. In this case, this will generate compilation error because doctor is neither abstract nor it has given body for fillform() method. Another reason is that User class doesn’t have experience attribute it is in Doctor class only so user cannot access it.
In the examples above, we see that a child class has to inherit all the characteristics from its parent although it may change its implementation by overriding it it cannot neglect any characteristic of the parent. This shows the restrictive form of generalization.
We also see that the child class has its own properties which the parent class object cannot use because these extended properties belong to a child only. This addition of extra properties shows
an extensible form of generalization.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Generalization and Specialization in Java
- Object Oriented Programming (OOPs) Concept in Java
- Difference between Generalization and Specialization in DBMS
- How to remove extension from string in PHP?
- Kotlin extension function
- How to remove .html extension from URL of a static page ?
- How to enable PHP's openssl extension to install Composer ?
- Perl | Objects in OOPs
- Perl | Polymorphism in OOPs
- Perl | Inheritance in OOPs
- Perl | Encapsulation in OOPs
- OOPs | Object Oriented Design
- Perl | Method Overriding in OOPs
- Messages, aggregation and abstract classes in OOPS
- Understanding OOPs and Abstraction using Real World Scenario
- Understanding Encapsulation, Inheritance, Polymorphism, Abstraction in OOPs
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.