interface a
{
Method1()
Method2()
}
class b: a
{
override Method1()
override Method2()
}

what will happen & why?

Answers were Sorted based on User's Feedback



interface a { Method1() Method2() } class b: a { override Method1() override Metho..

Answer / vijayakumar

It will throw an error bcz

1.override Key word not use with interface

correct ans :


class b : a
{
public void Method1()
{

}
public void Method2()
{

}
}

Is This Answer Correct ?    2 Yes 1 No

interface a { Method1() Method2() } class b: a { override Method1() override Metho..

Answer / arun

A compile time error will be thrown as there exist no
methods to override. The methods in the interface are just a
contract.
To override any method, it should be defined as virtual.

Is This Answer Correct ?    2 Yes 1 No

interface a { Method1() Method2() } class b: a { override Method1() override Metho..

Answer / rajeev kumar

1.Method1() and Method2() are not having the ";" for termination .So it is a compile time error.

2.Method must have a return type.So it is a compile time error.

3.Method1() and Method2() must declare a body because it is not marked abstract, extern, or partial .
So it is a compile time error.
It should be like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
interface a
{
void Method1();
void Method2();
}

class b: a
{

public void Method1()
{
Console.WriteLine("From Method1()");
}
public void Method2()
{
Console.WriteLine("From Method2()");
}
}
class Program
{
static void Main(string[] args)
{
b obj=new b();
obj.Method1();
obj.Method2();

}
}
}

Output:
From Method1()
From Method2()

Is This Answer Correct ?    2 Yes 1 No

interface a { Method1() Method2() } class b: a { override Method1() override Metho..

Answer / nithya

It will through the error because
in interface,the methods should have return type for them.
And we can create only implementation for the methods
available in interface. We cannt override it.

And Virtual/Abstarct or not a valid items in interface.

We must implement the methods available in interface.

interface a
{
void Method1();
void Method2();
}
class b: a
{
public void Method1();
public void Method2();
}

Is This Answer Correct ?    1 Yes 1 No

interface a { Method1() Method2() } class b: a { override Method1() override Metho..

Answer / sandeep joshi

without virtual keyword
cant override method
its error at compile time .....

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Sharp Interview Questions

Suppose two interfaces have same method, so how will you implement these methods in derive class?

0 Answers  


Is overriding of a function possible in the same class?

0 Answers  


What is the difference between struct and class in c#?

0 Answers  


What is the difference between delegates and superdelegates?

0 Answers  


What is native image generator (ngen.exe)?

0 Answers  






What are functions c#?

0 Answers  


How will you allow a class to be inherited, but prevent the method from being over-ridden?

0 Answers   Siebel,


what is application domain?

4 Answers   Kanbay,


Why is c# a good programming language?

0 Answers  


How Reflection is used and what it's significance ?

0 Answers   HCL,


Can we overload the main method in c#?

0 Answers  


What is an icollection in c#?

0 Answers  


Categories