Menu
Topics Index
...
`

creating Interfaces and its Implementation


If you need explanation Read this topic

If you need Answer Take test on this topic

creating Interfaces and its Implementation
What will be the output of the following program?
class I1 {
    public static void main(String[] args) {
        A a = new A();
        a.m1();
        a.m4();
        B.class.m2();
    }
}
interface I {
    void m1();
    void m4();
}
interface I2 {
    void m2();
}
class A implements I {
    public void m1() {    System.out.print("1");    }
    public void m4() {    System.out.print("2");    }
}
abstract class B implements I2 {
    public void m1() {    }
    public void m2() {    System.out.print("3");    }
    void m3() {    }
}

A. 12
B. 123
C. 12123
D. Some other output
E. Compilation Error or Runtime Error
Topic: Future Task Java Program Using Interfaces

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

c

Posted by Sahithi Boggarapu    2014-07-22 08:51:14


Please tell me what B. class.m2() does

Posted by Mohd Naveed    2014-07-22 09:01:09


ans e. compilation err.
B.class.m2();
tis line throws err.
m2() method undefined for the class type <B>

Posted by Maheshwari Natarajan    2014-07-22 09:10:04


Ans is B..Firstly, m1 and m4 methods are called using object a and then the method m2 is called by class b which outputs 3..So the output is 123

Posted by Badam Swathi    2014-07-22 09:20:28


E: compilation error
1. B is an abstract class you cannot create an instance of an abstract class
2. B.class will return class object of the Class i.e. Class<B>
u cannot call method directly on class object. (syntax is incorrect)
3. valid syntax is something like this
Class<A> a = A.class;
A o = a.newInstance();
o.m1(); //or simply A.class.newInstance().m1();
o.m4();
but since B is an abstract class .newInstance() will throw an exception
4. only possible way to call m2 is define a class that extends B and defines all its methods (override them) create instance of that class and then call method m2 on the instance

Posted by Tushar Ghude    2014-07-22 14:46:05


Congratulations Maheshwari Natarajan. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-07-23 04:16:10


© meritcampus 2019

All Rights Reserved.

Open In App