Interface is a contract which guarantees client(consumer) how your classes or structs are going to behave.They consists only method skeleton and no implementations.
example-
Interface i1
{
void Method1();
}
2.Whats the naming convention of interface ?
The interface name should start with an I.Example--IDisposable,IEnumerable
3.Whats the use of Interface?
->Interface can be used when you cannot inherit from a class like for structs.
->More than one interface can be inherited thus supporting multiple inheritance in .Net
->Interface helps in defining a common functionality across multiple types.
->Used in plug-n-play architecture.
4.Why Interface methods cannot have access modifiers ?
Interface methods are always public by default.
5.Can Interface have properties in them ?
yes.a property in interface is defined as below
Interface I1
{
int prop
{
get;
set;
}
}
Interface I1
{
int prop
{
get;
set;
}
}
get and set cannot have any body.
6.Can interface have fields ?
No
7.Can you create instance of an interface?
No you cannot create an instance of interface.
No
7.Can you create instance of an interface?
No you cannot create an instance of interface.
8.why is it better to access interface through interface reference only ?
9.Give a real world example of Interface ?
10.Whats the difference between Interface and Abstract Class ?
| |
| |
| |
| |
| |
| |
| |
11.When should we Abstract Class and Interface?
1. If multiple versions of the component are being created and they are reused then use Abstract Class as they simplify versioning.By updating base class all the
derived classes gets updated. Whearas if a new version of interface is required it
has to be created from scratch.
2. If the functionality is used across wide range then use Interface. Abstract Classes should be used for closely related objects.
3. Use interface if there are atleast 2 diff implementations available.
4. If you want to provide common functionality among all the implemented classes go for abstract classes.
12.what is explicit implementation. when is it used?
When a class which has inherited a interface explicitly implements methods of
When a class which has inherited a interface explicitly implements methods of
that class is called explicit implementation.This method can now be accessed
only through interface instance.They are useful in cases when a class inherits
two interfaces and both of them have same method name.
Example --
interface ISimpleCalculator
{
void Add();
}
interface IComplexCalculator
{
void Add();
}
class Calculator: ISimpleCalculator,IComplexCalculator
{
//This is explicit implementation
void ISimpleCalculator.Add()
{
//doSomething
}
void IComplexCalculator.Add()
{only through interface instance.They are useful in cases when a class inherits
two interfaces and both of them have same method name.
Example --
interface ISimpleCalculator
{
void Add();
}
interface IComplexCalculator
{
void Add();
}
class Calculator: ISimpleCalculator,IComplexCalculator
{
//This is explicit implementation
void ISimpleCalculator.Add()
{
//doSomething
}
void IComplexCalculator.Add()
//doSomething
}
}
Accesing these interface--
Calculator obj = new Calculator();
ISimpleCalculator i1 = new Calculator();
i1.Add(); //will call ISimpleCalculator.Add()
You cannot access interface methods from class reference.
obj.Add() //will give compilation error.
13.Why public modifier not used in explicit implementation?
The methods which are explicitly implemented are tied to a interface. and it can be
accessed only through interface reference.
14.What is implicit implementation ?
14.What is implicit implementation ?
interface ISimpleCalculator
{
void Add();
}
class Calculator: ISimpleCalculator
{
//This is implicit implementation
public void Add()
{
//doSomething
}
}
Add() can be access be directly class reference
15.Does structure support Inheriting from an interface ?
Yes
16.When a class A inherits from an interface I1 and another class B what should be taken care of ?
The class being inherited should be the base class
example- This is how it should be inherited
Class A:Class B,I1
{
}
This is wrong
Class A:I1,Class B
{
}
17.Can a method derived from interface marked as virtual ?
{
void Add();
}
class Calculator: ISimpleCalculator
{
//This is implicit implementation
public void Add()
{
//doSomething
}
}
Add() can be access be directly class reference
15.Does structure support Inheriting from an interface ?
Yes
16.When a class A inherits from an interface I1 and another class B what should be taken care of ?
The class being inherited should be the base class
example- This is how it should be inherited
Class A:Class B,I1
{
}
This is wrong
Class A:I1,Class B
{
}
17.Can a method derived from interface marked as virtual ?
Yes and they can then be overridden
18.Why cannot you have static method in Interface?
18.Why cannot you have static method in Interface?
Because Interface methods have implementation and also can be overriden . Thera is no point in having static methods as they should have a body and cannot be overriden.
19.Can Interface inherit another interface ?
Yes
19.Can Interface inherit another interface ?
Yes
20.Can Interface inherit another class ?
Interface cannot inherit another class.
21.Can Interface have access modifier ?
21.Can Interface have access modifier ?
Yes.
22.Mention some Interface in .Net Framework?
IDisposable,IEnumerable,ICollection
Very helpful article. Great points in the differences table
ReplyDeletegudone
ReplyDeleteVery helpful article
ReplyDeleteAllitTechnolgies Interview Question and Answers :
ReplyDeleteDotnet Interview Question and Answers
Can Interface have access modifier ?
ReplyDeleteInterface can have access modifier where interface members cant since they are public by default.
Delete21.Can Interface have access modifier ?
ReplyDeleteI think its answer is NO because interface is by default is public and its member as well.
learnt so many things from this article. Thanks.
ReplyDelete21.Can Interface have access modifier ?
ReplyDeleteThis one is wrong answer. can't access modifier of interface.
Please correct this.
Very Helpful, Thanks for sharing
ReplyDeleteReally helpful.. thanks
ReplyDeleteAn Interface may have access modifies but not it's member.For example.
ReplyDeletepublic interface IInterface
{
void Test();
}
internal interface IInterface
{
void Test();
}
Nice concept Easley to understand.
ReplyDeleteVery helpful
ReplyDeleteVery nice article Thanks for sharing Dot Net Online Course Hyderabad
ReplyDeleteExcellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. AngularJS Online Course
ReplyDeleteThanks for sharing... Very nice article and easy to understand!!!
ReplyDeleteI feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
ReplyDeleteDevops Course Training in Chennai |Best Devops Training Institute in Chennai
Selenium Course Training in Chennai |Best Selenium Training Institute in Chennai
Java Course Training in Chennai | Best Java Training Institute in Chennai
I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.
ReplyDeleteBest PHP Training Institute in Chennai|PHP Course in chennai
Best .Net Training Institute in Chennai
Oracle DBA Training in Chennai
RPA Training in Chennai
UIpath Training in Chennai
This content of the Blog is very Helpful for me.Thanks for this Article
ReplyDeleteJava training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery
This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
ReplyDeleteJava Training in Chennai
Java Training in Velachery
Java Training inTambaram
Java Training in Porur
Java Training in Omr
Java Training in Annanagar
Good job in presenting the correct content with the clear explanation. The content looks real with valid information. Good Work
ReplyDeletePython Training in Chennai
Python Training in Velachery
Python Training in Tambaram
Python Training in Porur
Python Training in Omr
Python Training in Annanagar
Very Helpful, Thanks for sharing
ReplyDeleteDigital Marketing Training in Velachery
Digital Marketing Training in Tambaram
Digital Marketing Training in Porur
Digital Marketing Training in Omr
Digital MarketingTraining in Annanagar
ReplyDeleteRead this also reated post
Constructor in c# with real time example
Important Point About Constructor
Syntax of a Constructor
Advantages of constructor in c#
Types of constructor in c#
Static Constructors
Disadvantages of constructor in c#
Great blog the content is informative and engaging. Visit my website to get best Information
ReplyDeleteFrozen Shoulder Treatment in Surrey
Wonderful blog with great piece of information. Visit my website to get best Information
ReplyDeletevintage oval mirror