I have a class that extends a class that I need to overide, but I need to call that class's parent class. si开发者_如何学Gonce I can't call super since that will execute the direct parent what is the best way to get the parent of my parent class?
I am sure this is a basic question, but its been a while since I have been doing any java.
class A
{
public void myMethod()
{ /* ... */ }
}
class B extends A
{
public void myMethod()
{ /* Another code */ }
}
class C extends B
{
I need to call Class A here
{ /* Another code */ }
}
You don't: it violates encapsulation.
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.
You can't because it has been overridden by B. There is no instance of A inside C.
The thing is that you want to call a method of the class A, but from which instance? You have instantiated C, which is a class with a couple of own and inherited methods from B. If B is overriding a method of A, you can't access it from C, because the method itself belongs to C (is not a method of B, it has been inherited and now it's in C)
Possible workarounds:
B does not override myMethod()
C receives an instance of A and saves it as a class property.
myMethod()
in A is static and you use A.myMethod()
(I don't recommend it)
What you are asking is bad practice, What you are saying is that C is not a B, but an A. What you need to do is have C inherit from A. Then you can call super.
If not this is the only way...
public String A()
{
String s = "hello";
return s;
}
public String B()
{
String str = super.A();
return str;
}
public String C()
{
String s = super.B();
return s;
}
Besides what pakore answered, you could also chain super.myMethod()
calls if that works for you. You will call myMethod()
from A from the myMethod()
in B.
class A {
public void myMethod() {
....
}
}
class B extends A {
public void myMethod() {
....
super.myMethod();
....
}
}
class C extends B {
public void myMethod() {
....
super.myMethod(); //calls B who calls A
....
}
}
You will eventually be calling myMethod from A, but indirectly... If that works for you.
You can't call A's method directly. In the few cases that I've hit this, the solution was to add a method in A to expose it's implementation. E.g.
class A
{
public myMethod() {
doAMyMethod();
}
void doAMyMethod() {
// what A want's on doMyMethod, but now it's available
// as a separately identifiable method
}
}
class C extends B
{
public void someStuff() {
this.doAMyMethod();
}
}
The scheme separates the public interface (doMyMethod) from the implementation (doAMyMethod). Inheritance does depend upon implementation details, and so this isn't strictly so bad, but I would try to avoid it if possible as it creates a tight coupling between the implementation in A and C.
What I do for this case is:
Make the object of class A inside the Class C and access the Class A there. This Example which clarifies more details:
class A{
int a;
}
class B extends A{
int a;
}
class C extends B{
int a;
A obj = new A();
public void setData(int p,int q, int r) {
this.a = p; // points to it's own class
super.a = q;// points to one up level class i.e in this case class B
obj.a = r; // obj points the class A
}
public void getData() {
System.out.println("A class a: "+ obj.a);
System.out.println("B class a: "+ super.a);
System.out.println("C class a: "+this.a);
}
}
public class MultiLevelInheritanceSuper {
public static void main(String args[]){
C2 obj = new C2();
obj.setData(10, 20, 30);
obj.getData();
}
}
The output of this example is:
A class a: 30
B class a: 20
C class a: 10
精彩评论