Rajib Rezwan

Hi ! I'm Rezwanul Islam Rajib . I'm a passionate programmer and software devloper . Everything about technology , the beauty in each and every one of them , makes me passionate about them . I wish I could see may be a little bit of all of them .

OOP , Part-3

Polymorphism:

Polymorphism means many forms (ability to take more than one form). In Polymorphism poly means “multiple” and morph means “forms” so polymorphism means many forms.

In polymorphism we will declare methods with same name and different parameters in same class or methods with same name and same parameters in different classes. Polymorphism has ability to provide different implementation of methods that are implemented with same name.

In Polymorphism we have 2 different types those are

        -   Compile Time Polymorphism (Called as Early Binding or Overloading or static binding)

        -   Run Time Polymorphism (Called as Late Binding or Overriding or dynamic binding)

Compile Time Polymorphism

Compile time polymorphism means we will declare methods with same name but different signatures because of this we will perform different tasks with same method name. This compile time polymorphism also called as early binding or method overloading.

Method Overloading or compile time polymorphism means same method names with different signatures (different parameters)

Example


public class Class1
{
public void NumbersAdd(int a, int b)
{
Console.WriteLine(a + b);
}
public void NumbersAdd(int a, int b, int c)
{
Console.WriteLine(a + b + c);
}
}
In above class we have two methods with same name but having different input parameters this is called method overloading or compile time polymorphism or early binding. 

Run Time Polymorphism

Run time polymorphism also called as late binding or method overriding or dynamic polymorphism. Run time polymorphism or method overriding means same method names with same signatures.

In this run time polymorphism or method overriding we can override a method in base class by creating similar function in derived class this can be achieved by using inheritance principle and using “virtual &override” keywords.

In base class if we declare methods with virtual keyword then only we can override those methods in derived class using override keyword

Example


//Base Class
public class Bclass
{
public virtual void Sample1()
{
Console.WriteLine("Base Class");
}
}
// Derived Class
public class DClass : Bclass
{
public override void Sample1()
{
Console.WriteLine("Derived Class");
}
}
// Using base and derived class
class Program
{
static void Main(string[] args)
{
// calling the overriden method
DClass objDc = new DClass();
objDc.Sample1();
// calling the base class method
Bclass objBc = new DClass();
objBc.Sample1();
}
}
If we run above code we will get output like as shown below

Output

----------------------------------
Derived Class
Derived Class

In this way we can implement polymorphism concept in our applications.


Abstraction:
In object-oriented software, complexity is managed by using abstraction. Abstraction is a process that involves identifying the critical behavior of an object and eliminating irrelevant and complex denials. Abstraction is a process of identifying the relevant qualities and behaviors an object should possess.

Example- A Laptop consists of many things such as processor, motherboard, RAM, keyboard, LCD screen, wireless antenna, web camera, USB ports, battery, speakers etc. To use it, you don't need to know how internally LCD screens, keyboard, web camera, battery, wireless antenna, speaker’s works.  You just need to know how to operate the laptop by switching it on.

Note- When derived class inherited with abstract class; derived class must be override abstract class methods.

Example of Abstraction: - 

using System;

using System.Collections.Generic;

using System.Linq;

namespace abstarction

{

    public abstract class university

    {

        public abstract void BTech();

        public abstract void MBA();

    }

    public class GBTU : university

    {

        public override void BTech()

        {

            Console.WriteLine("GBTU BTech Fee 50000/-");

        }

        public override void MBA()

        {

            Console.WriteLine("GBTU MBA Fee 100000/-");

        }

    }

    public class MTU : university

    {

        public override void BTech()

        {

            Console.WriteLine("MTU BTech Fee 40000/-");

        }

      public override void MBA()

        {

            Console.WriteLine("MTU MBA Fee 800000/-");

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            GBTU g = new GBTU();

            g.BTech();

            g.MBA();

            MTU m = new MTU();

            m.BTech();

            m.MBA();

            Console.ReadLine();

        }

    }

}

Output-

GBTU BTech Fee 50000/-

GBTU MBA Fee 100000/-

MTU BTech Fee 40000/-

MTU MBA Fee 800000/-

Explanation :-

Ø  From above example we have make one abstract class university and two abstract methods Btech and MBAGBTU and MTUboth are override university course fee.

Ø  University course common for both GBTU and MTU so university method BTech and MBA is abstract.

Ø  GBTU and MTU inherited abstract method so university method must be override here.







Loading