C# Programming - Interfaces - Discussion

Discussion :: Interfaces - General Questions (Q.No.1)

1. 

Which of the following statements is correct about the C#.NET code snippet given below?

interface IMyInterface
{ 
    void fun1(); 
    int fun2();
}
class MyClass: IMyInterface
{ 
    void fun1()
    { } 
    int IMyInterface.fun2()
    { } 
}

[A]. A function cannot be declared inside an interface.
[B]. A subroutine cannot be declared inside an interface.
[C]. A Method Table will not be created for class MyClass.
[D]. MyClass is an abstract class.
[E]. The definition of fun1() in class MyClass should be void IMyInterface.fun1().

Answer: Option E

Explanation:

No answer description available for this question.

Mahdi said: (Aug 17, 2011)  
The answer is wrong because
1.fun2 should return an int value
2. fun1 and fun2 in Myclass should be public

Vijayakumar R said: (Jan 11, 2012)  
It's correct, because if implicit implementation, first function must mark as public before return type, or go for explicit implementation.

Ajay Yadav said: (Sep 10, 2012)  
In this program how could we directly call the method from base class with out creating the obj of the class ! it only be possible while be use static keyword.

Pramod Khandare said: (Oct 7, 2012)  
No need to give Interface name while giving definition to method void fun1().

Sandeep said: (Jul 15, 2013)  
fun1() must be public in my class and fun2() must return a value of int type and there is no need to make int IMyInterface.fun2() public because it is explicit implementation which is private to class.

Sudhakar said: (Sep 21, 2013)  
void IMyInterface.fun1()
int IMyInterface.fun2()

(or)

public void fun1()
public int fun2()


As per multiple choice answer should be 'E'.

Vinit said: (Sep 25, 2013)  
Answer is correct. Because in interface all the signature are public by default and we can directly use them in derived class. But before declaring them in derived class we must tell that called method is of which interface like it is done in 2nd fun2() method.

Ganesh Baba said: (Oct 16, 2013)  
Yes its a right answer, we need to prefix interface name to a method name only when the same method is declared in another interface. In order to trace out in which interface the method is going to implement. You will understand better in multiple inheritance concept. It sounds complex in vocabulary but read it twice you'll get.

Manoj said: (Nov 12, 2013)  
Missing access specifier in implementation of fun1()

It should be:

interface IMyInterface
{
void fun1();
int fun2();
}
class MyClass: IMyInterface
{
public void fun1()
{ }
int IMyInterface.fun2()
{ }
}

or

interface IMyInterface
{
void fun1();
int fun2();
}
class MyClass: IMyInterface
{
void IMyInterface.fun1()
{ }
int IMyInterface.fun2()
{ }
}

Anas said: (Jul 2, 2014)  
1. The Interface Methods when called in Derived class should be Public.

2. It is not necessary to write Interface.Method() in derived class direct method name with public keyword would suffice.

Rahul Raj said: (Dec 20, 2015)  
Correct Answer "E".

Because Fun1() in MyClass is Void Fun1() and should be Fun2 also Void IMyInterface.fun2().

And no need to write public because public is by default.

Output:

interface IMyInterface
{
void fun1();
void fun2();
}
class MyClass : IMyInterface
{
void IMyInterface.fun1()
{ }
void IMyInterface.fun2()
{ }

Karthik D V said: (Jun 20, 2016)  
Explicit implementation of interface may not be needed in this case.

Public void fun1 () { } // This will solve the problem.

So, answer E isn't mandatory!

Omkar said: (Jan 9, 2018)  
void fun1() must be public.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.