+ 4

how can i change the value of a attribute in the superclass while i am in the childclass?

please answer me with some code

8th Sep 2020, 4:48 PM
Mahdi (Twelve Imam)
39 Réponses
+ 6
Sumit Programmer😎😎 yes that's because your casting to parents usjng polymorphism and does not know about its child methods unless its overridden during runtime.
8th Sep 2020, 8:04 PM
D_Stark
D_Stark - avatar
+ 5
Becz inside your heap memory 2 object will be created animal or lizard and inside animal you are delcar valriable a is 10 so if you changed the value of super class in sub class it does not mean your super class veriable value will be changed.
8th Sep 2020, 5:16 PM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 4
Just mention it, childobject.field = somthing The child inherits fields and methods of it's super class.
8th Sep 2020, 4:55 PM
D_Stark
D_Stark - avatar
8th Sep 2020, 5:03 PM
Avinesh
Avinesh - avatar
+ 4
Inside your child constructer super.filed = value
8th Sep 2020, 5:03 PM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 4
What you want to achive bro Mahdi Ardini
8th Sep 2020, 5:06 PM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 4
Sumit Programmer😎😎 yes. i told you in my new code... the question is that: why the result is 10 yet while we declared the constructor and changed the value of int a in class animal?
8th Sep 2020, 5:13 PM
Mahdi (Twelve Imam)
+ 4
Sumit Programmer😎😎 hey buddy see this part you wrote... if you changed the value of super class in sub class it does not mean your super class veriable value will be changed. Did you say this because both classes have the same variable name?
8th Sep 2020, 6:09 PM
D_Stark
D_Stark - avatar
+ 4
Sumit Programmer😎😎 yes I see, because creating an object of parent ignores whats happening in the child class?
8th Sep 2020, 6:29 PM
D_Stark
D_Stark - avatar
+ 4
D_Stark If you see boths hashcode are same its mean both sub and parent are pointed to sub object in heap its menas if you cast sub to parent its does not create instance of parent in heap its simply points sub object thats whay output will be jack.
8th Sep 2020, 7:06 PM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 3
Mahdi Ardini 'anim' , and 'li' objects are different ones. Each of which have different spaces. So you are not changing anim.a by lizard constructor. If you want to change through passed objects, this works.. Remove int a from child class Then put anim.a = li.a; String int cello= anim.a; System.out.println(cello);
8th Sep 2020, 5:30 PM
Jayakrishna 🇮🇳
+ 3
D_Stark yeah in real world example you will get your dads behavior and you modified those behavior according to your recruitment but its does not mean your father behavior changed
8th Sep 2020, 6:30 PM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 3
Sumit Programmer😎😎 is there away to see that in code? Because even if you cast your child object to parent type the inherited field has been modified I cant find away for the child to get the parents original field value without having to create an instance of parent. https://code.sololearn.com/cIizFV2RHAGX/?ref=app
8th Sep 2020, 6:41 PM
D_Stark
D_Stark - avatar
+ 3
D_Stark Regarding your previous comment: ---- "Because even if you cast your child object to parent type the inherited field has been modified I cant find away for the child to get the parents original field value without having to create an instance of parent." -- Since name wasn't defined in the Sub type, both the Sub and the Parent types are referring to the same instance of name, regardless of where it's accessed. In your code example, you simply assigned a value to this.name from the Sub type. To accomplish what you are attempting, you need to declare the same property name in the child for it to have a separate memory space for name.
8th Sep 2020, 7:19 PM
David Carroll
David Carroll - avatar
+ 3
Sumit Programmer😎😎 Regarding your earlier comment: ---- "But... if you create new method in child and if you acces those method using typecasted parent referace veriable then you will get compiler error even your parent referance veriable points to sub object." -- If I understand your comment, you're referring to something known as down casting from a derived type to a base type. If so, a compiler error would occur because the base type would only be aware of its own members. It would not be aware of the method in the sub type. --- D_Stark Regarding your comment: ---- "I'm trying to understand how an inherited field can be changed in sub class without it affecting the field in parent without having to add the same field in sub class... it's not possible right?." -- Right. The sub type would need to store its copy of the value in a separate field instance from the one used by the parent for each to have their own values.
8th Sep 2020, 10:51 PM
David Carroll
David Carroll - avatar
+ 3
David Carroll thanks for that , for a moment I thought I was going mad 😰, just spent the past 2 hours going over it all again. Glad your still here 👍
8th Sep 2020, 11:22 PM
D_Stark
D_Stark - avatar
+ 3
David Carroll 😅alright I know.... I'm not a java programmer... I don't know wht static means..
9th Sep 2020, 4:05 PM
Max Andy MOUMOUNI
Max Andy MOUMOUNI - avatar
+ 2
Theres a couple of things to take into consideration when using inheritance If you use any fields names that are the same in child class to them in parent class, those variables in parent are hidden when you create a child object.. very similar to how overridden methods work.. If you want to use the inherited variables in parent class inside child class you can use this.field as child class has access to those fields If you use protected it means classes in other packages cannot access those fields.
8th Sep 2020, 5:38 PM
D_Stark
D_Stark - avatar
+ 2
https://code.sololearn.com/c8vTByBW5V4w/?ref=app
8th Sep 2020, 5:47 PM
D_Stark
D_Stark - avatar