1

Class AbstractAction implements interface Action, but in Action, there's a method actionPerformed(ActionEvent e) which inherits from interface ActionListener
I know that the class implements an interface must supply all the implementations of methods in that interface
But I found that there's no implementation of actionPerformed(ActionEvent e) in AbstractAction, why ?

Bin_Z
  • 899
  • 1
  • 10
  • 15

3 Answers3

6

AbstractAction is an abstract class so it doesn't have to implement all of the methods on the interface. Abstract classes can't be instantiated so they can't be used without creating a subclass of it. Only concrete classes (ie non-abstract) have to provide an implementation of all methods of an interface. If you subclass AbstractAction your subclass will have to implement actionPerformed() or it will have to be abstract too.

Now those are the rules, but it doesn't make sense for AbstractAction to implement actionPerformed() because it couldn't possibly provide a useful implementation. Every subclass would have to override it's definition which makes it a good candidate for being marked abstract.

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
5

I know that the class implements an interface must supply all the implementations of methods in that interface

Not true in case of an abstract class. Only concrete classes are supposed to provide implementations for all the abstract methods in the superclass (which would be abstract) that it extends and all the methods in the interface it implements.

But I found that there's no implementation of actionPerformed(ActionEvent e) in AbstractAction, why ?

They wanted to keep it abstract in that class to make sure that any concrete that extends this class provides an implementation for that method.

Moreover, what default action would it perform (perhaps empty or nothing)?

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
1

An abstract class is allowed to be incomplete. Abstract classes are not required to implement any or all of the methods specified by an interface or parent abstract class. Additionally, an abstract class can declare new abstract methods which any subclass must implement.

The price paid for making a class abstract is that it may not be instantiated directly. That is, an abstract class must be extended to be used.

Tim Bender
  • 20,112
  • 2
  • 49
  • 58