locked
Can anyone share the real time example with explanation for Abstract class and Interface? RRS feed

  • Question

  • Real time example for Abstract class ?



    • Edited by m_p_k Monday, September 17, 2012 1:14 PM
    Friday, September 7, 2012 11:26 AM

All replies

  • Shape class with draw method

    Shape will be inherited by Circle, Rectangle classes to implement the Draw Method


    www.tnvbalaji.com

    Monday, September 17, 2012 6:19 PM
  • Hi 

    Actually this is vary old school days debate but in today`s worlds it is of no use , latest design principles says that "Program to Interface" , Let me explain more 

      •        Abstract Class
        1.        Use  abstract class when the entity it represent doesn’t  exist in real world , for example Shape itself has no meaning , It is pure conceptual so you should go for abstract implementation
      •        Interface
        1.        It is more restricted form of abstract class and always recommended
      •        Interface Vs Abstract class
        1.        Use interface when you do not have anything to share in base class
        2.       Use Abstract class of there are some function that will be share across all derived object
        3.        Always prefer Interface
      •        Best design Approach
        1.        Best Design approach is to use mix of both i.e. Interface >>Abstract>>Concrete , See the share Example below that is very simplified version.

    Hope it help

     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Practices.EnterpriseLibrary.Logging;
    
    namespace LoggerSample
    {
        interface IShape
        {
            void Render();
        }
    
        abstract class Shape : IShape
        {
    
            public virtual void Render()
            {
                Console.WriteLine("Default Drawing, Empty Box"); 
            }
        }
    
        class Default : Shape
        {
            // No implementation for IShape.Render()
        }
        class Circle : Shape
        {
            public override void Render()
            {
                Console.WriteLine("Drawing Circle"); 
            }
        }
        class Rectangle : Shape
        {
            public override void Render()
            {
                Console.WriteLine("Drawing Rectangle"); 
            }
        }
        class Program
        {
            static List<IShape> MyShapes = new List<IShape>();
            static void Main(string[] args)
            {
                MyShapes.Add((IShape)new Default());
                MyShapes.Add((IShape) new Circle());
                MyShapes.Add((IShape) new Rectangle());
                MyShapes.Add((IShape)new Rectangle());
                MyShapes.Add((IShape)new Rectangle());
                MyShapes.Add((IShape)new Circle());
    
    
                RenderMyShapes(MyShapes);
    
                Console.ReadLine();
            }
    
            public static void RenderMyShapes(List<IShape> MyShapes)
            {
                foreach (IShape shape in MyShapes)
                {
                    shape.Render();
                }
            }
        }
    }
    

    Friday, September 21, 2012 1:51 AM