Print Super Class Fields With Child Instance Fields

Print Super Class Fields With Child Instance Fields

If you want to print all value of child and parent class variables then you will have to override toString() method from object class which prints object values but if you have a class where child parent relationship exists then it will print only child class variable so to include parent class variable as well you will have to tweak method toString(). Please see example below:

  • Parent class: Employer.java:

  • Child class: Employee.java (Please have a look toString() method of this class where we are calling super.toString() to print parent class object value:

  • Test class PrintChidParentObjectData.java to print object value:

  • Output:

Print Super Class Fields With Child Instance Fields

Leave a Reply

Your email address will not be published. Required fields are marked *