Interviews11 answers
Q: What will be output of following code ? interface IMyInterface { void fun1(); void fun2(); } class MyClass : IMyInterface { void fun1() { } void fun2() { } }
11 Answers
Compile time error. In derived class, implemented methods should be declared as public.
interface IMyInterface
{
void fun1();
void fun2();
}
class MyClass : IMyInterface
{
public void fun1()
{
}
public void fun2()
{
}
}
Manoth KumarPosted Nov 25, 2018, 10:31 PM EST
It will give compile time error be as
MyClass should implement all interface member.
Because all interface function definition should be public in class.
Khaleek AhmadPosted Feb 2, 2017, 3:43 AM EST
above code will give the error please tyr below:-
interface IMyInterface
{
void fun1();
void fun2();
}
class MyClass : IMyInterface
{
public void fun1()
{
}
public void fun2()
{
}
}
manisha vermaPosted Nov 14, 2016, 11:03 AM EST
compile time error because Myclass is not implementing the method of IMyInterface . interface implented method must be public.
Ravi PatelPosted Oct 31, 2016, 10:17 PM EDT
For more information please read this link :
https://msdn.microsoft.com/en-us/library/ms173157.aspx
Joginder BangerPosted Oct 3, 2016, 7:57 AM EDT
Try this type...... public interface IMyInterface {
void fun1();
void fun2();
}
class MyClass : IMyInterface {
void IMyInterface.fun1()
{
}
void IMyInterface.fun2()
{
}
}
Joginder BangerPosted Oct 3, 2016, 7:56 AM EDT
interface IMyInterface
{
void fun1();
void fun2();
}
class Class1 : IMyInterface
{
public void fun1() { } public void fun2() { }
}
keshav kothariPosted Jul 25, 2016, 11:28 PM EDT
MyClass access modifiers functions are private now. So you cont build this one.You will get bellow error message:
"Error 1 'ConsoleApplication1.Program.MyClass' does not implement interface member 'ConsoleApplication1.Program.IMyInterface.fun2()'. 'ConsoleApplication1.Program.MyClass.fun2()' cannot implement an interface member because it is not public. D:\Work\Test\ConsoleApplication1\ConsoleApplication1\Program.cs 18 15 ConsoleApplication1
"
http://asp-net-corner.blogspot.in/
vijay sekaranPosted Mar 23, 2016, 5:00 AM EDT
Hi Arup,
Thanks for reply. As you said all members of interface are by default public that is correct. The members of class, MyClass is private, since access modifiers are not mention here, after implementing interface. So it will not compile. Plz let me know if you have any dought.
Rajesh SinghPosted Feb 7, 2016, 3:47 AM EST
It will not show any error during compile time. please check this code, all members inside interface
are public bydefault
ArupPosted Feb 6, 2016, 7:40 PM EST
Ans: It will not compile because access modifier of fun1() and fun2() are private.
It should be public.
Rajesh SinghPosted Dec 1, 2015, 1:35 AM EST


