U
U
user_of_toster2021-04-06 13:34:52
Java
user_of_toster, 2021-04-06 13:34:52

How to exclude a property from an interface?

Typescript has a special type Omitthat is used like this:

interface MyInterface {
    A: string;
    B: string;
    C: string;
}

class MyClass implements Omit<MyInterface, 'C'> {
    public A: string = '';
    public B: string = '';
}

How to implement this in Java?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2021-04-06
@user_of_toster

In Java, this is not allowed. Either the class implements the interface completely, or it does not conform to the given interface.
It would be correct to throw an "UnsupportedOperationException" or "NotImplementedException" exception in the implementation of the method, and indicate in the documentation for the method why this method is not implemented.
Another option is to change the interface, in one of the ways and depending on why you need to not implement one of the methods:

  1. Split functionality into two interfaces
  2. Describe the default method in the interface

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question