Consider the following case of inheritance:
public class Parent {
public Parent() {
getValue();
}
public void getValue() {
}
}
public class Child extends Parent {
private final Integer integer;
public Child() {
integer = new Integer(888);
}
@Override
public void getValue() {
System.out.println(integer);
}
}
Question: What would the following program print, why?
public class Test {
public static void main(String[] args) {
Child child = new Child();
child.getValue();
}
}
Lets assume that getValue() implementation in Child class was changed to:
@Override
public void getValue() {
System.out.println(integer.toString());
}
Question: What would the output of the Test class be now, why?
GD Star Rating
loading...
loading...
Related posts:
- Brainteaser: Broken Comparator
Question: The following program returns result “1″, which indicates that first Integer value is greater than the second, why? import java.util.*; public class... - Brainteaser: Hidden Iterators
… While locking can prevent iterators from throwing ConcurrentMofdificationException, You have to remember to use locking everywhere a shared collection might be iterated.... - Brainteaser: Broken Case of Inheritance
Consider the following case of inheritance: public class ExtendingHashSet<E> extends HashSet<E> { private int counter = 0; public ExtendingHashSet() { } @Override public... - Singleton Pattern and Problem With Double Checked Locking
There are several ways to initialize singleton object. Some are thread safe and some are not. Until the last two days I thought... - Brainteaser: ArrayList VS TreeSet
When I came across the following example I did not expect the results that the program has printed hehe… Question: What does this...
