• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on default methods in interfaces (OCA8 only)

 
Raghavendra Desoju
Ranch Hand
Posts: 95
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please see below code:-



It is printing the value from SuperClass not the Interface. Are there any rules defined for these kind of situations?

Thanks,
Raghu
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavendra Desoju wrote:Hi,

Please see below code:-



It is printing the value from SuperClass not the Interface. Are there any rules defined for these kind of situations?

Thanks,
Raghu


I don't mind helping my friend, but it would be worth looking up exactly how a java interface works first. I'm pretty new at this, but think that code is doing exactly what it should be (actually I'm surprised it compiles!)
 
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
nick woodward wrote:I'm pretty new at this, but think that code is doing exactly what it should be (actually I'm surprised it compiles!)

It only compiles in Java 8! In Java 7 the interface will not compile because default methods are not allowed.
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
nick woodward wrote:I'm pretty new at this, but think that code is doing exactly what it should be (actually I'm surprised it compiles!)

It only compiles in Java 8! In Java 7 the interface will not compile because default methods are not allowed.


I just ran it and it confused the hell out of me (I'm running java 8, but learning 7). Thanks!

By the way, I couldn't edit the above message. Not sure why.
 
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
Raghavendra Desoju wrote:It is printing the value from SuperClass not the Interface. Are there any rules defined for these kind of situations?

As you can see from your code snippet, the class definition always beats the interface version. You'll find more detailed information in this topic.

And what do you think about this code

Hope it helps!
Kind regards,
Roel

PS. I added "(OCA8 only)" to the subject of this topic to indicate this question is only appropriate if you are preparing for the OCA8 exam and OCA7 aspirants get not confused by this question. Hope that's ok with you!
 
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
nick woodward wrote:
Roel De Nijs wrote:
nick woodward wrote:I'm pretty new at this, but think that code is doing exactly what it should be (actually I'm surprised it compiles!)

It only compiles in Java 8! In Java 7 the interface will not compile because default methods are not allowed.


I just ran it and it confused the hell out of me (I'm running java 8, but learning 7). Thanks!

I would suggest installing JDK7 when you are preparing for the OCA7 exam. As you just noticed there are actual differences between both Java versions and this one is a big one but there are subtle differences as well. So compiling code in Java 7 could result in a compiler error, but exactly the same code might compile successfully in Java 8.
 
Poyan Gerami
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Raghavendra Desoju wrote:It is printing the value from SuperClass not the Interface. Are there any rules defined for these kind of situations?

As you can see from your code snippet, the class definition always beats the interface version. You'll find more detailed information in this topic.

And what do you think about this code

Hope it helps!
Kind regards,
Roel

PS. I added "(OCA8 only)" to the subject of this topic to indicate this question is only appropriate if you are preparing for the OCA8 exam and OCA7 aspirants get not confused by this question. Hope that's ok with you!



it seams you get complie error when you have 2 interface classes that have same default method name and you try to implement both this interfaces in your class.
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote: . . . (I'm running java 8, but learning 7). . . .
Why? If you have got Java8 why not learn the new features too.

By the way, I couldn't edit the above message. Not sure why.
We have restrictions on editing.
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
nick woodward wrote: . . . (I'm running java 8, but learning 7). . . .
Why? If you have got Java8 why not learn the new features too.


well initially it was because there was only the beta for the exam. that and most of my resources revolve around 7 (enthuware, mala guptas book, KB7). how much more content is there to 8? i really need to get on and take this exam to be fair, but wouldn't be adverse to reading something like Jeanne Boyarsky's OCA book - have always liked her explanations of concepts on here.



By the way, I couldn't edit the above message. Not sure why.
We have restrictions on editing.


yeah, i just couldn't quite grasp why it limited the edits so quickly. nevermind, not important!
 
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
nick woodward wrote:how much more content is there to 8?

Not so much: simple lambda expressions and some classes from new date/time API. You'll find the mapping between the exam objectives of the OCA7 and OCA8 exams here.
 
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
Poyan Gerami wrote:it seams you get complie error when you have 2 interface classes that have same default method name and you try to implement both this interfaces in your class.

True! This code will not compileAnd that makes sense, because there's no way the compiler knows which method you want to execute if line1 is invoked. Should it be A.foo() or B.foo()? With the code snippet as-is, this can't be determined hence the compiler error.

But you can easily fix this by overriding the foo method in ClassAB, as illustrated in this code snippetNow the code will print in ClassAB.foo()

But what do you do if you actually want to execute the foo method of B (instead of a custom implementation)? This can be easily done as well by adjusting the previous code snippetSo now the code prints in B.foo()

Hope it helps!
Kind regardss,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic