Generalization And Aggregation in Java

Generalization in Java

When we talk about database mostly we come to know a thing called generalization but while doing object oriented programming this concept is also implemented.

Basic Concept of Generalization

When we have two or more than two classes with same attributes or methods and we have to write then again and again so to save our time and efforts we combine the same attributes and function of all the classes and make a super class and define them all in that class and all the other class inherit that class to use that functions and attributes.

Generalization in java
Generalization in java

Example:

Let’s take a scenario we have different vehicles like car, bus, van etc. and as we know all vehicles have Tyres, engine, door, but all these have a difference in no of seats. So instead of definition tier and engine in all the vehicles we create a super class and define common attributes here. This is called the generalization relation.

Aggregation in Java

Aggregation is Has-A relation between two classes. It makes a reference to one class according to other class.

In Java when we need a class as a variable in some other class in other words when we create an object of one class within another class then this method is called aggregation.

It is a special form of association. It is also a relationship between two classes, however, it’s a directional association, which means it is strictly a one-way association.

Example:

If we have a student class but when we create school class we know students have a relation with school so we have to define students. this is known as aggregation.

Hope you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!

In the next tutorial, you will learn about the concept of Specialization in Java.

Leave a Reply

Your email address will not be published. Required fields are marked *