Specialization in Java

Specialization in Java

We discuss generalization previously where we have multiple classes with same attributes and we combine then and create a super class and all the subclasses extend that super class.

In specialization, the main programming thing is same it also contain one super class and multiple subclasses and all the super classes inherit from the super class. But the main concept is totally different from generalization. In specialization, first, we have a single main class which has many attributes and properties like if we take the same example as the generalization we have a vehicles class which has all the attributes of cars, bus, truck etc. So to maintain it, its a big issue and we can’t separate these vehicles like cars. So we divide that super class in multiple subclasses and this concept is called specialization.

specialization in java
specialization in java

Example:

First, we have a student class which contains all the student data in one place including school students and college and a university as well. University is incomplete without students.

Now as we see we have all the info of teachers, professors, and lecturers in one class but as we know they belong to a different type of institutes.  So we divide this class into subclasses according to their specifications.

But as the students info in all these classes requires their name and roll no so we can’t split it and put it in the main class and access their info from the superclass, this is called specialization.

Association in Java

has a relationship association
has a relationship association

Association is also known as Has-A relationship between two classes. The association relationship is a way of describing that a class knows about and holds a reference to another class. The relationship can be uni-directional or bi-directional with each class. Aggregation and composition are types of association relationships.

Both Composition and Aggregation are Associations:

  • Composition IS-A Association.
  • Aggregation IS-A Association.
  • The composition is a strong association.
  • Aggregation is a weak association

Example:

Like we go through a simple example in our daily life that parents has a relation with children and Childs also have a relation with parents. In other words, every parent has a child and child have parents.