0

I was reviewing some code and stumbled upon this:

export class A extends B implements C {
  ...
}

And then this other class

export abstract class B implements C {
  ...
}

Then I thought, if class A extends B, and B already implements interface C (as shown below), is it necessary for A to implement C too?

I hope I made myself clear and is not too complicated.

Thanks!

5 Answers5

3

You don't need to implement C when B is extended and it implements C. It's implicitly implemented when A extends B.

Though there's nothing wrong with doing it either (it's not going to crash the program)


Also, export is not a valid keyword in Java. The closest I get in terms of usage is related to TypeScript. You're probably looking for public instead

Zoe
  • 27,060
  • 21
  • 118
  • 148
1

If class A extends B and class B implements C, class A implements interface C, and doesn't have to declare implements C explicitly.

However, adding such implements C is a common practice in JDK classes.

For example:

class ArrayList<E> extends AbstractList<E> implements List<E>

may seem redundant, since AbstractList already implements List:

class AbstractList<E> extends AbstractCollection<E> implements List<E>

However, it is useful for the users of the ArrayList class to immediately see that it implements the List interface, without having to look into the AbstractList class (which is an implementation detail).

Eran
  • 387,369
  • 54
  • 702
  • 768
0

if class B implements class C, and class A extends B, does class A need to implement C too?

First of all, a class can never implement a different class. They extend it. A class can only implement interfaces

Now, whatever you implements and/or extends, class or interface became part of same hierarchy.

When class B implementes C interface

abstract class B implements C

Then, you don't need to implement again to class A as you have already extended class B and class B implements C

class A extends B
Ravi
  • 30,829
  • 42
  • 119
  • 173
0

First, @nbokmans is right. export is not a Java keyword.

Then I thought, if class A extends B, and B already implements C (as shown below), is it necessary for A to implement C too?

It is not necessary as the class inheritance is transitive in Java.

Chapter 8. Classes. 8.1.4. Superclasses and Subclasses

The subclass relationship is the transitive closure of the direct subclass relationship. A class A is a subclass of class C if either of the following is true:

  • A is the direct subclass of C

  • There exists a class B such that A is a subclass of B, and B is a subclass of C, applying this definition recursively.

Now make it explicit it :

class A extends B implements C {

makes sense if A makes part of an API that is used by other applications.
It documents explicitly C as an interface that A implements.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

Let's Interface is being defined as following

interface C {
    public void fun();
}

As class B implements interface C, In class B it's mandatory to implement method fun(). But since the class is being defined as the abstract then implementation of method fun() can be skipped as child class can provide the implementation.

abstract class B implements C {


}

Now as class A extends B, so it has already implemented interface C too by general inheritance rule. So it's not required explicitly that A will implement C interface but yes syntactically it's perfectly valid.

class A extends B {

    public void fun() {

    }
}
Ajay Yadav
  • 1,625
  • 4
  • 19
  • 29