Sitemap
Avinash Dogiparthi

Avinash Dogiparthi

Be productive to yourself everyday ✨️

Generalization and Specialization

2 min readMay 18, 2023

The Generalization and Specialization relationships are both reciprocal and hierarchical. In other words, Generalization and Specialization are the inverse of each other. It only differs in the design process.

Generalization is a bottom-up approach whereas Specialization is a top-down design process.

They both belongs to inheritance.

Like parrot and sparrow specialize Bird, and Bird generalizes from parrot and sparrow.

These realtionships are hierarchical because they create a realtionship tree.

If we are moving down in the hierarchy we will get specialization and if we will go up in the hierarchy then we will get Generalization. In java, Generalization can relate to the “extends” keyword.

Get Avinash Dogiparthi’s stories in your inbox

Join Medium for free to get updates from this writer.

Note : Generalization is the process of extracting common features from two or more classes and combining them into a generalized super class. Common features can be attributes, associations, or methods.

Representation

Finally we can say, a superclass is a generalization of its subclass. Inversely, a subclass is a Specialization of its super class.

Bird.java

package com.gaurav.generalization;

public abstract class Bird {
public void fly(){
System.out.println("Birds can fly");
}

public void twoLegs(){
System.out.println("Birds have two legs");
}
}

Parrot.java

package com.gaurav.generalization;

public class Parrot extends Bird {
public String sweetVoice() {
return "Very sweet voice";
}
}

Sparrow.java

package com.gaurav.generalization;

public class Sparrow extends Bird {
public String twitterVoice() {
return "having twittering voice";
}

}

--

--

Avinash Dogiparthi
Avinash Dogiparthi

Written by Avinash Dogiparthi

Be productive to yourself everyday ✨️