Showing posts with label Interfaces. Show all posts
Showing posts with label Interfaces. Show all posts

Saturday, January 28, 2012

.NET interview questions: - Elaborate interfaces in details with examples?

This is the basic and all time favorite .NET interview questions asked in the start or during the interview by the interviewer. So start with the definition & use an example to express the fundamental of the topic in more detail.

In real world interface allows two disparate/ different objects to communicate with each other. For instance a human is a very different object when compared to television object. But they can still use the remote control interface to communicate with each other. Same holds true for interfaces in software, it defines a communication vocabulary between objects. Interfaces have empty methods and we can not create an object of interface.

Figure: - Television interface

The other important thing is that interface brings in lot of uniformity in the project. They define common way of communication. Let’s consider a situation where you have multiple developers working on customer and supplier classes. You want that every developer should define the method name as ‘Update’ when these classes add the data to database. Developers are human’s so some would name it as update and some as add. You can tackle this situation by defining an interface with a method called as update. Now all the classes inherit from the same interface thus making update method constant through out the classes. You can also see how the client code methods are looking consistent in naming convention thus enforcing consistent communication interfaces across all classes.

Figure: - Interface fundamentals

Also see the OOPS fundamental video on polymorphism and its types as follows: -



Visit to get more stuff on Dotnet interview questions and answers

Regards,

Also visit for more author’s other blogs on Most asked Dotnet interview question

Wednesday, September 7, 2011

C# and .NET interview questions: - Differences between abstract class and interface?

Differences between abstract class and Interface is as below:-
Implementation - In Abstract class some methods in abstract classes can have implementation and In Interface all methods, function, properties in interfaces have to empty compulsorily.
Scenario- In Abstract class abstract classes are used when we want to share common functionality in parent child relationship and In Interface - Interfaces are used to define contract, enforce standardization, decoupling and dynamic polymorphism.

Variable declaration-
In Abstract class we can declare variables and In Interface we cannot do that.
Inheritance vs Implementation-Abstract classes are inherited and Interface are implemented.

Watch video on regular expression with practical demonstration as follows: -




Get more materials on important Dotnet interview questions

Regards,

See more stuffs on author’s blog for Most asked Dotnet interview questions

Wednesday, August 24, 2011

.Net Interview Question:- Can we declare Interfaces as private?

Sometimes I am surprised with some questions which come up during .NET interview and this one is one of them. I really do not understand why people ask such questions and how useful it is practically.
Most of the developers jump to the conclusion and say “NO”.

Let me first prove that how the developer are also right by saying that
Interfaces cannot be declared as private and later we will see that how they are
also wrong(incorrect).

Now, let first declare an interface as private and try to compile it and see
that what the compiler says about it.

private interface I1

{



}




In the above diagram you can see that the compiler throws an error saying that Elements defined in a namespace cannot be explicitly declared as private.

Now, let’s prove the second point that interfaces can also be declared as
private.

In the below code snippet you will see that I have declared the interface as
private and try to build the solution, will see that what the compiler has to
say about it.

class MyClass

{

private interface I1

{

}

}


In above diagram you can see that now the compiler has successfully build
solution without throwing the error this means that we can declare Interfaces as
private also but the interfaces declared as private should be declared inside
the class.
Note: - To declare Interfaces as Private you need to declare it inside the class.

Following is the video which shows that how the questions are asked C# and .NET interviews.



Please click here to see more interview questions and answers for .NET

Regards,


Visit Authors blog for more Most asked Dotnet interview questions

Saturday, August 20, 2011

C# and .NET interview question - Can we implement interface with same method name in C#?

This is one of the typical .Net interview questions and is also the favorable question of interviewers.
Let’s first try to understand what exactly the question means.
Suppose we have two interfaces namely I1, I2 and both the interface have a single method with same name like below code snippet.
interface I1

{

void Method1();

}

interface I2

{

void Method1();

}
In above code snippet you can see that we have two interfaces with same method name.
Now, let’s create a class which inherits both interfaces I1, I2 and also do the
implementation like below code snippet.

class A : I1, I2

{

public void Method1()

{

Console.WriteLine("Who has Called me......");

}

}
In above code snippet you can see that I have implemented method only once.
Now let’s see if this compile or not.



You can see that the compile successfully, this means that we have not break any
of the rule.

Now, let invoke both the interfaces method in main class like below code
snippet.

static void Main(string[] args)

{

I1 obj1 = new A();

obj1.Method1();

I2 obj2 = new A();

obj2.Method1();

Console.ReadLine();

}
Now, just run the application and you will see result like below diagram.


The above code works fine but our concern is to have two different for both the
interfaces. So in order to achieve this we have to “ExplicitInterface
method implement.

Now, let’s see how exactly we can achieve this practically.

Step1: - create a new console application for that Go To> New > File >
Project > Windows > Select Console Application.




Step2: - create two interfaces like below code snippet.

interface I1

{

void Method1();

}

interface I2

