71.7k views
4 votes
What is the output of the following code? public class Test { public static void main(String[] args) { new Person().printPerson(); new Student().printPerson(); } } class Student extends Person { private String getInfo() { return "Student"; } } class Person { private String getInfo() { return "Person"; } public void printPerson() { System.out.println(getInfo()); } }

User BlessedKey
by
5.1k points

1 Answer

3 votes

Answer:

The output of the given question is :

Person

Person

Step-by-step explanation:

Following are the description of Statements

  • The new Person().printPerson(); calling the function of printPerson() of the class Person so it returns the Person in the console window.
  • The new Student().printPerson(); calling the function of printPerson() of the class student .The student inherits the class Person So control will moves to the class it executed the statement inside the Person class So it executed the function public void printPerson() of the Person class so it also returns the Person String and execution of program become stops
User TrueCamelType
by
4.4k points