joydip_kanjilal
Contributor

Exploring generalization, specialization, and dependency in OOP

opinion
Feb 10, 20164 mins
Software Development

Generalization, specialization, and dependency define relationships between the objects in your application

OOP (object oriented programming) is a paradigm that is centered on objects and data rather than actions and logic. When working with OOP, it is imperative that you identify the objects and their relationships.

In OOP, a problem is decomposed into a number of objects and how they relate to one another — a process known as data modelling. The essential relationships between objects include: association, generalization, specialization, aggregation, dependency and composition. In this article we would discuss dependency and inheritance relationships in OOP with code examples in C# to illustrate the concepts.

Dependency

A dependency is a relationship between two or more objects in which an object depends on the other object or objects for its implementation. If one of these objects change, the other object(s) can be impacted. The dependency relationship between two or more objects is depicted in UML using dashed arrows. In other words, when a dependency relationship exists between two or more objects, the object needs to know about the other object(s) which it depends on.

Consider the classes IDGBlogEntry and IDGView. While the former contains all related information related to the blog entries, the latter is concerned with displaying the data received from the IDGBlogEntry class onto the user interface. So, the IDGView class is dependent on the IDGBlogEntry class to display contents (blog entries) in the user interface. Hence there exists a dependency relationship between the IDGView and IDGBlogEntry classes. A dependency relationship is represented in UML using a dashed arrow.

public class IDGBlogEntry

    {

        //Members of the IDGBlogEntry class

    }

public class IDGView

    {

        //Members of the IDGView class

    }

Generalization and specialization

Generalization may be defined as the technique of extracting the essential characteristics (these include attributes, properties and methods) from two or more subclasses and then combining them inside a generalized base class (also called a superclass). On the contrary, specialization is the reverse of generalization — it’s used to represent “type-of” relationship by creating subclasses from existing base classes.

Inheritance is defined as the ability of a class to extend one or more classes (also known as base classes). Note that generalization is the strongest form of class relationships as the classes participating in a generalization relationship are tightly coupled with each other — most of the internal intricacies of the parent class are visible to the child class.

The class that extends the base or parent class is also known as the child class or the derived class. The inherited or generalized class extends or inherits its base or parent class. In inheritance, a child class inherits the methods and attributes of the base or parent class except those that are private. In essence, the private members of the base class are not inherited as they belong “solely” to the class they are part of. Hence, you should take advantage of generalization only when you need to represent a class that is actually a more specialized form of another class.

Inheritance is of the following types:

  • Single
  • Multiple
  • Multilevel
  • Hierarchical
  • Hybrid

Single inheritance is the simplest form of inheritance in which one class extends another class. The following code snippet illustrates this form of inheritance — note how the IDGBlogAuthor class extends the IDGAuthor class.

public class IDGAuthor

    {

        //Members of the IDGAuthor class

    }

public class IDGBlogAuthor : IDGAuthor

    {

        //Members of the IDGBlogAuthor class

    }

In multiple inheritance you have multiple base classes from which a class is derived. Note that multiple inheritance is not supported in OOP programming languages like Java or C#.

The next type of inheritance in our list is multi-level inheritance. In this form of inheritance you have classes inherited from one another to form a chain. The following code snippet illustrates this.

public class Person

    {

        //Members of the Person class

    }

public class IDGAuthor : Person

    {

        //Members of the IDGAuthor class

    }

public class IDGBlogAuthor : IDGAuthor

    {

        //Members of the IDGBlogAuthor class

    }

In hierarchical inheritance you have classes that represent a hierarchical structure through inheritance, similar to a family tree. In this type of inheritance, you have more than one child class having the same base or parent class. In other words, this is a type of inheritance in which one or more derived class has a common base or parent class.

Hybrid inheritance is a type of inheritance in which two or more forms of inheritance are combined into one. Essentially, this type of inheritance is a combination of two or more forms of inheritance to form a closed structure. Note that Hybrid inheritance is also not supported in OO programming languages like C# or Java.

joydip_kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author