{

void Method1();

}


Step3: - create a class which inherits both the interfaces and implement both the interfaces methods like below code snippet.
class A : I1,I2

{

void I1.Method1()

{

Console.WriteLine("I have been called by I1.......");

}



void I2.Method1()

{

Console.WriteLine("I have been called by I2.......");

}

}

In above code snippet you can see that I have used “ExplicitInterface” methodimplementation.
void I1.Method1()// here I1.Method1() will indicate the Interface I1.

void I2.Method1()// here I2.Method1() will indicate the Interface I2.

Step4: - now just invoke both the interfaces methods in the main class
like below code snippet.

static void Main(string[] args)

{

I1 obj1 = new A();

obj1.Method1();

I2 obj2 = new A();

obj2.Method1();

Console.ReadLine();

}

Once you have done with all the above steps now just run your application and you will see the result like below diagram.


Now from the above result you can see that both Interfaces methods are having
different implementations.

View the following video how can we implement interfaces with same method names
in C#: -



Also see our tutorials on interview questions and answers for Dotnet

Regards,

View more author’s blog on Most asked Dotnet interview questions


Saturday, July 2, 2011

C# and .NET interview questions: Explain in brief an interface?

Answer:

Interface is a contract that defines the signature of the functionality. So if a class is implementing a interface it says to the outer world, that it provides specific behavior. Example if a class is implementing ‘Idisposable’ interface that means it has a functionality to release unmanaged resources. Now external objects using this class know that it has contract by which it can dispose unused unmanaged objects.

  • Single Class can implement multiple interfaces.

  • If a class implements a interface then it has to provide
    implementation to all its methods.

Following code shows that one has the interface definition and other
class implements the interface. Below is the source code “IInterface” is the
interface and “ClsDosomething” implements the “IInterface”. This sample just
displays a simple message box.

Public Interface IInterFace
Sub Do Something ()
End Interface

Public Class ClsDoSomething
Implements IInterFace
Public Sub DoSomething () Implements WindowsInterFace.IInterFace.DoSomething
MsgBox (“Interface implemented”)
End Sub
End Class

After implementing the above code you will get the following output as shown:



Following you can see a simple video on boxing and unboxing:



Please click here to see more Dotnet and C# interview questions

Regards,

Visit Authors blog for more Most asked Dotnet interview questions

Tuesday, February 15, 2011

.NET Interview questions for Interfaces:- If A class inherits from multiple interfaces and the interfaces have same method names. How can we provide different implementation?.

.NET Interview questions for Interfaces:- If A class inherits from multiple interfaces and the interfaces have same method names. How can we provide different implementation?.


Answer:
This is again one those typical .NET Interview questions which move around interfaces to confuse the developer.

For providing differentimplementation you can qualify the method names with interface names as as shown below. A reference to I1 will display "This is I1" and reference to I2 will display "This is I2". Below is the code snippet for the same.

interface I1
{
void MyMethod();
}
interface I2
{
void MyMethod();
}
class Sample : I1, I2
{
public void I1.MyMethod()
{
Console.WriteLine("This is I1");
}
public void I2.MyMethod()
{
Console.WriteLine("This is I2");
} 
}

50 .NET Interview questions and answers

Wednesday, December 1, 2010

C# and .NET OOP (Object oriented  programming) interview questions - Abstract classes and interfaces.


C# and .NET OOP (Object oriented programming) interview questions
-
Abstract classes and interfaces.


I have yet to remember a .NET interviewer who never asked about abstract classes, interfaces and object oriented interview questions . I am putting forward questions which comes around abstract classes and interfaces again and again...Hope every one benefits.
Normally the C# interviewer starts with.....

What is abstract class ?
Abstract class is a base class or a parent class. Abstract classes can have empty abstract methods or it can have implemented methods which can be overridden by child classes.

The next question i expected
was on interfaces and yes there it comes.


What are interfaces?
Interface is a contract class with empty methods , properties and functions. Any class which implements the interface has to compulsory implement all the empty methods , functions and properties of the interface.
Now the 1000% sure
question was bound to come difference between them...



What's the difference between abstract class and interface?
There are many differences, below are some key two differences :-

  • Abstract class are base class or parent class while interfaces are
    contracts.

  • Abstract class can have some implemented methods and functions while
    interfaces methods and functions are completely empty.

  • Abstract classes are inherited while interfaces are implemented.

  • Abstract classes are used when we want to increase reusability in
    inheritance while interfaces are used to force a contract.

Can we create a object of abstract class or interface?
No we can not.
Now the practical
question..


In what scenarios will you use a abstract class and in what scenarios will
you use a interface?

If you want to increase reusability in inheritance then abstract classes are
good. If you want implement or force some methods across classes must be for
uniformity you can use a interface. So to increase reusability via inheritance
use abstract class as it is nothing but a base class and to force methods use
interfaces.
Really friends having C# experience is one thing and cracking
Dot net and C# interviews is a different ball game all together .