Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interesting case regarding invoking superclass/interface methods with super keyword (OCA8)

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following question is my product I encountered interesting case when practicing codes and I would like to share it with you

Which statement(s) is/are true?

A. It will print 'Interface'.
B. It will print 'Abstract'.
C. It will not compile, if line 1 are commented out.
D. It will not compile, if line 2 are commented out.
E. It will not compile, if line 3 are commented out.
F. It will not compile.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the interface has a default method, this questions is only intended for those preparing for the OCA8 (and OCP8) certification exam. So I updated the subject accordingly, hope that's ok with you.

Have a cow for sharing a nice mock question you have created yourself. That's much better than sharing a question which will never be on a certification exam

I think I know the answers, but I would of course not spoil the fun of other ranchers, so I keep quiet for some time
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mushfiq, for posting such an interesting question. It makes us think .

B , D are the answers. Since the abstract method is called hence if you comment them out, it will not compile.


If there was another Interface instead of an abstract class, it wouldnt compile because they are conflicting, and the subclass might have to override this method. But because its an abstract class, Java lets you have such a method with the same name and it compiles.

If you want to specifically call the default method of the interface you can use the

Now why is the Abstract method called and Interface method not called ??
I dont have a solid answer for this, maybe because the Sub is a true concrete subclass extending the abstract super class.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:B , D are the answers.

If this was a question on a mock exam (or even worse, the actual exam) you would get 0 points for these answers
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry B,D,E are the answers.

It compiles because you have overridden the test method in your subclass.

My assumption is wrong here I guess
If there was another Interface instead of an abstract class, it wouldnt compile because they are conflicting, and the subclass might have to override this method. But because its an abstract class, Java lets you have such a method with the same name and it compiles.


 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:-( ..okay thats bad. so whts the answer Roel.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:B,D,E are the answers.

Much better! For these answers you'll get full marks (1 point)

Ramya Subraamanian wrote:My assumption is wrong here I guess
If there was another Interface instead of an abstract class, it wouldnt compile because they are conflicting, and the subclass might have to override this method. But because its an abstract class, Java lets you have such a method with the same name and it compiles.

Yes, your assumption was indeed incorrect!

If there would be another interface Inf2 defining a method with the same signature, the code will still compile. Because class Sub defines its own implementation. So this code compiles successfully as well (and will still print Abstract)But if you would remove the test() method from class Sub, you'll get a compiler error. Because the compiler doesn't know which test() method should be used: the Inf one or the Inf2 one. But class Sub defines its own implementation, this issue is fixed. And if you want to execute the Inf2 test() method, you'll need to use this implementation in the test() method of class Sub.

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay. do we get 0 points even if we get some answers partially right in the real exam ( like B,D instead B,D,E)???
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:okay. do we get 0 points even if we get some answers partially right in the real exam ( like B,D instead B,D,E)???

I think my previous reply already answers that question.

The grading system is pretty easy: you only get 1 point if you have selected all correct answers; 0 points otherwise (so no points for partially correct answers). No negative points for wrong answers (so you can - if needed - guess for free )
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any insight into why the Abstract method is called and Interface method not called ?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:Any insight into why the Abstract method is called and Interface method not called ?

Let's ask the question the other way around: why would you think the interface method should be called instead of the superclass method? Don't forget an interface is nothing more than a contract. Since Java 8 you can add default (and static) methods to provide a default implementation if you add a new method to the interface (required to implement e.g. the new Stream API). Otherwise all implementing classes won't compile anymore until the method is implemented. Without this feature no backwards compatibility anymore.

And what if the interface method was defined as an abstract method instead? Would you still expect the interface method to be executed?
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subramanian and Roel, thanks for interesting discussions. The error text of option E was surprised me more than others:


I couldn't understand what is relation between Abs class and Inf interface.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:I couldn't understand what is relation between Abs class and Inf interface.

At first, it might be a little bit surprising to see that compiler error message. But if you think about if for some time, it should be crystal clear as the reason why is completely related to the rules for method overriding (and not for some Java 8 feature). The class Sub implements interface Inf, so it has to implement the public method test(). If class Sub decides not to implement this method, then the default method defined in interface Inf will be used (which is definitely a Java 8 feature). If class Sub decides to implement this method, it must adhere to all method overriding rules (that's plain old Java ). And in this example class Sub inherits a test() method with exactly the same method signature as the method in interface Inf but with a weaker access modifier, so that's a compiler error.

And if you think it might be related to Java 8 (and default methods), that's definitely not the case. In Java 7 (and prior versions) you can simulate exactly the same compiler error with this code snippetTrying to compile this code snippet using javac produces this result:
B.java:7: error: go() in A cannot implement go() in I
class B extends A implements I {
^
attempting to assign weaker access privileges; was public
1 error

Like I promised, it's exactly the same (with other class, interface, and method names of course)

Hope it helps!
Kind regards,
Roel
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your helpful reply, Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic