• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initializing an interface

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am learning Java and am really confused why this code compiles and prints "test". I thought you can not initialize an interface.


 
Bartender
Posts: 5465
212
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Neal,

welcome to the Ranch! I hope you'll enjoy the stay.

You do not 'initialize' an interface. If you create a class that 'implements' an interface, you are creating a class that gives an implementation for every abstract method that is in that interface. No doubt other people will point to abstract classes, but that for some other time.

With the statement

you are creating an instance of an anonymous class that implements the interface 'I'. That is what happens on the right side. What exactly does it implement? Have a look at the interface 'I'. As you see, it has no abstract methods to implement, and so a class that does implement 'I' has an easy job: it needs to implements nothing! That is what happens on the right hand side: new I() {};

But I has a default method, that is available to any instance of a class that implements 'I'. So the 'tester' variable has the method 'printer' available, and applies it.

I hope it makes the code a little more comprehensible.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch again.

Since default methods were introduced in Java8, I shall duplicate this discussion in our Java8 forum. PS has given you a correct answer, but I shall add some challenges to it. Can you instantiate that interface via a λ expression? Can you override that method?
 
Neal Kornreich
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! The code makes much more sense now. Do you know if anonymous classes are on the OCA exam?  I can't seem to find anything on them in my textbook.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should know about anonymous classes whether they come up on the exam or not.
I looked through the OCA/OCP7 study guide by Kathy Sierra and Bert Bates and anonymous classes appear in the OCP section only, but you should seek confirmation from the Oracle site about those exams.
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neal Kornreich wrote:Do you know if anonymous classes are on the OCA exam?

I've checked OCAJP8: 1Z0-808 syllabus It's not there but It is mentioned in OCPJP8: 1Z0-809 but as Campbell said you better refer Oracle's official site for such information which is always up to date before taking any exam.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganesh Patekar wrote:. . . I've checked . . . .

That does appear to be the official site; it would be very naughty for them to change the syllabus without much prior notice and warning.
 
Neal Kornreich
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. Yeah I just ran into the question on a Whizlabs practice test for OCA and was confused why it was there. Seems like there are a decent amount of questions on their practice tests that are irrelevant for OCA like nested class and interfaces and testing on whether or not you can identify if something is new in SE8.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch again.

Since default methods were introduced in Java8, I shall duplicate this discussion in our Java8 forum. PS has given you a correct answer, but I shall add some challenges to it. Can you instantiate that interface via a λ expression? Can you override that method?


Trick question, OCAJP worthy! Something to do with Functional Interface, perhaps?    
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be and it might not.....
 
reply
    Bookmark Topic Watch Topic
  • New Topic