-5

I got a compiler error on HPrinter class saying to include unimplemented methods or make my class abstract, what is obviously correct, but what I don't understand is why does java allows to create an object of that class, I know it is not explicitly abstract but it is implicitly. I know I don't call the unimplemented method, if I do it so I would got an error, but in first place it shouldn't let you create an object of HPrinter class, doesn't it? Here is an example:

public interface Printer {
  void print();
  void scan();
}

public class HPrinter implements Printer {  
  public void print() {
    System.out.println("print()");
  }
}

public class TestInterface {
  public static void main(String[] args) {
    Printer p=new HPrinter();
    p.print();
  }
}
  • 3
    allow creating but gives error ... sorry, hard to understand You – Jacek Cz Sep 04 '18 at 18:09
  • No, Java does not allow this. Either implement all (required) methods, or mark your class as abstract. Did you actually try to compile this code? (If you can actually run the code, I am pretty sure that you have a compiled version from before you added the scan method.) – folkol Sep 04 '18 at 18:12
  • 4
    If you got a compiler error, ¿how is Java allowing you to do this? – Pablo Sep 04 '18 at 18:13
  • You should implement the scan() method that is the rule for any implementing class. You can make object of a class HPrinter, it is not a violation. – PJAutomator Sep 04 '18 at 18:13
  • It is not implicity abstract, you need the keyword on the class. You HAVE to implement all interface methods and/or all abstract methods on the first concrete class. If HPrinter was abstract then the following would be possible: ```new HPrinter() { public void scan() {}}.print();``` – Marcos Vasconcelos Sep 04 '18 at 18:15
  • 1
    If you are using Eclipse, have a look at https://stackoverflow.com/questions/10546718/how-eclipse-execute-java-code-when-there-are-compile-errors and/or https://stackoverflow.com/questions/16394152/how-can-eclipse-create-a-class-with-unresolved-compilation-problems/16394204#16394204 – Holt Sep 04 '18 at 18:17
  • With java 8 , if you are using default or static keywords in method signature , you can give the implementation in interface itself, which then not necessarily requires concrete classes to provide implementations. That is for default that's the default behaviour, this is still overridable. static methods are not. For your code though, it would seem this should give you compile time errors. – MithunS Sep 04 '18 at 18:26
  • I guess the compiler complains of unimplemented methods (Contract compliance for Java classes) but it does not avoid the virtual machine to load the program and since the virtual machine does not check whether some class implements all methods of some interface (as remark by @briarheart) it runs without problem. – Candy E. Sansores Sep 04 '18 at 19:03
  • Thank you @Holt, that it's the case, then the problem is that Eclipse lets me run the code that didn't properly compile, and since the java virtual machine does not check whether some class implements all methods of some interface the problem is concealed. – Candy E. Sansores Sep 04 '18 at 19:33

2 Answers2

2

A Java interface may have any of the following methods...

  • Abstract methods
  • Default methods
  • Static methods

(PS: Valid only for version 8 and above of java language.)

If a class implements an interface it must provide implementation for all the abstract methods of the interface it is implementing.

The default method does not need to be implemented it is inherited automatically by the class implementing the interface, however it can be overridden

The static method is not inherited by the class implementing the interface it can only be called by using the name of the interface

However it is important to note that if an abstract class implements an interface it does not need to provide implementation for all its abstract methods, furthermore an abstract class cannot be instantiated.

MithunS
  • 485
  • 7
  • 28
0

Contract compliance for Java classes is checked statically by the compiler. Virtual machine is responsible for bytecode loading, verifying and running. It does not check whether some class implements all methods of some interface.

briarheart
  • 1,906
  • 2
  • 19
  • 33
  • I hope you are not confusing runtime polymorphism with simple inheritances. Contract compliance would mean that all concrete classes will need to provide implementations for the abstract class or interfaces they are implementing or they are marking themselves as abstract. Now with those keywords `{default, static}`you can add method bodies, but thats not what OP is doing or asking about. – MithunS Sep 04 '18 at 18:41
  • Thank you for your answer. I understand for what you are saying is that the compiler is notifying me I have an unimplemented method, my IDE shows me that error on the HPrinter class, but this doesn't avoid the virtual machine to load that class and since the virtual machine doesn't check if it implements all interface methods I can run a program like this. – Candy E. Sansores Sep 04 '18 at 18:46
  • No, I'm not confusing polimorfism. Maybe I had a misunderstanding of compiler errors. I thought I shouldn't be able to run a program with an error like this. I know I should implement all methods of the interface or make the class abstract like you say. Actually, I was teaching oop, and i was telling this to my students and one of them told me he was able to not implement all methods and his program run very well. Of course, it doesn't mean it is correct, if you call the unimplemented methods of course it will throw a runtime error. I just wanted to explain them why it was possible. – Candy E. Sansores Sep 04 '18 at 18:57