public class override {
public static void main(String[] args) {
c1 obj = new c1();
System.out.println(obj);
}
}
class a1 {
public String toString (){
return "this is clas a";
}
}
class b1 extends a1{
public String toString (){
return "this is clas b";
}
}
class c1 extends b1{
public String toString (){
return super.toString() + "\nthis is clas c";
}
}
I need to access the superclass a1
toString
method in c1
subclass. Is there any way to do it. I'm learning java, any help would be a great support.
You would probably like to have something as super.super.toString()
. But this is not allowed in java. So you can simply use it twice as bellow :
public class override {
public static void main(String[] args) {
c1 obj = new c1();
System.out.println(obj);
}
}
class a1 {
public String toString (){
return "this is clas a";
}
}
class b1 extends a1{
public String toString (){
return "this is clas b";
}
public String superToString(){
return super.toString();
}
}
class c1 extends b1{
public String toString (){
return super.superToString() + "\nthis is clas c";
}
}
This Question may also help.