Unheilig
Unheilig

Reputation: 16302

Overriding inner class' method in Interface

I have an interface in which I declare an inner class:

public interface myInterface 
{
    int count();    
    public class myInterfaceInnerClass
    {
        public void testOverride()
        {

        }
    }
}

And class Test implements myInterface:

public class Test implements myInterface
{

    public Test() 
    {
        // TODO Auto-generated constructor stub
    }

    public int count()
    {
        return 1;
    }

    //Here I would like to be able to override the inner class' method testOverride
}

Is it possible to override the inner class' method testOverride in class Test?


EDIT:

In response to comment made by O. Charlesworth, I came up with the following:

public interface myInterface 
{
    myInterface aInterface = new myInterfaceInnerClass();
    int count();

    public class myInterfaceInnerClass implements myInterface
    {
        @Override
        public int count() 
        {
            // TODO Auto-generated method stub
            return 0;
        }
        public void testOverride()
        {

        }
    }
}

In class Test:

public class Test implements myInterface
{
    public Test() 
    {
        // TODO Auto-generated constructor stub
    }

    public int count()
    {
        return 1;
    }
}

Is it still possible to override the inner class' method testOverride here in class Test?

Upvotes: 0

Views: 718

Answers (1)

Unihedron
Unihedron

Reputation: 11051

Here's something you can do:

import myInterface.myInterfaceInnerClass;

public class Test extends myInterfaceInnerClass implements myInterface {

    @Override public int count() {
      // ...
      return 0;
    }

    @Override
    public void testOverride() {
      // ...
    }

}

Reason for extending class myInterfaceInnerClass (A picture > a million words):

alt text
(source: gyazo.com)

Upvotes: 1

Related Questions