+ 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
39 Respuestas
+ 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.
+ 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.
+ 4
Just mention it,
childobject.field = somthing
The child inherits fields and methods of it's super class.
+ 4
This is one way.
https://code.sololearn.com/crARAS5F3Qy4/?ref=app
+ 4
Inside your child constructer
super.filed = value
+ 4
What you want to achive bro Mahdi Ardini
+ 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?
+ 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?
+ 4
Sumit Programmer😎😎 yes I see, because creating an object of parent ignores whats happening in the child class?
+ 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.
+ 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);
+ 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
+ 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
+ 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.
+ 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.
+ 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 👍
+ 3
David Carroll 😅alright I know.... I'm not a java programmer... I don't know wht static means..
+ 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.
+ 2
https://code.sololearn.com/c8vTByBW5V4w/?ref=app