0% found this document useful (0 votes)
33 views

Polymorphism

Polymorphism is a key concept in object-oriented programming that allows objects of different types to be treated as objects of a general "parent" type. It involves method overriding and overloading. Method overriding replaces the implementation of a method in a parent class, while overloading provides different implementations of a method based on parameters. An example shows a Customer class with two derived classes, CorporateCustomer and PersonalCustomer, that each override the CustomerType method to return the specific customer type.

Uploaded by

Owais Khan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Polymorphism

Polymorphism is a key concept in object-oriented programming that allows objects of different types to be treated as objects of a general "parent" type. It involves method overriding and overloading. Method overriding replaces the implementation of a method in a parent class, while overloading provides different implementations of a method based on parameters. An example shows a Customer class with two derived classes, CorporateCustomer and PersonalCustomer, that each override the CustomerType method to return the specific customer type.

Uploaded by

Owais Khan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Polymorphism:-

The word polymorphism comes from Greek and means having several different
forms. This is one of the essential concepts of object-oriented programming.
Where inheritance is related to classes and (their hierarchy), polymorphism is
related to object methods.

Before discussing more about polymorphism I wanted to tell about


overloading, overriding and their differences

Method overriding is a feature in Object Oriented programming language. This is


used to implement a method for subclass which overrides in other words
replaces the implementation of the super class. Overloading the concept of
providing different meaning to a object based on the context of its presence.
Overloading is one type of polymorphism and this is also a feature in Object
Oriented programming language.

Overriding is the concept of having functions of same name and signature in


different classes. One in the super class can be made virtual and other can
override the functionality of virtual one
.

Overloading is the concept of having functions of same name, but different


signature in same class. They are differentiated by the compiler by their
signatures.

There is big difference between both:


see following example

When overriding, you change the method behavior for a derived class.
e.g Clas owais
{
Virtual void hi(int a)
{
}
}

Class khan:owais
{
public overrid void hi(int a)
{

}
}
Overloading simply involves having a method with the same name within the
class.

Example for Over loading

Class A
{
class a()

}
class a(int a)
{
}
}

Example 1:

Using polymorphism in the example of customer

using System;
public class Customer
{
public virtual void CustomerType()
{
Console.WriteLine("I am a customer");
}
}
public class CorporateCustomer : Customer
{
public override void CustomerType()
{
Console.WriteLine("I am a corporate customer");
}
}
public class PersonalCustomer : Customer
{
public override void CustomerType()
{
Console.WriteLine("I am a personal customer");
}
}

You might also like