Click here to Skip to main content
14,879,224 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Following is the code Snippet:

C#
public interface ITest
{
    void print();
}
public abstract class Testing
{
    public abstract void print();
}
public class Usage: Testing, ITest
{
}



Can we Implement an Interface and Abstract class together. If Yes, then whose print() gets called. Can you please give explanation also.
Posted
Updated 15-Oct-10 9:03am
v2

First, that won't even compile. It has to look like this:

C#
public interface ITest
{      
    void print();
}

public abstract class Testing : ITest
{      
    public abstract void print();
}

public class Usage: Testing
{
    public override void print() {}
}


Second, only the print method in the derived class will be hit. The methods in ITest and Testing simply require the derived class to have that method. Essentially you're being redundant about the requirement for the print method.

=========
You stated that "the question is correct because it was asked in an interview".

It's NOT correct. It simply won't compile. If you had taken the time to actually put it in a test app and try it, you would have found that out. Further, my answer is correct.

A method in an abstract class cannot be "hit" because it has no body. A method in an interface can also not be hit because it has no public accessor. The only place the print method will be hit is in the derived class.
   
v4
No. In the given code example, ITest.print and Test.print can have the same implementation in Usage. It is perfectly legal to define Usage.print as follows:

C#
public class Usage: Testing, ITest {

    public override void print() {
     Console.WriteLine("Usage.Print");
    }

}


The key is to add the override modifier to the definition of Usage.print. Otherwise, Usage.print would (a) hide the implementation of Test.print and (b) Usage could not be instantiated because it would be lacking the definition of the abstract declaration from Test.print.

All of the following invocations default to Usage.Print:

C#
static void CallPrint(Usage pUsageInstance) {
    pUsageInstance.print();
}

static void CallPrint(Test pTestInstance) {
    pTestInstance.print();
}

static void CallPrint(ITest pITestInstance) {
    pITestInstance.print();
}


C#
Usage tUsage = new Usage();

CallPrint(tUsage);
CallPrint((Test)tUsage);
CallPrint((ITest)tUsage);
   
v4
There is another option apart from what paul_71 and John Simmons wrote.

You can do the following:

C#
public class Usage: Testing, ITest {
  public override void print() {
    // Implementation of the Testing.print() method
  }

  void ITest.print() {
    // Implementation of the ITest.print() method
  }
}


Now whenever you call the Usage.print() method, the method inherited from Testing will be called. However, if you access your object as an ITest instance (you cast it to ITest) and then call the print() method, the second method will be called.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900