Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Interface

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,
I was not able to understand the behaviour of variables present in the Interface.
As  static methods and static variables are class level, so they are not inherited by the subclass. So I am not able to understand that how we are able to call the variable declared in the interface with the help of implementing class Object.



As in above example, as all the variables declared in interface are by default final and static, so I am not able to understand how we can call a static variable of interface with the job class Object.
Please help me out with this.
I am confused.
 
Marshal
Posts: 73738
332
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sahil Kairon wrote:Hello Everyone,
I am preparing for OCA . . .

So I shall move you to our OCA forum.

behaviour of variables present in the Interface.

Interfaces don't have variables. They only have fields as public “constants”.

As we all know . . .

I don't know that.

Beware of saying, “as we all know,” or similar. If you are wrong, it makes you look daft, and if you are right, it makes anybody who doesn't know feel stupid.

static methods and variables are class level, so they are not inherited by the subclass. . . . .

Where on earth did you find that? It looks incorrect. The only members that cannot be inherited by implementing classes or subtypes are private members and are static methods in interfaces.
 
sahil Kairon
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the constant of interface are inherited by the implementing class ?
 
Campbell Ritchie
Marshal
Posts: 73738
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Didn't you see that in yout example? Have you read the Java® Language Specification about members of classes or inheritance?
 
sahil Kairon
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I am quite new to this. And I am learning java.  Please ignore if i sound stupid. What I have learnt so far was that static method are not inherited by the subclass and they cannot be over-riden. So I was confused that "are static variables inherited by the subclass" ?
 
Master Rancher
Posts: 1005
27
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To defend the OP, or more accurately, to help understand where the confusion comes from, since this is education and not a debate.

It is definitely true that it is not possible to override static methods in sub-classes.  If you attempt to do so, this will just hide them instead.
There is no polymorphic behavior with respect to static methods, unlike abstract and default ones.

This causes too many people being listened to/watched/read by too many other people to say "static methods aren't inherited" which is not true.

Fraternizing with such sources caused ME to repeat this, and I know better.

The core distinction being blurred here is inheritance vs. polymorphic/overriding behavior.

My mis-statement and "Doh!"-moment can be found here:
https://coderanch.com/t/745070/java/public-static-final-method-modifier
 
Jesse Silverman
Master Rancher
Posts: 1005
27
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sahil:

An additional source of confusion is the counter-intuitive phrase of "constant variable".
I hate the term, it sounds like it came from the song "Oh! Susanna":
https://en.wikipedia.org/wiki/Oh!_Susanna

I prefer to just say constants, but that could mean "constant literal" which is its own concept.

At the level of the Java Language Specification, which needs to be Very Precise, the term "constant variable" is used somewhat frequently.
People who go to the JLS to resolve disputes (it is sort of like the Supreme Court for Java) will use the term as well, for precision.  You can just think "constant" here.

So interfaces can contain "constant variables", since they are implicitly static and final.
Many normal people will just say that "interfaces can contain only constants", in JLS terms they would be said to contain variables that are implicitly "constant variables".
Feel free to write out any or all of the public static final modifiers on all your constant variable declarations in interfaces, they are implied whether we type them or not.
There are numerous exam questions testing to see whether or not you remember this.

You are additionally getting bit by a "Very Bad Thing" in Java which causes much confusion to many.

You should not, but are totally allowed to, refer to static methods and data by using the name of a reference.
Repeat, you should not do this, it leads to confusion.  Java has allowed it since the beginning, will never make it illegal because it would break billions of lines of code, so learn to recognize it when you see it but don't do it.

So in your lines of code below:

job j = new job();
 System.out.println(j.i);


It sure LOOKS like i is an instance variable, doesn't it?
Well, it isn't.  It is a bad and discouraged way to refer to the static constant (um, constant variable) of the Test interface.

Which brings me to one more point about this code sample.

It is legal to have class and interface names as ALL_CAPS, all_lowercase, or likeTHIS.

You need to know that for the exams.

Please do not name actual classes or interfaces in any of those ways, this also leads to confusion, frustration and costly errors.
So for the exam, you can capitalize class and interface names however you want, the Java compiler doesn't care.
It will very much confuse your readers, however.  Use names like Test and Job, but avoid using names from the Java standard classes that are likely to be imported and used in your programs, that can also confuse people.  

Same with referring to static methods and data members as if they were instance ones.
It is legal, Java doesn't care, you should know that for the exam.
But, it is much better to not do this in your own code to avoid confusion.

This one question brought up several Very Confusing things about Java for beginners (or forgetful experts).
The good news is that there aren't all that many more, so in a way you are lucky if you now understand these things!
 
sahil Kairon
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much Jesse Silverman for helping me out. Really appreciate the efforts. I know many beginners like me will confuse in such topics and will make sure to help them with such patience and clarity as you did today.
 
Jesse Silverman
Master Rancher
Posts: 1005
27
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To steal Campbell's catchphrase, as I haven't come up with my own yet:

It's a pleasure!



There's also no better way to keep confusion about murky awkward things that can creep into one's code than to identify and clarify instances of people currently being confused by them, so it will likely help me in my own work as well.
 
Campbell Ritchie
Marshal
Posts: 73738
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have found, looking through the JLS, that I was mistaken earlier about static methods being inherited Sorry.
 
Marshal
Posts: 3643
516
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:To steal Campbell's catchphrase, as I haven't come up with my own yet:

It's a pleasure!


Suggestion #4 in Lucy's 12 ways to response to thank-you ...

 
Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic