C# Programming - Interfaces - Discussion
Discussion Forum : 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()
{ }
}
Discussion:
15 comments Page 1 of 2.
Manoj said:
1 decade ago
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()
{ }
}
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()
{ }
}
Rahul Raj said:
10 years ago
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()
{ }
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()
{ }
Ganesh baba said:
1 decade ago
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.
Jasir said:
2 years ago
@All. As per my knowledge;
1) You can't use Access Modifiers while explicit implementation of interfaces in the Derived Class.
2) All methods in interface derived class should have either implicit or explicit implementation of interfaces, you can't have a mixture of them.
1) You can't use Access Modifiers while explicit implementation of interfaces in the Derived Class.
2) All methods in interface derived class should have either implicit or explicit implementation of interfaces, you can't have a mixture of them.
Vinit said:
1 decade ago
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.
Sandeep said:
1 decade ago
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.
Anas said:
1 decade ago
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.
2. It is not necessary to write Interface.Method() in derived class direct method name with public keyword would suffice.
Ajay yadav said:
1 decade ago
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.
Karthik D V said:
9 years ago
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!
Public void fun1 () { } // This will solve the problem.
So, answer E isn't mandatory!
VijayaKumar r said:
1 decade ago
It's correct, because if implicit implementation, first function must mark as public before return type, or go for explicit implementation.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers