Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many interfaces are allowed to be implemented?

Tags:

In C#:

How many interfaces a class can implement at the same time?

public class MyClass: IInteferface_1, IInterface_2, ... , IInterface_N { } 

Is there a limit for N?

Don't worry I don't want to implement or maintain such an object. I was just wondering if there is a limit.

like image 338
Liviu Mandras Avatar asked Nov 26 '10 13:11

Liviu Mandras


People also ask

How many interfaces can be implemented?

But if you really really want to know the theoretical maximum number of interfaces a class can implement, it's 65535.

How many interfaces should a class implement?

A class can implement more than one interface at a time. A class can extend only one class, but implement many interfaces. An interface can extend another interface, in a similar way as a class can extend another class.

How many interfaces can a class have in Java?

The number of direct superinterfaces of a class or interface is limited to 65535 by the size of the interfaces_count item of the ClassFile structure. That is the only limitation.

Can we implement multiple interfaces in Java?

Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).

How many interfaces can a class implement at once?

When implementation interfaces, there are several rules −. A class can implement more than one interface at a time. A class can extend only one class, but implement many interfaces. An interface can extend another interface, in a similar way as a class can extend another class.

How many types of interfaces are there in Java?

At present, a Java interface can have up to six different types. Interfaces cannot be instantiated, but rather are implemented. A class that implements an interface must implement all of the non-default methods described in the interface, or be an abstract class.

Do all methods of an interface contain an implementation?

All methods of an Interface do not contain implementation (method bodies) as of all versions below Java 8. Starting with Java 8, default and static methods may have implementation in the interface definition. Then, in Java 9, private and private static methods were added.

Can interfaces be instantiated?

Interfaces cannot be instantiated, but rather are implemented. A class that implements an interface must implement all of the non-default methods described in the interface, or be an abstract class.


1 Answers

The C# language imposes no limit on the number of interfaces. There are two practical limits though.

First, as chibacity points out, the compiler will eventually run out of heap or stack space when processing large numbers of interfaces, or extremely deep hierarchies of interfaces.

Even if we fixed those problems, there would still be a second issue. Interface implementation is encoded in metadata in the InterfaceImpl table. Metadata tables typically can have no more than 2^24 members, so the total number of interfaces implemented by all types in an assembly must be less than about 16 million.

Obviously you are going to never run into these limitations in practice. Don't worry about it.

like image 148
Eric Lippert Avatar answered Nov 02 '22 18:11

Eric Lippert