// What will happen when you attempt to compile and run the following code?
class
Base
... {
int i = 99;
public void amethod()
...{
System.out.println("Base.amethod()");
}
Base()
...{
amethod();
}
}
public class Derived extends Base
... {
int i = -1;
public static void main(String argv[])
...{
Base b = new Derived();
System.out.println(b.i);
b.amethod();
}
public void amethod()
...{
System.out.println("Derived.amethod()");
}
}
... {
int i = 99;
public void amethod()
...{
System.out.println("Base.amethod()");
}
Base()
...{
amethod();
}
}
public class Derived extends Base
... {
int i = -1;
public static void main(String argv[])
...{
Base b = new Derived();
System.out.println(b.i);
b.amethod();
}
public void amethod()
...{
System.out.println("Derived.amethod()");
}
}
answer:
Derived.amethod() |
99 |
Derived.amethod() |
The reason is that this code creates an instance of the Derived class but assigns it to a reference of a the Base class. In this situation a reference to any of the fields such as i will refer to the value in the Base class, but a call to a method will refer to the method in the class type rather than its reference handle. But note that if the amethod() was not present in the base class, then compilation error would be reported as at compile time. When the compiler sees the statement like b.amethod(), it checks if the method is present in the base class or not. Only at the run time it decides to call the method from the derived class.
//
Read the code below. Will be the result of attempting to compile and run the code below.
public class AQuestion ... {
public void method(Object o)...{
System.out.println("Object Verion");
}
public void method(String s)...{
System.out.println("String Version");
}
public static void main(String args[])...{
AQuestion question = new AQuestion();
question.method(null);
}
}
//The code compiles cleanly and shows "String Version"
public class AQuestion ... {
public void method(Object o)...{
System.out.println("Object Verion");
}
public void method(String s)...{
System.out.println("String Version");
}
public static void main(String args[])...{
AQuestion question = new AQuestion();
question.method(null);
}
}
//The code compiles cleanly and shows "String Version"
Read the code below. Will be the result of attempting to compile and run the code below.
public
class
AQuestion
...
{
public void method(StringBuffer sb)...{
System.out.println("StringBuffer Verion");
}
public void method(String s)...{
System.out.println("String Version");
}
public static void main(String args[])...{
AQuestion question = new AQuestion();
question.method(null);
}
}
public void method(StringBuffer sb)...{
System.out.println("StringBuffer Verion");
}
public void method(String s)...{
System.out.println("String Version");
}
public static void main(String args[])...{
AQuestion question = new AQuestion();
question.method(null);
}
}
The code throws an Exception at Runtime.
public interface AQuestion{ public abstract void someMethod() throws Exception; }
A Class implementing this interface should
1. Necessarily be an abstract class.
2. Should have the method public abstract void someMethod();
3. Should have the method public void someMethod() which has to throw an exception which is a subclass of java.lang.Exception.
4. Should have the method public void someMethod() which need not throw an Exception.
[ 4 is right ]
public
class
AQuestion
...
{
private int i = j;
private int j = 10;
public static void main(String args[])...{
System.out.println((new AQuestion()).i);
}
}
private int i = j;
private int j = 10;
public static void main(String args[])...{
System.out.println((new AQuestion()).i);
}
}
2. Compiler error complaining about forward referencing.
3. No error - The output is 0;
4. No error - The output is 10;