0

i am confused using interface. i don't know where it is used. here are two programs. tell me, why interface use here when i can implement method directly.

First Program Without Inteface :

namespace InterFaceDemo 
{    
    class ODDEVEN     
        public void ONE()      
        {            
            Console.WriteLine("This is ONE");        
        }        
        public void TWO()        
        {           
            Console.WriteLine("This is TWO");       
        }        
        public void THREE()        
        {           
            Console.WriteLine("This is THERE");        
        }        public void FOUR()        
        {            
            Console.WriteLine("This is FOUR");       
        }        public void FIVE()       
        {            
            Console.WriteLine("This is FIVE");        
        } 
    } }

namespace InterFaceDemo 
{    
    class Program   
    {       
        static void Main(string[] args)      
        {            
            Console.WriteLine("This is ODD");
            ODDEVEN obj1 = new ODDEVEN();            
            obj1.ONE();            
            obj1.THREE();            
            obj1.FIVE();  
            Console.WriteLine("\n\nThis is EVEN");
            ODDEVEN obj2 = new ODDEVEN();            
            obj2.TWO();            
            obj2.FOUR();  
            Console.ReadLine();        
        }    
    } 
}

Second Program Interface

namespace InterFaceDemo 
{    
    interface IOne    
    {       
        void ONE();//Pure Abstract Method Signature    
    }    interface ITwo    
    {        
        void TWO();    
    }    interface IThree:IOne    
    {        
        void THREE();    
    }    
    interface IFour    
    {        
        void FOUR();    
    }   
    interface IFive:IThree    
    {        
        void FIVE();    
    }    
    interface IEVEN:ITwo,IFour    
    { 
    }   
    class ODDEVEN:IEVEN,IFive
    {
        //Must Implement all the abstract method, in Derived class.    

        public void ONE()//Implementation of Abstract Method.        
        {            
            Console.WriteLine("This is ONE");        
        }        
        public void TWO()        
        {           
            Console.WriteLine("This is TWO");       
        }        
        public void THREE()        
        {           
            Console.WriteLine("This is THERE");        
        }        public void FOUR()        
        {            
            Console.WriteLine("This is FOUR");       
        }        public void FIVE()       
        {            
            Console.WriteLine("This is FIVE");        
        } 
    } }

namespace InterFaceDemo 
{    
    class Program   
    {       
        static void Main(string[] args)      
        {            
            Console.WriteLine("This is ODD");
                        IFive obj1 = new ODDEVEN();            
            obj1.ONE();            
            obj1.THREE();            
            obj1.FIVE();  
            Console.WriteLine("\n\nThis is EVEN");            
            IEVEN obj2 = new ODDEVEN();            
            obj2.TWO();            
            obj2.FOUR();  
            Console.ReadLine();        
        }    
    } 
}

Output same for both programs.

  • Do you think your example is verbose enough? Wouldn't a single interface have been enough to make your point? – spender Jul 28 '16 at 10:08
  • 1
    There is a good answer on [programers.stackechange.com](http://programmers.stackexchange.com/questions/108240/why-are-interfaces-useful) for same question – VidasV Jul 28 '16 at 10:09

0 Answers0