Problems | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
16 Answers
+ 9
Simple answer: You can't access a member of a super parent's class. If you need to do that you have to redesign your classes. The sub sub class shouldn't need to access the super super class A. B should be able to handle the request from the subclass on it's own (encapsulation principle is broken otherwise).
2nd Apr 2018, 8:38 PM
Tashi N
Tashi N - avatar
+ 4
You can also do this, add this function to class b. then call it from class Program void Display(){ System.out.println(super.m); } the whole class should look like this: class A { int m=10; } class B extends A { int m=50; void Display(){ System.out.println(super.m); } } public class Program extends B { int m=100; void display() { int m=200; System.out.println(m); System.out.println(this.m); System.out.println(super.m); super.Display(); }
2nd Apr 2018, 8:40 PM
Tomer Sim
Tomer Sim - avatar
+ 4
Change void Display(){ System.out.println(super.m); } } to: int Display(){ return super.m;} and then do this: System.out.println(super.Display());
2nd Apr 2018, 8:50 PM
Tomer Sim
Tomer Sim - avatar
+ 4
Well: int Display(){ return super.m;} means this. int Display = a function named Display which will return an integer value. return super.m; means return the m value from my super class. since were in class b so its parent is class a. which has the m value of 10. because of that . super.m=10; so we define this function: int Display(){ return super.m;} which will return the value of m in the parent of class b(class a).which is 10.
2nd Apr 2018, 9:19 PM
Tomer Sim
Tomer Sim - avatar
+ 3
try this: System.out.println(m); System.out.println(this.m); System.out.println(((B)this).m); System.out.println(((A)this).m);
2nd Apr 2018, 8:47 PM
🇮🇷 M N
+ 3
that working.. great job M N can you explain me pls Thanks all of you
2nd Apr 2018, 8:51 PM
Arun Tomar
Arun Tomar - avatar
+ 3
Do you need explanation on the function i suggested you?
2nd Apr 2018, 9:15 PM
Tomer Sim
Tomer Sim - avatar
+ 2
pls see the code everyone and reply me
2nd Apr 2018, 8:14 PM
Arun Tomar
Arun Tomar - avatar
+ 2
Replace line 27 with System.out.println(new A().m); n you will get 10 :)
2nd Apr 2018, 8:35 PM
SatyaJit Pradhan
SatyaJit Pradhan - avatar
+ 2
bro.. i got your point.. but. i want to acces without create an object of that class
2nd Apr 2018, 8:38 PM
Arun Tomar
Arun Tomar - avatar
+ 2
actually problem is.. My teacher say.. you can only call there class A { int m=10; } class B extends A { int m=50; } public class Program extends B { int m=100; void display() { int m=200; System.out.println(m); System.out.println(this.m); System.out.println(super.m); System.out.println();//only here } public static void main(String[] args) { Program p=new Program (); p.display(); } }
2nd Apr 2018, 8:42 PM
Arun Tomar
Arun Tomar - avatar
+ 2
I understand your point Tomer Sim
2nd Apr 2018, 8:43 PM
Arun Tomar
Arun Tomar - avatar
+ 2
Sorry bro it violates encapsulation. You can use implementations like ((a)this)... but that is different from the concept of parent and super class. It's fine to say, "No, I don't want my own behaviour - I want my parent's behaviour" because it's assumed that you'll only do so when it maintains your own state correctly. However, you can't bypass your parent's behaviour - that would stop it from enforcing its own consistency. If the parent class wants to allow you to call the grandparent method directly, it can expose that via a separate method... but that's up to the parent class. Sorry for late reply. Hope it helps.
2nd Apr 2018, 9:01 PM
SatyaJit Pradhan
SatyaJit Pradhan - avatar
+ 2
Thanks i got it.. SatyaJit
2nd Apr 2018, 9:09 PM
Arun Tomar
Arun Tomar - avatar
+ 2
i appreciate
2nd Apr 2018, 9:16 PM
Arun Tomar
Arun Tomar - avatar
+ 1
that a great idea Tomer
2nd Apr 2018, 9:21 PM
Arun Tomar
Arun Tomar - avatar