• Post Reply Bookmark Topic Watch Topic
  • New Topic

Two interfaces, two variables with the same name in each. I want to use both.  RSS feed

 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.

Is it possible to define and use two variables from different interfaces, if they have the same name?

Example:

 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Aleksey, welcome to the Ranch!

If I'm not mistaken, you would use A.test and B.test to use those two variables. But you don't need to implement the interfaces to do that.

If you were looking for a way of simply using test somewhere in that C class and having it mean one of those two possibilities, then clearly there's an ambiguity there which you have to clarify. I suspect you might get a compiler error if you do that, but expanding your posted code to a small program which tests that idea, that would be pretty simple. Why not try it and let us know what you find out?
 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, it's working.

I'm quite new to Java and I learn interfaces now, so example was just from my head.
I read about overriding methods and was wondering whether this is possible to override variables. Turns out they all are constants in interfaces
 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you're correct about that. Whether they are in interfaces or classes, you can't override variables in Java. And yes, "variables" defined in an interface are implicitly static and final, which makes them act as constants.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!