Sunday, June 15, 2014

Understanding Polymorphism in C#

Understanding Polymorphism in C# is one of main steps to understand OOP , also understanding polymorphism will help you to understand almost of Design patterns.

First What is Polymorphism?

Polymorphism is the use of virtual methods to make one method (name) produce one of many possible outcomes (behaviors) depending upon the instance.
Polymorphism allow ancestor to reach descendants.

Roles to know polymorphism 

1- Non virtual method is based in object type and it done at compile time.
sample to be clear

public class ThumbnailBase
    {
        public virtual void GetMetadata()
        {
            Console.WriteLine("ThumbnailBase.GetMetadata");
        }
        public void GetThumbnail()
        {
            Console.WriteLine("ThumbnailBase.GetThumbnail");
        }
    }

    public class ThumbnailImage : ThumbnailBase
    {
        public override void GetMetadata()
        {
            Console.WriteLine("ThumbnailImage.GetMetadata");
        }
        public new void GetThumbnail()
        {
            Console.WriteLine("ThumbnailImage.GetThumbnail");
        }
    }

static void Main(string[] args)
        {
            ThumbnailBase obj = new ThumbnailImage();
            obj.GetMetadata();  // This is Virtual Method so result is ThumbnailImage.GetMetadata
            obj.GetThumbnail(); // This is Non Virtual Method so result ThumbnailBase.GetThumbnail
            Console.ReadLine();

        }

2- Virtual method is based in instance type and it done at Run time.


sample to be clear

public abstract class ThumbnailBase
    {
        protected abstract void GetMetadata();

        public void GetThumbnail()
        {
            GetMetadata();
            Console.WriteLine("ThumbnailBase.GetThumbnail");
        }

    }

    public class ThumbnailImage : ThumbnailBase
    {
        protected override void GetMetadata()
        {
            Console.WriteLine("ThumbnailImage.GetMetadata");
        }
        public new void GetThumbnail()
        {
            Console.WriteLine("ThumbnailImage.GetThumbnail");
        }
    }

    public class ThumbnailVideo : ThumbnailBase
    {
        protected override void GetMetadata()
        {
            Console.WriteLine("ThumbnailVideo.GetMetadata");
        }
        public new void GetThumbnail()
        {
            Console.WriteLine("ThumbnailVideo.GetThumbnail");
        }

    } 

  static void Main(string[] args)     
   {
            ThumbnailBase obj = new ThumbnailImage();
            ThumbnailBase obj2 = new ThumbnailVideo();
            Console.WriteLine("---------------ThumbnailImage-----------by EslamF----");
            obj.GetThumbnail();
            Console.WriteLine("---------------ThumbnailVideo---------by EslamF------");
            obj2.GetThumbnail();
            Console.ReadLine();

     }









key of polymorphism is depends on important feature , The ability to identify families of object with identical interfaces.

Polymorphism allow you to manipulate objects without change client code.and get behavior change depend on the instance.

Finally,, Hope that i reach my Goal.

Thursday, June 12, 2014

Abstract Function VS Virtual Function

Abstract Function has no Implementation and it can only declared in abstract class although 
Virtual Function can have implementation and can declared in any type of class except sealed class.

When I should use abstract function or virtual function ?
If you need to enforce any derived class to implement this function, you should use abstract function. 
And if you need function to be optional for derived class to implement or not , you should use virtual function. 

Sunday, June 1, 2014

The Power of Decoupling in OOP

First what is Decoupling ??
Encapsulating the behavior that have several values.Decoupling encourage you to make another layer.
For Example
Suppose that u have animal class and another subclass class ( Dog , Bride ) this class inherit from animal as will and you need to add the capability of flying for this subclass , the best solution to do that is using Decoupling as following :-

public class animal {
      public Flys FlyingType  ;
}

public interface Flys{
      string fly();
}

class ItFly : Flys{
    public string fly(){
         return "It Can Fly";
       }
}

class CantFly : Flys{
     public string fly(){
         return "It Can not Fly";
       }
}

Now you can set and change the behavior in the fly.
You can also extend this behavior by add new class (FlySuperFast or FlyWithWings) without effect super-class or any subclass. 
The Negative for using Decoupling is increase number of object and class.
Look following UML for more information