There are three classes – Children, Father, and Ancestor. Children extends Father, and Father extends Ancestor, like below:
public class Ancestor {
public void test() {
}
}
public class Father extends Ancestor {
@Override
public void test() {
}
}
public class Children extends Father {
@Override
public void test() {
}
}
How I can use Ancestor’s test() method in Children’s test() method? I want to skip Father’s test() method.
You can’t. Java does not permit doing something like
super.super.method(). The reasons for this are outlined in this excellent answer, but the bottom line is that it violates encapsulation.If the functionality is really necessary, and it makes sense to do something like this, you can always add a method in your
Fatherclass that just calls thesuper.test()method, but doing things like this is usually bad practice. Unless you have some really good reasoning, rethink your code. There shouldn’t really be any necessity to call a method from eitherthisnorsuper.