1

I'm a self taught hobbyist programmer and all my knowledge is derived from seeing what the compiler does and doesn't like.

Suppose I have (in C# notation, Java may have other abilities)

The class I need to override looks like this:

public interface ICanDoSomethingElse
{
    void DoIt();
}
class Parent : ICanDoSomethingElse
{
   public void EatTacos()
   {
   }

   void ICanDoSomethingElse.DoIt(string thingToDo)
   {
      // Implementation
   }
}

So I do this:

class Child : Parent, ICanDoSomethingElse
{
   new public void EatTacos()
   {
   }
   void ICanDoSomethingElse.DoIt(string thingToDo) // NEW keyword illegal here? 
   {
      // Implementation
   }
}

Question

I observe that the new keyword is illegal in the interface. Is that because this is an explicit Interface?

Is there any way to force children to implement an interface, or does that just mean I need to set up an implicit/explicit cast?

makerofthings7
  • 60,103
  • 53
  • 215
  • 448
  • Questions about language design like this would probably be more appropriate for cs.stackexchange.com. – Barmar Jun 01 '16 at 19:47
  • I think `Child` should inherits from `Parent` – Arturo Menchaca Jun 01 '16 at 19:48
  • There's no parent-child or base-derived relationship on this code at all age out doesn't make sense – Amit Jun 01 '16 at 19:48
  • I don't see any interface. – shmosel Jun 01 '16 at 19:49
  • 3
    Java doesn't have any equivalent for C#'s `new` method modifier. – shmosel Jun 01 '16 at 19:50
  • In a sense Child, already implements the interface via members it has inherited from Parent. What do you want to force the implementer of Child to do? If it is to supply an override, then the obvious thing to do would be to make Parent abstract and make whatever method you want force Child to implement abstract. – Mike Zboray Jun 01 '16 at 20:03

2 Answers2

3

I observe that the new keyword is illegal in the interface. Is that because this is an explicit Interface?

The error is because you're implementing the interface explicitly in the classes.

Explicit implementaton forces the methods to be exposed only when you are working with the interface directly, and not the underlying implementation.

If your classes are like this:

class Parent : ICanDoSomethingElse
{
   ...

   public void DoIt(string thingToDo)
   {
      // Implementation
   }
}

class Child : Parent, ICanDoSomethingElse
{
   ...

   public new void DoIt(string thingToDo)
   {
      // Implementation
   }
}

You will need to use new keyword to somehow hide Parent.DoIt implementation.

Is there any way to force children to implement an interface, or does that just mean I need to set up an implicit/explicit cast?

You can make Parent class abstract, then the interface's methods abstract too:

abstract class Parent : ICanDoSomethingElse
{
   ...

   public abstract void DoIt(string thingToDo);
}

class Child : Parent, ICanDoSomethingElse
{
   ...

   public override void DoIt(string thingToDo)
   {
      // Implementation
   }
}

Here, Child must implement DoIt method.

Arturo Menchaca
  • 15,783
  • 1
  • 29
  • 53
0

I think the interface implementation you're doing has nothing to do with the new keyword.

The new keyword is using when defining a new behavior on a method that is inherits from its parent

public class Parent{
    public void SayHello(){
        Console.WriteLine("Hello from parent");
    }    
}

And the child

public class Child : Parent {
    public new void SayHello(){
        Console.WriteLine("Hello from child");
    }    
}

If you want to use an interface as an implementation, you don't need to use new keyword

jcvegan
  • 3,111
  • 9
  • 43
  • 66