OOPS | Generalization as extension and restriction using Java

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:

generalization example

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// super or parent class
class Person 
{
    static int count = 1;
    String name;
    int age;
    String gender;
}
  
// subclass or child of Person
class Student extends Person 
    // name, age and gender 
    // get inherited from Person
    int rollNo;
    String course;
}
  
// subclass of Person
class Teacher extends Person 
    // name, age and gender 
    // get inherited from Person
    static int count = 20;
    String subject;
    int experience;
}
  
class Test {
    public static void main(String[] s)
    {
        Teacher t = new Teacher();
        t.name = "Shaan";
  
        // name gets inherited in teacher
        System.out.println(t.name); 
  
         // will give priority to its own count
        System.out.println(t.count);
    }
}

chevron_right


Output:

Shaan
20

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// abstract super class
abstract class User
{
    String name;
    int age;
    String occupation;
  
    // abstract method
    abstract void fillForm(String name, String occupation); 
}
  
// non abstract sub class of User
class Doctor extends User 
{
    String specialization;
  
    // extra attributes
    int experience; 
  
    // doesn't give implementation of fillForm() method
}
class Test {
    public static void main(String[] s)
    {
        User u = new User();
  
        // extra attribute of child class so will
        // not be accessible to parent class
        u.experience = 10
          
    }
}

chevron_right


Output: 3 Errors –

  1. Doctor is not abstract and doesn’t override abstract method fillForm(String, String) in User
    class Doctor extends User
                   ^
  2. User is abstract; cannot be instantiated
      User u = new User();
                      ^
  3. cannot find symbol
     u.experience = 10;
                         ^
  4. 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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.