• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance  RSS feed

 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code: I do not understand the variable location. In the interface Movable it is a public, static and final variable; in the class Donkey it is a default variable. That means the class donkey inherits a static variable and it declares an instance variable. I do not understand why there is not a conflict, both variables have the same name. How does it work?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
546
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs,'YOu are allowed to have variables with the same name that are static/instance. It's with methods where this starts to get tricky.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an other code: It does not compile. This code compiles. The class Test inherits the public, static and final variable x; it has an instance varible x. In the first code it is not possible to have a static variable named x and an instance variable named x; it does not compile. I am looking for an explanation.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs Waefler wrote:There is an other code: It does not compile.


A Java class can't have (declare) an instance variable and static variable with the same name.  Doing so would be a case of "hiding" a variable, with no way to get to the "hidden" variable.

Henry
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. I got it. What is about the other code? It seems that inheriting a static variable to a class, which has an instance variable with this name, is not a problem; probably because the static variable does not belong to the class?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seemed to have answered your own question.

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