Look at two classes below. Class B extends class A and overrides “toString()” me
ID: 3639175 • Letter: L
Question
Look at two classes below. Class B extends class A and overrides “toString()” method.If you have instance of class B, how can you call “toString()” method of class A?
public class A {
public A() {}
public String toString() {
return "Class A";
}
}
public class B extends A {
public B() { super(); }
public String toString() {
return "Class B";
}
}
public static void main(String[] args) {
B objectB = new B();
String stringB = objectB.toString();
// Here find a way to call toString() method of the superclass of objectB
String stringA = ...
}
Explanation / Answer
class A { public String toString() { return "A toString()"; } } class B extends A { public String toString() { return "B toString()"; } public String superString() { return super.toString(); } } public class Test { public static void main(String[] args) { A a = new B(); System.out.println(a.toString()); System.out.println(a.superString()); } }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.