Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Saturday, March 11, 2017

Interface in OOPS


Interface is mainly made for function declaration.It specifies what must be done, but not how.
In an interface, no method can include a body. That is, an interface provides no implementation whatsoever.
Interface is the place where we can see what is concern but we do not know how will it be done. If we make a planning of what to do is Interface.
If there is no Interface,there is no planning,unstructured work,unplanned work.
You can define architecture,process,planning by Interface.

Interface is a keyword to declare Interface.
Within Interface,I supposed to declare name of method.It is by default abstract.
In addition to methods, interfaces can specify properties, indexers, and events.
Interfaces cannot have data members. 
They cannot define constructors, destructors, or operator methods. Also, no member can be declared as static.

To implement an interface member, the corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member.

Example- Reserve Bank of India is an Interface.It provides rule.All bank follow that rule.
If any class implements Interface,he is restricted to define all the function of Interface in its own class.
There are several reason,I want to implement Interface.
a)Interface and classes,There is in between contract relation.

Bank --> Customer. If customer want to have account in bank,he has to follow all contract of Bank.
so Bank is an Interface and customer is a class .

b)Interface provides business rule,work plan and layout.

C)By Interface,we also achieve polymorphism.

Example - 
Step 1- We have Interface IMobile
    public interface IMobile
    {
        void SendSMS();
        void Call();
    }

Step 2- Class Nokia implements Interface IMobile.Class Nokia must have to define method SendSMS and Call.
class Nokia : IMobile
    {
        public void SendSMS()
        {
            Console.WriteLine("Send sms");
        }

        public void Call()
        {
            Console.WriteLine("Calling");
        }
        public void Camera()
        {
            Console.WriteLine("Camera");
        }
    }

Step 3-
class Program
    {
        static void Main(string[] args)
        {
            Nokia nokia = new Nokia();
            nokia.SendSMS();
            nokia.Call();
            nokia.Camera();
            Console.ReadLine();
        }
    }

Step 4 Output




















Interfaces Can Be Inherited One interface can inherit another. The syntax is the same as for inheriting classes. When a class implements an interface that inherits another interface, it must provide implementations for all the members defined within the interface inheritance chain. Here is an example: Step 1-  
    public interface IA 
        {  
            void MethodA1(); 
            void MethodA2();
        }
Step 2-
        public interface IB : IA
        { 
            void MethodB1();
        }
Step 3- Class 'MyClass' is implementing Interface IB. So 'MyClass' must have to define functions of interface IB and Interface IA as IB is inherited IA.
        class MyClass : IB
        { 
            public void MethodA1() 
             {    
                Console.WriteLine("Implement MethodA1()."); 
             }  
            public void MethodA2() 
             {   
                Console.WriteLine("Implement MethodA2().");
             }  
            public void MethodB1() 
             {   
                Console.WriteLine("Implement MethodB1().");
             }
         }
    Step 4-
 class Program
    {
        static void Main(string[] args)
        {
            MyClass ob = new MyClass();
            ob.MethodA1();
            ob.MethodA2();
            ob.MethodB1();
            Console.ReadLine();
        }

    }
Step 5-Output


3 comments :

  1. My partner and I stumbled over here different web page and thought I might as well check things out. I like what I see so now i'm following you. Look forward to looking over your web page for a second time. capital one login

    ReplyDelete
  2. Hi! I'm at work surfing around your blog from my new iphone 3gs! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the superb work! capitalone com login

    ReplyDelete

  3. Since the admin of this website is working, no question very rapidly it will be renowned, due to its quality contents. facebook sign in

    ReplyDelete