C# Programming - Interfaces - Discussion
Discussion Forum : Interfaces - General Questions (Q.No. 13)
13.
Which of the following statements is correct about the C#.NET code snippet given below?
interface IMyInterface
{
void fun1();
void fun2();
}
class MyClass: IMyInterface
{
private int i;
void IMyInterface.fun1()
{
// Some code
}
}
Discussion:
7 comments Page 1 of 1.
Sonya said:
9 years ago
The right answer is : E.
The compiler will report an error since the interface IMyInterface is only partially implemented.
Checked by code.
The compiler will report an error since the interface IMyInterface is only partially implemented.
Checked by code.
Vishwanath Heddoori said:
9 years ago
Application does not implement interface member 'Application.IMyInterface.fun2()'. The correct answer is E.
Andrew said:
1 decade ago
@Vani if class is abstract you can do this:
interface IMyInterface
{
void fun1();
int fun2();
}
abstract class MyClass : IMyInterface
{
public abstract void fun1();
public abstract int fun2();
}
or implement one of this:
abstract class MyClass : IMyInterface
{
void IMyInterface.fun1()
{Console.WriteLine("Hello world"); }
public abstract int fun2();
}
interface IMyInterface
{
void fun1();
int fun2();
}
abstract class MyClass : IMyInterface
{
public abstract void fun1();
public abstract int fun2();
}
or implement one of this:
abstract class MyClass : IMyInterface
{
void IMyInterface.fun1()
{Console.WriteLine("Hello world"); }
public abstract int fun2();
}
Sumit said:
1 decade ago
[E] must be the correct option. As its not an abstract class. It has to implement all the methods of the interface. Here, it will report an error.
Vani said:
1 decade ago
Even if implementing class is abstract we still get same error that all the methods must be implemented. Please check below code and explain?
interface IMyInterface
{
void fun1();
int fun2();
}
abstract class MyClass : IMyInterface
{
void IMyInterface.fun1()
{ }
//int IMyInterface.fun2()
//{ return 1; }
}
interface IMyInterface
{
void fun1();
int fun2();
}
abstract class MyClass : IMyInterface
{
void IMyInterface.fun1()
{ }
//int IMyInterface.fun2()
//{ return 1; }
}
Ravi Srivastava said:
1 decade ago
Interface types, not being classes, are not derived from object. They are all convertible to object, to be sure, because we know that at runtime the instance will be a concrete type. But interfaces only derive from other interface types, and object is not an interface type.
for more follow below link: http://blogs.msdn.com/b/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx
In above code, the Implementing class is not a abstract class, so answer is E.
for more follow below link: http://blogs.msdn.com/b/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx
In above code, the Implementing class is not a abstract class, so answer is E.
Mallikarjuna Dongre said:
1 decade ago
OOPs concept says that the class which is inheriting Interface must implement all the members in that Interface.
If that class doesn't want to implement all methods of Interface then it should declare itself as abstract class.
In above code, the Implementing class is not a abstract class, so answer is E
If that class doesn't want to implement all methods of Interface then it should declare itself as abstract class.
In above code, the Implementing class is not a abstract class, so answer is E
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers