What is the result?

Given:

public class SampleClass {
public static void main(String[] args) {
SampleClass sc, scA, scB;
sc = new SampleClass();
scA = new SampleClassA();
scB = new SampleClassB();
System.out.println(“Hash is : ” +
sc.getHash() + “, ” + scA.getHash() + “, ” + scB.getHash());
}
public int getHash() {
return 111111;
}
}
class SampleClassA extends SampleClass {
public long getHash() {
return 44444444;
}
}
class SampleClassB extends SampleClass {
public long getHash() {
return 999999999;
}
}
What is the result?

Given:

public class SampleClass {
public static void main(String[] args) {
SampleClass sc, scA, scB;
sc = new SampleClass();
scA = new SampleClassA();
scB = new SampleClassB();
System.out.println(“Hash is : ” +
sc.getHash() + “, ” + scA.getHash() + “, ” + scB.getHash());
}
public int getHash() {
return 111111;
}
}
class SampleClassA extends SampleClass {
public long getHash() {
return 44444444;
}
}
class SampleClassB extends SampleClass {
public long getHash() {
return 999999999;
}
}
What is the result?

A.
Compilation fails

B.
An exception is thrown at runtime

C.
There is no result because this is not correct way to determine the hash code

D.
Hash is: 111111, 44444444, 999999999

Explanation:
The compilation fails as SampleClassA and SampleClassB cannot override
SampleClass because the return type of SampleClass is int, while the return type of
SampleClassA and SampleClassB is long.
Note: If all three classes had the same return type the output would be:
Hash is : 111111, 44444444, 999999999



Leave a Reply 0

Your email address will not be published. Required fields are marked *