Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

do interfaces belong in files of their own

As as rule of thumb I generally put classes in a file of their own. Visual studio seems to encourage this but what is appropriate with regards to interfaces?

e.g.

I have Class Foo that implements interface Bar

public interface IBar
{

}

public class Foo : IBar   
{

}

it seems natural to group these within the same file until another class implements the interface but to dedicate a file to 2 lines code seems excessive but correct.

What is appropriate?

like image 754
Johnno Nolan Avatar asked Jun 26 '09 11:06

Johnno Nolan


People also ask

Do interfaces have to be in their own file?

In terms of encapsulation, each object, whether a class or an interface, should be in its own file. Even if the interface only contains one abstract method, the fact that it's in a different file allows for better organization and better encapsulation.

Should interfaces be in a separate file?

Don't do it. You put IServiceAnnouncer in a separate file, and import the class name in HelloWorld. java. You merely compile them at the same time, passing both file names to javac.

Are interfaces .Java files?

An interface is written in a file with a . java extension, with the name of the interface matching the name of the file. The byte code of an interface appears in a .

Are interfaces inherited?

Interfaces can inherit from one or more interfaces. The derived interface inherits the members from its base interfaces. A class that implements a derived interface must implement all members in the derived interface, including all members of the derived interface's base interfaces.


1 Answers

I would split them into 2 files. I often found classes starting to go unmanageable when they are not in their own files.

Imagine trying to find class Foo in a file named IBar.cs or vice versa.

like image 143
Adrian Godong Avatar answered Oct 11 '22 12:10

Adrian Godong