extend a class to conform to an interface?

William Jockusch

I have an interface:

interface IFoo {
  int foo(int bar);
}

Can I now extend an existing class to conform to the interface? Say class String. I know I can define the foo() method on strings. But is it possible to go further and tell the compiler that strings can be cast to IFoo?

Sergey Kalinichenko

You can do it with other classes, but not with System.String, because it is sealed.

If you wanted to do this to a non-sealed class, you could simply derive from it, add appropriate constructors, and put the interface as something that your new class implements.

interface IFoo {
    int Size {get;}
}
// This class already does what you need, but does not implement
// your interface of interest
class OldClass {
    int Size {get;private set;}
    public OldClass(int size) { Size = size; }
}
// Derive from the existing class, and implement the interface
class NewClass : OldClass, IFoo {
    public NewCLass(int size) : base(size) {}
}

When the class is sealed, your only solution of presenting it as some interface through composition: write a wrapper class implementing your interface, give it an instance of the sealed class, and write method implementations that "forward" calls to the wrapped instance of the target class.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Java: conform static class (not instance) to an interface

From Java

Class does not conform NSObjectProtocol

From Dev

If Annotation is an interface then how it can extend an Object class?

From Dev

extend a class to conform to an interface?

From Dev

Is it possible to extend the EventTarget interface?

From Dev

Member variable which must extend class A and implement some interface

From Dev

Why insist all implementations of an interface extend a base class?

From Dev

Interface/class/implement/extend confusion

From Dev

Can I have a class field that is the Class<?> of a type that must extend another class and an interface?

From Dev

Class does not conform to 'CBPeripheralManagerDelegate' in Swift

From Dev

MCSessionDelegate Requires Class to Conform to NSObjectProtocol

From Dev

typescript extend an interface as not required

From Dev

Extend class that implements Interface shadows name

From Dev

Why can't I extend an interface "generic method" and narrow its type to my inherited interface "class generic"?

From Dev

Class does not conform to protocol

From Dev

TypeScript: Extend Express.Session interface with own class

From Dev

Does a class which implements an interface's method (without explicitly implementing that interface) extend that specific interface?

From Dev

How do I correctly restrict a generic interface to extend Number class in Java and be able to run it in another class?

From Dev

Is it possible to extend the EventTarget interface?

From Dev

Member variable which must extend class A and implement some interface

From Dev

Partial interface implementation when you cannot extend a abstract class in java

From Dev

Correctly extend interface with generics

From Dev

Extend class that implements Interface shadows name

From Dev

Extend interface with common logic

From Dev

How to extend the Serializable interface to all sub Class?

From Dev

Class does not conform to protocol

From Dev

Extend native interface for element

From Dev

How to extend class from library when there's an interface involved?

From Dev

How do you make a declarative_base-derived class conform to an interface?

Related Related

  1. 1

    Java: conform static class (not instance) to an interface

  2. 2

    Class does not conform NSObjectProtocol

  3. 3

    If Annotation is an interface then how it can extend an Object class?

  4. 4

    extend a class to conform to an interface?

  5. 5

    Is it possible to extend the EventTarget interface?

  6. 6

    Member variable which must extend class A and implement some interface

  7. 7

    Why insist all implementations of an interface extend a base class?

  8. 8

    Interface/class/implement/extend confusion

  9. 9

    Can I have a class field that is the Class<?> of a type that must extend another class and an interface?

  10. 10

    Class does not conform to 'CBPeripheralManagerDelegate' in Swift

  11. 11

    MCSessionDelegate Requires Class to Conform to NSObjectProtocol

  12. 12

    typescript extend an interface as not required

  13. 13

    Extend class that implements Interface shadows name

  14. 14

    Why can't I extend an interface "generic method" and narrow its type to my inherited interface "class generic"?

  15. 15

    Class does not conform to protocol

  16. 16

    TypeScript: Extend Express.Session interface with own class

  17. 17

    Does a class which implements an interface's method (without explicitly implementing that interface) extend that specific interface?

  18. 18

    How do I correctly restrict a generic interface to extend Number class in Java and be able to run it in another class?

  19. 19

    Is it possible to extend the EventTarget interface?

  20. 20

    Member variable which must extend class A and implement some interface

  21. 21

    Partial interface implementation when you cannot extend a abstract class in java

  22. 22

    Correctly extend interface with generics

  23. 23

    Extend class that implements Interface shadows name

  24. 24

    Extend interface with common logic

  25. 25

    How to extend the Serializable interface to all sub Class?

  26. 26

    Class does not conform to protocol

  27. 27

    Extend native interface for element

  28. 28

    How to extend class from library when there's an interface involved?

  29. 29

    How do you make a declarative_base-derived class conform to an interface?

HotTag

Archive