DEV Community

Calin Baenen
Calin Baenen

Posted on

Can you implement abstract classes, like you can interfaces?

Like, if I had:

abstract class Test {
    abstract void test();
}
Enter fullscreen mode Exit fullscreen mode

could I do

class Test2 implements Test {/*...*/}
Enter fullscreen mode Exit fullscreen mode

?

Top comments (14)

Collapse
 
phlash profile image
Phil Ashby

Almost: you extend abstract classes, and provide the abstract methods, to take your example, you would:

class Test2 extends Test {
    void test() { /* do that lovely stuff */ }
}
Enter fullscreen mode Exit fullscreen mode

this means you can only extend one abstract class of course, as Java is a single-inheritance language.

Collapse
 
baenencalin profile image
Calin Baenen

Can I subclass (extend) without inheriting any of the methods?
Or can I override the return type of the only given method?

Collapse
 
phlash profile image
Phil Ashby

By definition, if you sub-class a type, your sub-class inherits all the base type methods and fields. You can add method overrides with different parameter types or quantities, but you cannot simply change a return type. I would recommend adding a new method with a well-chosen name rather than trying to fudge something in with the same name as an existing method, again taking your example:

class Test2 extends Test {
    void test() { /* implements abstract method, providing expected (hopefully documented!) behaviour */ }
    void test(int arg) { /* override for test() method that is parametrised in some way, similar behaviour.. */ }
    int getSomeMagicField() { /* implements new behaviour, which returns some useful information */ }
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
baenencalin profile image
Calin Baenen

Okay. Thanks.

I really wish there was a way to do so (for the "daring").
I have something I wanted to be considered a java.lang.Runnable (because it has similar functionality), the only difference is that run returns an Object, and run is a default method (in my future library, this is called a CodeShell).

Thread Thread
 
alainvanhout profile image
Alain Van Hout

For a 'Runnable that returns an object' have a look at the Supplier class. For a runnable that takes an object as part of its method call, there's Consumer, and if you want both combined there's Function.

Thread Thread
 
baenencalin profile image
Calin Baenen

Thanks!

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
baenencalin profile image
Calin Baenen

What is Dart? Another JVM language?

Collapse
 
thi3rry profile image
Thierry Poinot • Edited

An abstract class is a class that you can't instantiate, but another class can "extends" it.

You must extends class and abstract class and you must implements interface.

So you must write :

class Test2 extends Test {/.../}

Collapse
 
hugueschabot profile image
Hugues Chabot

In a subclass, you can override the return type of a method: stackoverflow.com/a/14694885

Collapse
 
baenencalin profile image
Calin Baenen

Yes, but it must be a subclass of the original return type, and I seen this.
But, I don't think void has any subclasses, and I don't think you can subclass void (even though void IS a class (void.class proves)).

Collapse
 
hugueschabot profile image
Hugues Chabot

Exactly, nothing extends void. I am not sure of what you want to do but could java.util.function.Supplier be a better suited interface than Runnable?

Thread Thread
 
baenencalin profile image
Calin Baenen

No, because Runnable implies it's a piece of code that's run (one that's not seeking a result), much to the likes of Thread (which implements Runnable).
If Runnable was useless, it wouldn't have been made.

I want my class to be considered part of Runnable because it matches the format with similar (if not (essentially) the same) methods, and because a lot of things use Runnable in place of functions in Java.

Thread Thread
 
alainvanhout profile image
Alain Van Hout

Note that a runnable doesn't 'return' void. What the 'void' return type means is that it doesn't return something.

Things like Supplier, Consumer and Function are used as much as Runnable.