RSS

OOP Concepts – Inheritance !!

09 May

What Is Inheritance?

Introduction:

Inheritance, together with encapsulation and polymorphism, is one of the three primary characteristics (or pillars) of object-oriented programming. Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.

Conceptually, a derived class is a specialization of the base class. For example, if you have a base class Animal, you might have one derived class that is named Mammal and another derived class that is named Reptile. A Mammal is an Animal, and a Reptile is an Animal, but each derived class represents different specializations of the base class.

When you define a class to derive from another class, the derived class implicitly gains all the members of the base class, except for its constructors and destructors. The derived class can thereby reuse the code in the base class without having to re-implement it. In the derived class, you can add more members. In this manner, the derived class extends the functionality of the base class.

Inheritance is a key concept in the world of object orientation. You can use inheritance as a tool to avoid repetition when you are defining different classes that have several features in common and are related to each other. Perhaps they are different subclasses of the same type, each with its own distinguishing feature—for example, managers and manual workers are all employees of a factory. If you were writing an application to simulate the factory, how would you specify that managers and manual workers have several features that are the same, but also other features that are different? For example, they all have an employee reference number and a name, but managers have different responsibilities and perform different tasks from manual workers.

As a solution, you might develop a class to represent an employee. This class can include fields to hold information that is common to all employees, such as the employee reference number and name. You can then develop a class to represent a manager and another class to represent a manual worker. Both the manager class and the manual worker class would also need to store the employee reference number and name. Rather than adding duplicate fields to each of these classes, you can simply specify that both of these classes inherit from the employee class, which already has fields to store these values. In addition, there might be some behavior that is common to managers and manual workers. You can implement this behavior as a method in the employee class. The manager and manual worker classes can then inherit this behavior. Using inheritance in this way reduces the need for code duplication, which reduces both development time and the risk of bugs being introduced. You can add additional, specific fields and methods to the manager and manual worker classes to model the different data and behaviors of these types.

Defining an Inherited Class

You specify that a class inherits from another class (called a base class) by using a colon and providing the name of the base class. Remember that all of the members in the base class are private by default. This means that they cannot be accessed by code in other classes, including classes that inherit from the base class. You can make the members of a base class visible to inheriting classes, but keep them hidden from other classes that are not part of the inheritance hierarchy by using the protected keyword, as the following code example shows.

// Base class
class Employee
{
protected string empNum;
protected string empName;
protected void DoWork()
{ … }
}

// Inheriting classes
class Manager : Employee
{
public void DoManagementWork()
{… }
}

class ManualWorker : Employee
{
public void DoManualWork()
{ … }
}

Finally, C# supports single inheritance only. You cannot define a class that directly inherits from more than one base class.

 
Leave a comment

Posted by on May 9, 2012 in Visual C# . NET

 

Tags: , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: