Thursday, April 13, 2017

what is generalization - Specialization

        Generalization and Specialization both are example of  Inheritance

Generalization and Specialization both are example of Inheritance both showing IS-A relationship.

When super-classes shared their features with different sub-classes is known as specialization. It is top-down approach in Inheritance

When more than one sub classes extract their common features and combine them in a one supper class is known as generalization. It is bottom up approach in inheritance
Specialization IS-A relationship

Example: 
Suppose we have a supper class “Person” it has two sub classes “Student” and “Teacher”. Now person having some common attributes like gender, address, age, etc. In general Student is a Person,Student also having the same common attribute of a Person and also some own personal attributes. So, that’s why we say Student IS-A Person or Students extends Person or Students Inherit the Person or Student become a child (sub class) of parent (supper class) Person. The same thing with teacher. Now this is showing Specialization, because a Super class Person shared their properties with Sub Classes Student and Teacher.

Now take same thing but in reverse order like say sub Classes Student and Teacher both having some common attributes, now we extract that common features from both sub classes and combine them to make a Super Class Person this is known as Generalization.
Specialization 

Association HAS-A Relationship

Generally, association is a ‘HAS-A’ relation. In simple word when one object is related to other object to use functionality and services of that object. So, these types of relation between two objects is known as association.

Association provides us one to one, one to many or many to one and many to many relationships between objects. Association has two special forms
o   Aggregation
o   Composition

Specialization

Aggregation
Aggregation is a special form of association and it is week relation of association. when an object ‘HAS-A’ another object, but both are independent to each other is known as an Aggregation.

For example
If we have two classes one is Studentand second is School, now if School is close, then Student still exist. It may change the school. Same way if Student leave the School, then may be another Student take the place of that student. So notice that they both are independent of each other this is known as Aggregation.



Composition strong relation

Composition is a special form of association and it is strong relation of association. when an object ‘HAS-A’ another object and any one object depend upon other object this is known as composition.

For Example:
If we have three Classes which are Human, Hand and Leg. Now in general Human Has-A hand and Human Has-A Leg. If a human has no Hand or has no Leg than that Human will be alive, but Human itself destroy than its Hand and Leg are also destroy because these parts depend upon Human. So notice that Human is independent Class and hand and Leg are dependent Classes. So, this type of association is known as Composition.

Example of Aggregation
We have two classes Employee and Address, Employee HAS-A Address.
Specialization

Address Class:


generalization

In address class I create three String objects city, state and country. Generally, every employee has an address so that’s why I create an address class with it constructor to initialize the value. In constructor I use this keyword, but be confused with this keyword. I will explain it in This Keyword section, right now just understand that This keyword refers the current instance of the class.

Employee Class

generalization



No comments:

Post a Comment