The knowledge sharing zone - Guruzon.com

What is Inheritance?

This page contains the details about What is Inheritance, What is IS-A relationship and Its representation with UML Diagram notations and When to use Inheritance.
reviews
  • Inheritance and composition plays major roles in the design of object-oriented systems.
  • Many of the most complex and interesting design decisions come down to deciding between inheritance and composition.
  • Both inheritance and composition provides mechanisms for RE-USE.

What is inheritance

  • Inheritance as its name implies, involves inheriting attributes and behaviors from other classes.
  • Inheritance represents the is-a relationship between two entities or objects.
  • There is a true parent/child relationship and The child (or subclass) inherits directly from the parent (or superclass).
  • Remember that Composition is also as its name implies, involved building objects by using other objects. 

Inheritance example

  • One of the most powerful featureof Object Oriented programming is code Reusability.
  • Structured design provides code reuse to a certain extent hence you can write a procedure and then use it as many times as you want.
  • Inheritance also helps by organizing classes and factoring in commonalties of various classes.
  • Inheritance is a primary means of providing this functionality.
  • Inheritance allows a class to inherit the attributes and methods of another class.
  • This allows creation of brand new classes by abstracting out common attributes and behaviors.
  • One of the major design issues in OO programming is to factor out commonality of the various classes.

Inheritance UML notation example

  • You have a Dog class and a Cat class,and each will have an attribute for eye color.
  • In a procedural model,the code for Dog and Cat would each contain this attribute.
  • In an OO design, the color attribute could be moved up to a class called Mammal along with any other common attributes and methods.
  • In this case, both Dog and Cat inherit from the Mammal class,as shown below:

  • The Dog and Cat classes both inherit from Mammal.
  • This means that a Dog class actually has the following attributes:

eyeColor         // inherited from Mammal 
barkFrequency    // defined only for Dogs
In the same vein, Dog object has the following methods:
getEyeColor    // inherited from Mammal 
bark         // defined only for Dogs

Inheritance represents Is-a Relationships

  • For example there is a parent class (Shape Class) and Circle,Square and Star all inherit directly from Shape Class.
  • This relationship is often referred to as an is-a relationship because a circle is a shape, Square is a shape and Star is a shape.
  • When a subclass inherits from a super class, it can do anything that the superclass can do.
  • Thus, Circle,Square and Star are all extensions of Shape.
  • In below figure, the name on each of the objects represents the Draw method for the Circle,Star and Square objects respectively.
  • When we design this Shape system it would be very helpful to standardize how we use the various shapes.
  • This way we could decide that if we want to draw a shape, No matter what shape, we will invoke a method called draw.

Advantages of Inheritance

  • There are two major benefits to inheritance. 
  • The First, we can specialize existing classes (types) and write only the code that is changed. 
  • In this way, we can reuse our existing classes (as base classes) and capitalize on the work already done. 
  • We no longer have to "copy and paste" code. 
  • Copying and pasting is particularly problematic because changes in one section of the code must be replicated in another—a dangerous and failure-prone model. 
  • Instead, by using Inheritance, we can make changes in the base class that automatically update all the derived classes.
  • The second significant benefit of inheritance is that it allows us to treat the derived objects polymorphically. 
  • Poly means many, morph means form - polymorphism means the ability to take many forms.

You would also like to read:

What is Aggregation?
What is Composition?
Aggregation Vs Composition Vs Association
What is Realization?
What is Association?
Difference between IS-A and HAS-A relationships
What is Generalization?

Comments :


Perfectly Explained Inheritance.Nice collection of Java related technology in easy language.

up vote icon down vote icon
Name :
Email :
Comment :
Verify Text :
capch image, refresh page if not loaded somehow