Emailid
Password
         
  
    Forgot password

New user Sign Up
 

Polymorphism, Abstraction and Inheritance - a short description

       Current Rating:  0%                                                     Total Members Rated:  0
                                                                     Send To Friend

  

Polymorphism, Abstraction and Inheritance - a short description

 

 

Introduction

 

Basic programming perceptions like inheritance and polymorphism are taught using indistinct examples, like class "AA" and class "BB". They're too intangible to offer a proper clarification. And here this article is trying to give a short notion about polymorphism, abstraction and inheritance. 

 

Suppose you have a program to replicate a workshop, and you want to generate a class for each possible tool like hammers, power saws, screwdrivers, etc. Pretty much every tool class you create is going to have common methods, like Use () and Put Away (), in spite of of what it is. Inheritance lets you to enforce this common ground between tools, and reap some very useful benefits in the process.

 

See the example below:

 

public abstract class Tool()

 

{

 

private double _price;

 

public double GetPrice()

 

{

 

return _price;

 

}

 

public abstract void Use();

 

public abstract void PutAway();

 

 

}

 

It provides a common interface for all of the other tools you create but itdoesn't do much of anything on its own.

 

We have already written code for Get Price (), any other Tool class we write can take advantage of it right away. Every class that inherits from Tool will automatically have a _price, a method called "GetPrice()", and two other methods called Use() and PutAway().

There is a slight difference between Use() and PutAway() . Because we've declared Use() and PutAway() as abstract methods, any class we write that inherits from Tool [i]must[/i] have its own Use() and PutAway() methods. Then why trouble putting them in the base Tool class in the first place, if you have to rewrite them in all the inherited classes, anyway"? There is sufficient clarification for it because these abstract methods [i]guarantee [/i] to our program that any tool it ever deals with will have these methods and it is hundred percentages sure. Here is a quick look at two classes that inherit from Tool. And it is explained below:

 

public class ScrewDriver: Tool

 

{

 

public override void Use()

 

{

 

// Insert into screw head and rotate

 

}

 

public override void PutAway()

 

{

 

// Find correct drawer in toolbench and place inside

 

}

 

}

 

 

public class PowerSaw: Tool

 

{

 

public override void Use()

 

{

 

// Flip "on" switch and apply blade to wood surface

 

}

 

public override void PutAway()

 

{

 

// Flip "off" switch and place on shelf

 

}

 

public void ReplaceBatteries()

 

{

 

// Remove old batteries, and insert new ones

 

}

 

}

 

The ones who succeed to the Use() and PutAway() methods from Tool are ScrewDriver and PowerSaw. Also they can implement their own unique methods, as well. Eg: PowerSaw has a ReplaceBatteries() method, which would be pointless on a screwdriver, but important on a power tool. It is a critical position: that's why we didn't put a ReplaceBatteries() method in the tool class... because not [i]every[/i] tool needs a ReplaceBatteries() method. Instead, we placed it in the class that would actually need it. Use() and PutAway() are needed by every tool, and so those methods are in the base division.

 

Although you can't see , both ScrewDriver and PowerSaw also have _price and GetPrice(), because they inherited it from "Tool". For example, the following code is, in theory, completely official:

 

ScrewDriver myScrewDriver;

 

myScrewDriver.GetPrice();

 

The Use() and PutAway() are actually written in ScrewDriver and PowerSaw. It guarantees that you won't forget to add those methods to your inherited classes. Also you can do it in this way:

 

Tool[] myArrayOfTools = new Tool[2];

 

tool[0] = new ScrewDriver();

 

tool[1] = new PowerSaw();

 

foreach( Tool myTool in myArrayOfTools )

 

{

 

myTool.Use();

 

myTool.PutAway();

 

}

 

Although element 0 and element 1 in that array are totally different types that will be perfectly lawful.  One is a ScrewDriver, and one is a PowerSaw, but because they are both tools, we can call their Use() and PutAway() methods regardless of their specific type.

 

Some points to be remembered:

 

1. The capacity to use all inherited tools as a normal tool ie, using a ScrewDriver as a Tool is called "polymorphism".

 

2. When we want to write code for Use() in the base class, but still allow it to be overridden by an inheritence class, use "virtual" instead of "abstract". That is:

public virtual void Use()

 

{

 

// Generic instructions for using a tool

 

}

 

Although all inherited classes still have access to virtual methods, they are not required to dominate them but go with the rule.

 

3. Also there are some really neat things called "Interfaces", which you may want to look into once you're comfortable with bequest.

 

4. What is meant by the "abstract" in "public abstract class Tool()" is that we can't create instances of our Tool class... which is good, because the actual base Tool class doesn't do anything. We don't want people creating copies of it by accident, they should stick to ScrewDrivers and PowerSaws that can actually do the things.

 

5. You can create two additional base classes, PowerTool and HandTool that inherit from Tool, and then have your specific tools inherit from either PowerTool or HandTool. PowerTool would have specific methods that all power tools need, and HandTool would have specific methods that all hand tools need. Think of inheritance as a family tree, where in this case, "Tool" is the trunk, "PowerTool" and "HandTool" are both branches, and "ScrewDriver" and "PowerSaw" are stems.

 

Happy programming!


                           Rate This Article:   

Author is Offline
  Author: Raphaelin Mantein
       


Comments Posted
Label
Subject Author Status Date

 

Post Comment

Related Articles
Fundamentals on .NET Framework
N-Tier in ASP.NET or other .NET apps
Software Development Outsourcing (Offshore ) to India
Make your web site ‘perfect and Search Engine Friendly for Google and Yahoo
Color combination of your Web site



Home | About Us | Site Map | Privacy Policy | Submit Links