• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface  RSS feed

 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

i have wriiten a small piece of code:



From i came to know that we create a reference variable of an Interface and we cant created an object.AM I RIGHT?


and can you please let me know how 10 is getting printed.

Thanks in advance.
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a reference of type Inter but the instance its referring to is of InterfaceTest.

Do read the Inheritance and Interfaces tutorials to get an idea of how inheritance works and how the variable in Interface is getting accessed by the implementing class.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables defined in an interface are implicitly public static final, in other words, they are static constants. So your interface is the same as this:

You don't need an instance to access static members. You could just as well have written this to print the value of i:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!