Followers

Saturday, 29 September 2012

Overloading


Question 1

 
class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D extends C {
  void m1(D d) {System.out.print("D");}
  public static void main(String[] args) {
    A a1 = new A(); B b1 = new B(); C c1 = new C(); D d1 = new D();
    d1.m1(a1); d1.m1(b1); d1.m1(c1);
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: DDD
d.  Prints: ABCD
e.   Compile-time error
f.   Run-time error
g.  None of the above

Question 2

class A {}  class B extends A {}  class C extends B {}
class D {
  void m1(A a) {System.out.print("A");}
  void m1(B b) {System.out.print("B");}
  void m1(C c) {System.out.print("C");}
  public static void main(String[] args) {
    A c1 = new C(); B c2 = new C(); C c3 = new C(); D d1 = new D();
    d1.m1(c1); d1.m1(c2); d1.m1(c3);
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: CCC
d.  Compile-time error
e.   Run-time error
f.   None of the above

Question 3

class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
  public static void main(String[] args) {
    A c1 = new C(); B c2 = new C(); C c3 = new C(); C c4 = new C();
    c4.m1(c1); c4.m1(c2); c4.m1(c3);
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: CCC
d.  Compile-time error
e.   Run-time error
f.   None of the above

Question 4

class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
  public static void main(String[] args) {
    A c1 = new C(); C c2 = new C(); c1.m1(c2);
}}
What is the result of attempting to compile and run the program?
a.   Prints: A
b.  Prints: B
c.   Prints: C
d.  Compile-time error
e.   Run-time error
f.   None of the above

Question 5

class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
  public static void main(String[] args) {
    A a1 = new A(); A b1 = new B(); A c1 = new C(); C c4 = new C();
    a1.m1(c4); b1.m1(c4); c1.m1(c4);
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: CCC
d.  Compile-time error
e.   Run-time error
f.   None of the above

Question 6

class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
  public static void main(String[] args) {
    A a1 = new A(); B b1 = new B(); C c1 = new C(); A c2 = new C();
    c2.m1(a1); c2.m1(b1); c2.m1(c1);
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: CCC
d.  Compile-time error
e.   Run-time error
f.   None of the above

Question 7

class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
  public static void main(String[] args) {
    A a1 = new A(); B b1 = new A(); C c1 = new A(); C c2 = new C();
    c2.m1(a1); c2.m1(b1); c2.m1(c1);
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: CCC
d.  Compile-time error
e.   Run-time error
f.   None of the above

Question 8

class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
  public static void main(String[] args) {
    A a1 = new A(); B b1 = new B(); C c1 = new C(); C c2 = new A();
    c2.m1(a1); c2.m1(b1); c2.m1(c1);
}}
What is the result of attempting to compile and run the program?
a.   Prints: AAA
b.  Prints: ABC
c.   Prints: CCC
d.  Compile-time error
e.   Run-time error
f.   None of the above



                            Answers

No.
Answer
Remark
1
Prints: ABC 
The method invocation expression d1.m1(a1) uses reference d1 of type D to invoke method m1. Since the reference d1 is of type D, the class D is searched for an applicable implementation of m1. The methods inherited from the superclasses, C, B and A, are included in the search. The argument, a1, is a variable declared with the type A; so method A.m1(A a) is invoked.  
2
Prints: ABC 
Three methods overload the method name m1. Each has a single parameter of type A or B or C. For any method invocation expression of the form m1(referenceArgument), the method is selected based on the declared type of the variable referenceArgument--not the run-time type of the referenced object. The method invocation expression d1.m1(c1) uses reference d1 of type D to invoke method m1 on an instance of type D. The argument, c1, is a reference of type A and the run-time type of the referenced object is C. The argument type is determined by the declared type of the reference variable c1--not the run-time type of the object referenced by c1. The declared type of c1 is type A; so the method m1(A a) is selected. The declared type of c2 is type B; so the method invocation expression d1.m1(c2) invokes method m1(B b). The declared type of c3 is type C; so the method invocation expression d1.m1(c3) invokes method m1(C c).  
3
Prints: ABC 
Three methods overload the method name m1. Each has a single parameter of type A or B or C. For any method invocation expression of the form m1(referenceArgument), the method is selected based on the declared type of the variable referenceArgument--not the run-time type of the referenced object. The method invocation expression c4.m1(c1) uses reference c4 of type C to invoke method m1 on an instance of type C. The argument, c1, is a reference of type A and the run-time type of the referenced object is C. The argument type is determined by the declared type of the reference variable c1--not the run-time type of the object referenced by c1. The declared type of c1 is type A; so the method A.m1(A a) is selected. The declared type of c2 is type B; so the method invocation expression c4.m1(c2) invokes method B.m1(B b). The declared type of c3 is type C; so the method invocation expression c4.m1(c3) invokes method C.m1(C c).  
4
Prints: A 
The reference c1 is of the superclass type, A; so it can be used to invoke only the method m1 declared in class A. The methods that overload the method name m1 in the subclasses, B and C, can not be invoked using the reference c1. A method invocation conversion promotes the argument referenced by c2 from type C to type A, and the method declared in class A is executed. Class A declares only one method, m1. The single parameter is of type A. Class B inherits the method declared in class A and overloads the method name with a new method that has a single parameter of type B. Both methods sharing the overloaded name, m1, can be invoked using a reference of type B; however, a reference of type A can be used to invoke only the method declared in class A. Class C inherits the methods declared in classes A and B and overloads the method name with a new method that has a single parameter of type C. All three methods sharing the overloaded name, m1, can be invoked using a reference of type C; however, a reference of type B can be used to invoke only the method declared in class B and the method declared in the superclass A. The method invocation expression c1.m1(c2) uses reference c1 of type A to invoke method m1. Since the reference c1 is of type A, the search for an applicable implementation of m1 is limited to class A. The subclasses, B and C, will not be searched; so the overloading methods declared in the subclasses can not be invoked using a reference of the superclass type.  
5
Prints: AAA 
The declared type of the reference variables, a1, b1 and c1, is the superclass type, A; so the three reference variables can be used to invoke only the method m1(A a) that is declared in the superclass, A. The methods that overload the method name m1 in the subclasses, B and C, can not be invoked using a reference variable of the superclass type, A. A method invocation conversion promotes the argument referenced by c4 from type C to type A, and the method declared in class A is executed.  
6
Prints: AAA 
The reference c2 is of the superclass type, A; so it can be used to invoke only the method, m1, declared in class A. The methods that overload the method name m1 in the subclasses, B and C, can not be invoked using the reference c2.  
7
Compile-time error 
The declarations of b1 and c1 cause compile-time errors, because a reference of a subclass type can not refer to an instance of the superclass type.  
8
Compile-time error 
The declaration of c2 causes a compile-time error, because a reference of a subclass type can not refer to an instance of the superclass class.  




No comments:

Post a Comment