1

I want to create my custom activity class which extends Activity,

but adds another abstracts method which inheriters must implement.

Example:

interface MyActivity extends Activity
{
    public void myAbstractMethod();
}

The IDE says :"The type Activity cannot be a superinterface of MyActivity; a superinterface must be an interface"

How can I do such thing?

Thank you.

3
  • 1
    Interfaces can only extend from other interfaces (not classes). Commented Apr 1, 2013 at 17:23
  • So there's no way I can do such thing? Commented Apr 1, 2013 at 17:24
  • @TaruStolovich, you could create an abstract class which extends Activity -- it's similar in function -- but since Activity contains implementation, you can't have an interface (which contains no implementation) extend it. Commented Apr 1, 2013 at 17:26

4 Answers 4

6

It sounds like you need MyActivity to be an abstract class, not an interface. Interfaces don't inherit from classes; they can only inherit from other interfaces.

Try

abstract class MyActivity extends Activity
{
    public abstract void myAbstractMethod();
}

An abstract class with all its methods abstract behaves like an interface, so this sounds like what you need.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! Worked perfectly!
2

You can use an abstract class instead of an interface.

Read more at http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

e.g.

abstract public class AbstractActivity extends Activity {
    public abstract void myAbstractMethod();
}

However, this does not allow you to implement both an AbstractActivity and another superclass, but it doesn't sound like you need this.

Comments

1

An interface only can extends other interface, never a concrete class.

Comments

0

It looks like interfaces CAN extend some classes. For example,

java.util.concurrent.ExecutorService:

public **interface** ExecutorService **extends** Executor { ...


java.util.concurrent.Executor:

public **class** Executors { ...

but java.util.concurrent.Executorhas static methods only.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.