what is the result of the following piece of code:public class Person{ public void talk(){ System.out.print("I am a Person"); }}public class Student extends Person{ public void talk(){ System.out.print("I am a Student"); }}public class Test{ public static void main(String args[]){ Person p = new Student(); p.talk(); }}I am a Person I am a Student I am a Person I am a StudentI am a Student I am a Person
Question
what is the result of the following piece of code:public class Person{ public void talk(){ System.out.print("I am a Person"); }}public class Student extends Person{ public void talk(){ System.out.print("I am a Student"); }}public class Test{ public static void main(String args[]){ Person p = new Student(); p.talk(); }}I am a Person I am a Student I am a Person I am a StudentI am a Student I am a Person
Solution
The result of the code will be "I am a Student".
Here's the step by step explanation:
-
The
Personclass has a methodtalk()which prints "I am a Person". -
The
Studentclass extendsPersonand overrides thetalk()method to print "I am a Student". -
In the
mainmethod of theTestclass, aPersonreferencepis created but it is pointing to aStudentobject. -
When
p.talk()is called, becausepis actually aStudentobject, thetalk()method ofStudentclass is called, not thePersonclass. -
Therefore, "I am a Student" is printed.
Similar Questions
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 { @Override public String getInfo() { return "Student"; }}class Person { public String getInfo() { return "Person"; } public void printPerson() { System.out.println(getInfo()); }}A. Person PersonB. Person StudentC. Stduent StudentD. Student Person
Given:class FourWheeler { public FourWheeler () { System.out.print(1); } } class Car extends FourWheeler{ public Car() { System.out.print(2); } } class Audi extends Car{ public Audi() {System.out.print(3); } } public class Driver{ public static void main( String[] argv ) {new Audi(); } } What is the result when this code is executed?Select one:a.The code runs with no outputb.321c.123d.3
What is the result of the following code? 1: public abstract class Bird { 2: private void fly() { System.out.println("Bird is flying"); } 3: public static void main(String[] args) { 4: Bird bird = new Pelican(); 5: bird.fly(); 6: } 7: } 8: class Pelican extends Bird { 9: protected void fly() { System.out.println("Pelican is flying"); } 10: } *1 pointA. Bird is flyingB. Pelican is flyingC. The code will not compile because of line 4.D. The code will not compile because of line 5.E. The code will not compile because of line 9.
B A class A { String name="A"; public String getName() { return name; } String greeting() { return "class A"; }}class B extends A { String name="B"; String greeting() { return "class B"; } }public class Test { public static void main(String arg[]) { A a=new A(); A b=new B(); System.out.println(a.greeting()+" has name "+a.getName()); System.out.println(b.greeting()+" has name "+b.getName()); }} Place the names "A" and "B" in the following output. class has name class has name
Analyze the following code and choose the correct answer from below.public class Test { public static void main(String[] args) { new B(); } } class A { int i = 7; public A() { System.out.println("i from A is " + i); } public void setI(int i) { this.i = 2 * i; } } class B extends A { public B() { setI(20); // System.out.println("i from B is " + i); } @Override public void setI(int i) { this.i = 3 * i; } }
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.