I made an abstract class and three subclasses of that class.When I override toString method, I want to return the values of variable of subclass(from toString method) and not the superclass.But it gives the value of superclass.What to do?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I made an abstract class and three subclasses of that class.When I override toString method, I want to return the values of variable of subclass(from toString method) and not the superclass.But it gives the value of superclass.What to do??

30th Aug 2016, 5:47 PM
Raj Joshi
Raj Joshi - avatar
2 Answers
+ 1
it may be becouse you are using an instance of super-class not sub-class. Try the below Ex: abstract class Father {} class Son extends Father { public String toString() { return "I am the Son class"; } } public class Program { public static void main(String[] args) { Father f = new Son (); System.out.println (f); } }
31st Aug 2016, 3:36 AM
Tiger
Tiger - avatar
0
ohkk thanxx👍👍
31st Aug 2016, 9:24 AM
Raj Joshi
Raj Joshi - avatar