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 ?
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.
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).
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.
A type can implement multiple interfaces.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With