Knowee
Questions
Features
Study Tools

7.Cho biết kết quả hiển thị của đoạn mã sau:class Run1 {void display(){System.out.println("Run1");}}class Run2 extends Run1{@Overridevoid display(){System.out.println("Run2");}}class test {public static void main(String[] args) {Run1 run = new Run1();run.display();}}doneRun1Run2Run1Run2Compilation fails

Question

7.Cho biết kết quả hiển thị của đoạn mã sau:class Run1 {void display(){System.out.println("Run1");}}class Run2 extends Run1{@Overridevoid display(){System.out.println("Run2");}}class test {public static void main(String[] args) {Run1 run = new Run1();run.display();}}doneRun1Run2Run1Run2Compilation fails

🧐 Not the exact question you are looking for?Go ask a question

Solution

Kết quả hiển thị của đoạn mã trên sẽ là "Run1".

Giải thích: Trong đoạn mã trên, chúng ta có 3 class: Run1, Run2 và test.

  • Class Run1 có phương thức display() in ra "Run1".
  • Class Run2 kế thừa từ Run1 và ghi đè phương thức display() để in ra "Run2".
  • Class test có phương thức main(), nơi chúng ta tạo một đối tượng của Run1 và gọi phương thức display().

Vì đối tượng được tạo là của Run1, nên khi gọi phương thức display(), nó sẽ gọi phương thức từ class Run1, không phải từ Run2. Do đó, kết quả in ra sẽ là "Run1".

This problem has been solved

Similar Questions

3.Chọn đáp án đúng.class Run1 {void display(){ System.out.println("Run1");}}class Run2 extends Run1{void display2(){ System.out.println("display2");}}public class test {public static void main(String[] args) { Run2 run = new Run2(); run.display();}}doneRun1Run2Run1Run2Lỗi biên dịch

Cho biết kết quả hiển thị của đoạn mã sau:class TestA{public void start(){System.out.println("TestA");}}class TestB extends TestA{public void start(){System.out.println("TestB");}public static void main(String[] args){((TestA)new TestB()).start();}}doneTestATestBLỗi biên dịchLỗi thực thi

the correct answerWhat will be the output of the given Java program? abstract class java {    int x;    abstract void display(); }     class method extends java {     int y;     void display() {        System.out.println(y);     } }     class Abstract_demo {     public static void main(String args[]) {         method obj = new method();         obj.y = 1;         obj.display();         } }Options0Runtime Error1Compilation Error

Select the correct answerWhat is the output of the following program?public class Test extends Thread implements Runnable{  public void run()  {    System.out.printf("CT ");  }  public static void main(String[] args) throws InterruptedException  {    Test obj = new Test();    obj.run();    obj.start();  }}OptionsRuntime errorCompilation errorCT CTNone of the above

Find the output: abstract class One { int i; abstract void display(); } class Two extends One { int j; void display() { System.out.println(j); } } class AbstractDriver { public static void main(String args[]) { Two obj = new Two(); obj.j=2; obj.display(); } }

1/2

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.