+ 3
Problems
pls ans my code... https://code.sololearn.com/c5nrYNuG3L6D/?ref=app https://code.sololearn.com/c5nrYNuG3L6D/?ref=app
16 Respuestas
+ 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).
+ 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();
}
+ 4
Change
void Display(){
System.out.println(super.m);
}
}
to:
int Display(){
return super.m;}
and then do this:
System.out.println(super.Display());
+ 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.
+ 3
try this:
System.out.println(m);
System.out.println(this.m);
System.out.println(((B)this).m);
System.out.println(((A)this).m);
+ 3
that working..
great job M N
can you explain me pls
Thanks all of you
+ 3
Do you need explanation on the function i suggested you?
+ 2
pls see the code everyone and reply me
+ 2
Replace line 27 with
System.out.println(new A().m);
n you will get 10 :)
+ 2
bro..
i got your point..
but.
i want to acces without create an object of that class
+ 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();
}
}
+ 2
I understand your point
Tomer Sim
+ 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.
+ 2
Thanks i got it..
SatyaJit
+ 2
i appreciate
+ 1
that a great idea
Tomer