-2

in .Net you can simply do this with the following code

Public class MyClass:ISomeInterface, SomeBaseClass

Can something similar be done in Java?

The code I have is as follows

public class InputHander extends OnTouchListener implements IControlHandler

However I get the error "no interface expected here" with regards to OnTouchListener

coolblue2000
  • 3,796
  • 10
  • 41
  • 62

3 Answers3

7

Yes, use the extends keyword for class inheritance and implements for interface implementation:

public class MyClass extends SomeBaseClass implements SomeInterface

From your question edit, you should use extends in class declaration for class inheritance. In other words, a class can only extend from another single class, not from interfaces. Multiple class inheritance is not allowed. In case you want to implement an interface, use implements keyword only. So, your code would be:

public class InputHander implements OnTouchListener, IControlHandler

More info:

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
3

Yes. It takes couple of seconds to try it out:

public class MyClass extends MyAbstractClass implements MyInterface
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
2

In order to prevent the pitfalls and complexities that arise if you have a language that supports multiple inheritance (e.g. C++), Java allows you to inherit from exactly one class (using extends and that class may or may not be abstract) and any number of interfaces (using implements).

So this is valid:

public class MyClass 
    extends SomeBaseClass 
    implements SomeInterface1, SomeInterface2

Java 8 will allow you to have default methods in interfaces but it's implemented carefully enough so as to avoid multiple inheritance ambiguities.

See Interface with default methods vs Abstract class in Java 8

Community
  • 1
  • 1
Bathsheba
  • 231,907
  • 34
  • 361
  • 483