Forums Register Login

Variables in interface

+Pie Number of slices to send: Send
Variables defined in interface are public static and final so I was thinking that we should not be able to override the variables in a class thats implementing the interface.

But when I am compiling the below class, it compiles fine and gives the correct values. but when I did disp.abhi = 35; it gives a compile error (cannot override final variable)




+Pie Number of slices to send: Send
You can never override fields. You are hiding them. Those are completely new fields which cause you to ignore any fields in the interface.
+Pie Number of slices to send: Send
And welcome to the Ranch

There are other people who waited much more than 5 years between registration and first post
I added code tags to your post: doesn't it look better:) Always use the tags.
+Pie Number of slices to send: Send
Hi Ritchie,

Thanks for the reply.

I guess this is what is called as "Shadowing". And yes, I will use tags going forward while posting questions. I have been preparing for 803 certificaion for 2-3 months and trying different combinations to see how will the code behave.

+Pie Number of slices to send: Send
You're welcome It is called hiding (I am pretty sure); we have an FAQ about it (but that might only cover methods).
+Pie Number of slices to send: Send
The code provided compiles? Really?
It doesn't for me.
On the line:
Error: cannot instantiate the type display

Oh, and the reason for the error you mentioned: As well as being public and static, any variables declared in an interface are also final.
So assigning a new value to them is an error.
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 451 times.
Similar Threads
Interface
Confusion regarding multiple inheritance
How to find the base class
Order of Variable declaration
Is it possible to put constructor inside a method?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:43:09.