2. polymorphism
The word polymorphism means having many forms.
In object-oriented programming paradigm, polymorphism is often expressed as 'one interface, multiple functions'.
Polymorphism can be static or dynamic.
In static polymorphism, the response to a function is determined at the compile time.
In dynamic polymorphism, it is decided at run-time.
Static Polymorphism:
The mechanism of linking a function with an object during compile time is called early binding. It is
also called static binding. C# provides two techniques to implement static polymorphism. They are:
Function overloading
Operator overloading
3. Function Overloading
You can have multiple definitions for the same function name in the same scope.
The definition of the function must differ from each other by the types and/or the number of arguments in the
argument list.
You cannot overload function declarations that differ only by return type.
Example
using System;
namespace PolymorphismApplication {
class Printdata {
void print(int i) {
Console.WriteLine("Printing int: {0}", i );
}
void print(double f) {
Console.WriteLine("Printing float: {0}" , f);
}
void print(string s) {
Console.WriteLine("Printing string: {0}", s);
}
static void Main(string[] args)
{ Printdata p = new Printdata(); // Call print to print integer
p.print(5); // Call print to print float
p.print(500.263); // Call print to print string
p.print("Hello C++");
Console.ReadKey();
}
}
}
4. Dynamic Polymorphism
C# allows you to create abstract classes that are used to provide partial class implementation of an interface.
Implementation is completed when a derived class inherits from it.
Abstract classes contain abstract methods, which are implemented by the derived class.
The derived classes have more specialized functionality.
Here are the rules about abstract classes:
You cannot create an instance of an abstract class
You cannot declare an abstract method outside an abstract class
When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed.
5. The following program demonstrates an abstract class
Example
using System;
namespace PolymorphismApplication
{
abstract class Shape {
public abstract int area();
}
class Rectangle: Shape
{
private int length;
private int width;
public Rectangle( int a = 0, int b = 0)
{
length = a; width = b;
}
public override int area ()
{
Console.WriteLine("Rectangle class area :");
return (width * length);
}
}
class RectangleTester {
static void Main(string[] args)
{ Rectangle r = new Rectangle(10, 7);
double a = r.area();
Console.WriteLine("Area: {0}",a);
Console.ReadKey();
}
}
}
6. Example
Example explained
The output from the example above was probably
not what you expected. That is because the base
class method overrides the derived class method,
when they share the same name.
class Animal // Base class (parent)
{
public void animalSound()
{
Console.WriteLine("The animal makes a sound");
}
}
class Pig : Animal // Derived class (child) {
public void animalSound()
{
Console.WriteLine("The pig says: wee wee");
}
}
class Dog : Animal // Derived class (child) {
public void animalSound() {
Console.WriteLine("The dog says: bow wow");
}
}
class Program {
static void Main(string[] args)
{
Animal myAnimal = new Animal(); // Create a Animal object
Animal myPig = new Pig(); // Create a Pig object
Animal myDog = new Dog(); // Create a Dog object
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound(); } }
The animal makes a sound
The animal makes a sound
The animal makes a sound
output
7. Example 2
Example explained
However, C# provides an option to override the
base class method, by adding the virtual keyword
to the method inside the base class, and by using
the override keyword for each derived class
methods:
The animal makes a sound
The pig says: wee wee
The dog says: bow wow
output
class Animal // Base class (parent)
{
public virtual void animalSound()
{
Console.WriteLine("The animal makes a sound");
} }
class Pig : Animal // Derived class (child)
{
public override void animalSound() {
Console.WriteLine("The pig says: wee wee");
} }
class Dog : Animal // Derived class (child) {
public override void animalSound() {
Console.WriteLine("The dog says: bow wow"); } }
class Program {
static void Main(string[] args) {
Animal myAnimal = new Animal(); // Create a Animal
object
Animal myPig = new Pig(); // Create a Pig object
Animal myDog = new Dog(); // Create a Dog object
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
} }
8. Abstract Classes and Methods
Data abstraction is the process of hiding certain details and showing only essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces
The abstract keyword is used for classes and methods:
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from
another class).
Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by
the derived class (inherited from).
An abstract class can have both abstract and regular methods:
abstract class Animal
{
public abstract void animalSound();
public void sleep()
{
Console.WriteLine("Zzz");
} }
Example From the example above, it is not possible to
create an object of the Animal class:
Animal myObj = new Animal(); // Will generate an error (Cannot create an instance of the abstract class
9. Abstract Classes and Methods
To access the abstract class, it must be inherited from another class. Let's convert the Animal class we used in
the Polymorphism chapter to an abstract class.
Remember from the Inheritance chapter that we use the : symbol to inherit from a class, and that we use the
override keyword to override the base class method.
10. Example
Example
// Abstract class
abstract class Animal
{ // Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep()
{
Console.WriteLine("Zzz");
}
}
// Derived class (inherit from Animal)
class Pig : Animal
{
public override void animalSound()
{
// The body of animalSound() is provided here
Console.WriteLine("The pig says: wee wee");
}
}
class Program
{
static void Main(string[] args)
{
Pig myPig = new Pig();// Create a Pig object
myPig.animalSound();// Call the abstract method
myPig.sleep(); // Call the regular method
} }
Why And When To Use Abstract Classes
and Methods?
To achieve security - hide certain details
and only show the important details of
an object.