Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple interfaces in single file

Tags:

java

interface

I was just wondering if you can limit the number of interface files on a project. Couple of projects i work with have dozen of interfaces with nothing special in them, and i was thinking can you write all interfaces in single file ? and in specific class point to interface needed ?? i.e:

//interface file
interface InterfaceOne {

}
interface InterfaceTwo{

}
//foo file
public class foo implements InterfaceTwo{
   public void foo {
         //....
   }
}
//foo1 file
public class foo1 implements InterfaceOne{
   public void foo {
        //....
    }
}

or something similar ?

like image 483
Maciej Cygan Avatar asked Sep 06 '13 22:09

Maciej Cygan


People also ask

Can we have multiple interfaces in single form?

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.

Can a Java file have multiple interfaces?

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).

Can an interface have multiple interfaces?

An interface can extend other interfaces, just as a class subclass or extend another class. However, whereas a class can extend only one other class, an interface can extend any number of interfaces.

Can a type fulfill multiple interfaces?

A type can implement multiple interfaces.


1 Answers

Yes, you can fill a file with interfaces, but those interfaces cannot be public. Thus, they can only be accessible from the package they are defined in. If that's OK with you, you can collect your interfaces into a single file.

Note that this might make your interface definitions harder to find.

like image 101
nneonneo Avatar answered Sep 20 '22 18:09

nneonneo