Question: Consider the following class definitions. publ ic class A { publ ic St r ing message ( int i ) { return A

Consider the following class definitions.
publ ic class A
{
publ ic St r ing message( int i )
{
return "A"+ i ;
}
}
publ ic class B extends A
{
publ ic St r ing message( int i )
{
return "B"+ i ;
}
}
The following code segment appears in a class other than A or B.
A obj1= new B() ; // Line 1
B obj2= new B() ; // Line 2
System.out .pr int ln(obj1.message(3)) ; // Line 3
System.out .pr int ln(obj2.message(2)) ; // Line 4
Which of the following best explains the difference, if any, in the behavior of the code segment that will result
from removing the message method from class A ?
(A) The statement in line 3 will cause a compiler error because the message method for obj1 cannot
be found.
(B) The statement in line 4 will cause a compiler error because the message method for obj2 cannot
be found.
(C) As a result of the method call in line 3, the message method in class B will be executed instead of
the message method in class A.
(D) As a result of the method call in line 4, the message method in class B will be executed instead of
the message method in class A.
(E) The behavior of the code segment will remain unchanged

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!