If I declare that my object implements an interface, but fail to implement those methods, when I compile my code, I get a compiler error. How does the java compiler know that I haven't implemented all of the methods of an interface?
-
2It has access to both the interface and your class. Why would it not know? – resueman Jul 27 '16 at 14:37
-
This question is similiar to asking: "How does the compiler know that a string literal cannot be assigned to an int variable?" – isnot2bad Jul 27 '16 at 14:40
-
@resueman I'm looking for a deeper explanation. For example, since an interface is an abstract type, how is the type checking done, and how is an interface represented by the compiler, to do these checks. – kurryt Jul 27 '16 at 14:44
-
1The deeper explanation is in [JLS Sec 8.1.5](https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.1.5): "Unless the class being declared is abstract, all the abstract member methods of each direct superinterface must be implemented (§8.4.8.1) either by a declaration in this class or by an existing method declaration inherited from the direct superclass or a direct superinterface, because a class that is not abstract is not permitted to have abstract methods (§8.1.1.1)." The compiler checks the class declaration against the language spec. – Andy Turner Jul 27 '16 at 14:45
-
@isnot2bad Yes, the question is related to type checking, but its not that similar to that quesiton. – kurryt Jul 27 '16 at 14:46
-
@AndyTurner thanks, that's it. So essentially the compiler knows that the Object must implement those methods to conform to its abstract type, and then just looks for each method declaration. – kurryt Jul 27 '16 at 14:53
1 Answers
How does the java compiler know that I haven't implemented all of the methods of an interface?
It knows all methods that your class has implemented because it has found and analyzed them during the compilation.
It knows all of the methods that were defined in all of the superclasses and interfaces of your class because:
it has either just compiled their source code, or loaded their ".class" files, and
it has analysed the interfaces / classes and figured out which methods need to be implemented by your class.
Then it compares the two sets of methods. (Note that the methods don't need to be exactly the same. For example, the actual method could return a subtype of the return type of the method in the interface. The set comparison needs to take account of that.)
(Actually, that is just one approach to doing this check. An actual Java compiler might do the checking a bit differently.)

- 698,415
- 94
- 811
- 1,216
-
This is a good answer, to finish this out... In Java an interface is an abstract type, which is a set of operations with well-defined and mutally consistent semantics. The Java compiler has a type checker, which makes sure that the program obeys the type-compatibility rules of the language. So the type checker, would just check if the class has a method signature, with the appropriate return type, and then return an error if it does not. Source: http://www.cs.columbia.edu/~aho/cs4115/Lectures/15-03-23.html – kurryt Jul 27 '16 at 15:18