Ravisha
Ravisha

Reputation: 3343

Why classes compile to .class but interface does not to .interface

Is there any specific reason why interfaces are not compiled into MyInterface.java compiled into .interface file?But any class is compiled into .class file.!

Upvotes: 11

Views: 12921

Answers (6)

b.roth
b.roth

Reputation: 9542

Because the point is to indicate that the file is Java byte code (and .class was the extension chosen for that), not the specific language construction.

Upvotes: 16

fastcodejava
fastcodejava

Reputation: 41097

It is just a choice they made. I wouldn't bother about it. It is a binary file anyway. One way to think is "Even it is an interface it is still in a file.java".

Upvotes: 1

Eli Acherkan
Eli Acherkan

Reputation: 6411

That's the way the language designers decided.

It makes sense in several ways:

  • .class files are a byproduct that you don't normally see or manipulate by hand.
  • The less different extensions a program uses, the easier it is to maintain.
  • In many cases, there's no distinction in the code between a class and an interface, so it's logical that the binary files look alike.

Frankly, I can't think of a good reason to have different extensions for compiled classes and interfaces. Why would it be important to distinguish between them?

Upvotes: 4

Bozho
Bozho

Reputation: 597114

The physical representation of the byte-code on the file system doesn't matter.

It's the logical realization (whether class or interface) that matters.

Upvotes: 6

Tim Jansen
Tim Jansen

Reputation: 3368

Java treats interfaces almost like classes, eg they share the same namespace (you can't have an interface that has the same name as a class) and a compiled interface is almost identical to a compiled abstract class.

So it would not make any sense to store them in a different format or with a different file extension. On the contrary, this would make many things harder. For example, when you load a class or interface by name (Class.forName("my.class.name")) Java does not know whether it is a class or an interface. If there would be two different extensions, Java would have try to find a file "my/class/name.class" and then "my/class/name.interface", instead of only trying the first one.

Upvotes: 8

Thirler
Thirler

Reputation: 20760

In java, you have source files, called .java and binaries called .class. Its just a choice of naming.

Also for java classes and interface's don't differ that much (a class just contains a lot of extra information like method bodies).

Upvotes: 3

Related Questions