+ 1
i changed the value of variable of superclass but there is no change in subclass.why?
Hi guys I have 3 class : Super1 & Sub1 & Main Super1 is parent Sub1 is child there is public variable m in Super1 and the Main class is: public class Main { public static void main(String[] args) { Super1 gholam=new Super1(); gholam.m=10; Sub1 akbar=new Sub1(); System.out.println(akbar.m); } } the output is 0 why? i expected to watch 10
9 ответов
+ 3
They're different instances. That's like saying if I have a mold for an object (parent class) and create a second mold based off the first (child class) and then create an object from each mold (instances) , then when I change some property of the parent object that the same property of the child object will also change. (Changing the color of the object created from the parent mold won't change the color of the object created from the child mold).
The exception to this is if the property of the parent mold is the exact same property of the child (static). Then any change made to said property would be made across all instances of all parent and inheriting children objects.
+ 4
It is because that the instance variables are bound to the object of the class and not to the class itself, unlike static members.
So every instance will have it's own copy of the variable 'm'. Considering it is not assigned a value, it returns 0.
+ 3
Without seeing the makeup of the Super and Sub class. We can only give some educated guesses which may lead to incorrect answers and frustration on both parts.
For now gholam and akbar are separate instances that are each built off of their class blueprint definitions. So, unless m is a static inherited variable then of course the value for akbar will be whatever it is originally set to in the class definition.
+ 1
ChaoticDawg Mohammad Touseef there isn't any statement in Sub1
Sub1 class is only the child of Super1 class
and there is only 1 statement in Super1:
public int m;
and the main class that i post
+ 1
Avinesh but the Sub1 class is child of the Super1 class
so i thought that any change in parent will change the child
is that wrong?
+ 1
ChaoticDawg
yes you right
I was really confused
thanks
I now understand
you explained perfect😊
+ 1
saeedeh In Java every object contains different values for their fields even if the objects are from the same class, because technically speaking the class is only the blueprint so all fields will have different values for each object unless of course that field is declared static. Now specking in the context of inheritance, the instances of the subclass will still not have the same field values as the instances of the superclass because again the class is only a blueprint so all objects won't and don't have to look the exact same unless of course the field is static and the value is shared between all the superclasses instances and the subclass instances . Hope this helps
+ 1
Samuel Adejumo yes your explanation is very useful
thanks
0
saeedeh
Please post full code