Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
Hope im not wasting your valueble time,i have 4.4 years of experience in .net,
im employee of Hp, right now im looking for jobchange,today i was attend the interview
in bangalore,the interviewer asked only one question that is what is interface,
i was not said like used for multiple inheritance, and that guy also not expected that answer.
i told some thing.

that only one question , later he said ok.

techies, kindly tell me why we go for interface . give me a sample.

i never implemented it.

if my question was very basic ,kindly ignore.
Posted
Comments
Sergey Alexandrovich Kryukov 25-Jun-12 1:18am    
I must note that multiple inheritance with interfaces is a really important aspect of this feature; such kind of multiple inheritance is called "weak form of multiple inheritance". The fact you never implemented one makes your 4.4 years of experience -- quite considerable time otherwise -- questionable. You are advised to overview the language and technology you use even before you actually use it, otherwise your use could be very ineffective.
--SA

1 solution

An interface is a contract: when you add an Interface to your class you are agreeing to abide by the rules that the interface lays down, and in return it allow your class to join the exclusive membership club, and get the benefits of that membership, no matter what they might be.

For example, all arrays, and Generic Collection objects (such as List<T>) implement IEnumerable, and as a result they are all usable in a foreach loop - if your class implements the IEnumerable Interface, then it can be used in foreach as well.

I use them a reasonable amount. Recently, I needed a panel which contained controls which it auto sized, depending on which were Expanded and which were Shrunken. So I created a FlexPanel to hold them, and an IFlexPanel interface which the controls had to implement:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UtilityControls
    {
    /// <summary>
    /// Interface to add support for FlexControls within a FlexPanel
    /// </summary>
    /// <remarks>
    /// When you implement this interface:
    ///
    ///    DefaultShrunken
    /// Indicates the desired initial state of your control. You may or may not get
    /// your wish, depending on the AutoSize mode of the FlexPanel, and the DefaultShrunken
    /// status of other controls, combined with your controls position in the list
    /// of FlexControls
    ///
    ///    Shrinking
    ///    Expanding
    /// Are called when the appropriate actions are being taken - this allows you to
    /// update buttons, or remove toolbars, etc. to reflect the visual state in your
    /// control.
    ///
    ///    RequestShrink
    ///    RequestExpand
    ///    RequestToggleSize
    /// Are Events that the FlexPanel handles to change the size of your control
    /// Note that your panel may change size without you signalling any of these
    /// if the FlexPanel is set to AutoSize mode and a different panel is Expanded
    /// or Shrunk.
    /// </remarks>
    public interface IFlexControl
        {
        /// <summary>
        /// Called when the the control is to be shrunk to mininum height
        /// </summary>
        void Shrinking();
        /// <summary>
        /// Called when the control is to be enlarged
        /// </summary>
        void Expanding();
        /// <summary>
        /// User request to shrink this control
        /// </summary>
        /// <remarks>
        /// The parent FlexPanel will handle the actual decision to shrink.
        /// </remarks>
        event EventHandler RequestShrink;
        /// <summary>
        /// User request to enlarge this control
        /// </summary>
        /// <remarks>
        /// The parent FlexPanel will handle the actual decision to enlarge.
        /// </remarks>
        event EventHandler RequestExpand;
        /// <summary>
        /// User request to change the Shrunk / Enlarged state of this control
        /// </summary>
        /// <remarks>
        /// The parent FlexPanel will handle the actual decision to enlarge or shrink.
        /// </remarks>
        event EventHandler RequestToggleSize;
        /// <summary>
        /// If true, will default to shruken when first shown.
        /// </summary>
        bool DefaultShrunken { get; set; }
        }
    }
Provided the control implemented the two methods "Shrinking" and "Expanding", the three events "RequestShrink", "RequestExpand" and "RequestToggleSize" and the property "DefaultShrunken" they could be based on any Control or UserControl and they would be autosized as necessary.

Using an interface here means that I don't have to restrict myself later to having to use controls based on a Button, or a DataGridView, or even a UserControl - anything which implements IFlexControl will be fine. Remember, C# only allows you to derive your class from a single class - so if I constructed IFlexControl as a normal or abstract class then only controls derived from that class coule be loaded into a FlexPanel.


[edit]Code block changed from "xml" to "c#" - I didn't notice before I posted - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Linto Leo Tom 2-Jun-12 11:33am    
Good narration.
Linto Leo Tom 16-May-14 17:03pm    
+50
Manas Bhardwaj 2-Jun-12 11:41am    
Very well said. +5!
jameschowdaree 2-Jun-12 12:54pm    
Got it.
Sergey Alexandrovich Kryukov 25-Jun-12 1:18am    
Sure, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900