Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance and Variable Shadowing Dilemma

 
C. Mitzel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the problem: Class B extends class A, and inherits class A's printX() method. Both A and B have a declared variable x. In A, it's 5; in B, it's 10. Program VarStuff creates an object of class B and invokes its printX() method. The expectation is that the method will act as if it belongs to B, and call the x variable in B. But instead, it prints 5.
Could anyone please explain why this happens, or even better, why it is supposed to respond this way?
Thanks much
class A {
int x = 5;
public void printX() {
System.out.println(x);
}
}
class B extends A {
int x = 10;
}
public class VarStuff {
public static void main(String[] args) {
B b = new B();
b.printX();
}
}
Results:
5
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Defining a variable in the subclass with the same name as one in the superclass is never a good idea, almost always a mistake. It's called "shadowing," as you apparently already know.
The method in the parent class is compiled to access the variable from the parent class. "Shadowing" the variable is nothing at all like method overriding; variables aren't dynamically resolved the way that methods are.
If you need a new value for the variable in a child class, just set the parent's variable; don't try to redefine the variable in the child class. If it's not accessible, then the author of that class didn't want you to set it, and changing the value would probably cause the class to malfunction.
 
C. Mitzel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest... that actually leads to another question. Well... several, if you have a moment.
1. Would it be correct to say that by inheriting members of parent classes, a subclass actually contains all of the code that it inherits, or does that code still reside in the other classes?
1a. Closely related: When all of the constructors in the parents of a class are called, do those constructors create separate objects, or is it all code belonging to one conglomerate subclass, and only one object is created?
2. How can abstract classes have constructors if you can never instantiate them?
Thanks much,
CM
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Would it be correct to say that by inheriting members of parent classes, a subclass actually contains all of the code that it inherits, or does that code still reside in the other classes?
You should think of it as the code still resides in the other classes. That will make the example above make a little more sense, methinks.
1a. Closely related: When all of the constructors in the parents of a class are called, do those constructors create separate objects, or is it all code belonging to one conglomerate subclass, and only one object is created?
Only one, conglomerate object will be created; this may seem a little bit disparate from the above answer, but the difference is this: variable and static method binding is done at compile-time (by javac), while object creation/polymorphism is done at run-time (by the JVM). So, the code is considered "separate" when javac is doing its thing, but "together" when the JVM is doing its thing.
2. How can abstract classes have constructors if you can never instantiate them?
Abstract classes might still need to initialize variables; if you had a Mammal abstract class, you might have a constructor that looks like:

or some such. This means that you don't have to write this code in every subclass of Mammal (but you will have to providethe constructor that calls the superconstructor...)
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. Would it be correct to say that by inheriting members of parent classes, a subclass actually contains all of the code that it inherits, or does that code still reside in the other classes?

I guess you'd have to define "reside in". I think it's bst to think of it like the child's toy "Mr. Potato Head." The Potato part is the base class. Defining a subclass is like adding some of the eyes and noses and mouths. You've got the same potato in there, but then it has more stuff too.

1a. Closely related: When all of the constructors in the parents of a class are called, do those constructors create separate objects, or is it all code belonging to one conglomerate subclass, and only one object is created?

Only one object. The superclass constructor initializes the parent part of that object, and the child constructor initializes the child part.

2. How can abstract classes have constructors if you can never instantiate them?

See the answer to 1a. The constructor of the abstract superclass initializes the superclass part of an object, and the subclass constructor does the rest.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic