Does the following program compile without errors? If so proceed further. If not, delete as little as possible (the smallest sub-expression) to get an error-free compile before proceeding further.
Now determine the output of the following Java program.
class Parent { static int sv = 4; int iv = 8; static int SF() { return sv; } int IF() { return iv; } } class Child extends Parent { static int sv = 3; int iv = 6; static int SF() { return sv; } int IF() { return iv; } int GG() { return super.iv; } } class Finals { public static void main (String [] args) { Child c = new Child(); Parent p = c; System.out.println(""); System.out.println( " p.SF() = " + p.SF() + " \t c.SF() = " + c.SF() ); System.out.println( " p.IF() = " + p.IF() + " \t c.IF() = " + c.IF() + "\n" ); System.out.println( " ( (Parent) c).iv = " + ((Parent) c).iv + "\n" + " ( (Parent) c).IF() = " + ((Parent) c).IF() + "\n" + " c.GG() = " + c.GG() + "\n" ); System.out.println( " ( (Child) p).iv = " + ((Child) p).iv + "\n" + " ( (Child) p).SF() = " + ((Child) p).SF() + "\n" + " p.GG() = " + p.GG() + "\n" ); } }
Java method header declaration syntax is given in Section 8.4 (Pages 167-187) of the JLS book. Informally, the method header consists of modifiers, followed by type/void, followed by identifier, followed by typed formal parameter list, followed by throws-clause. Explain clearly all the restrictions that must be satisfied by a legal overload of a method in a class.
Comment on the validity of each of the following statements supplying adequate justification.
(a) A private instance method of a class is not accessible in a subclass, and hence cannot be overridden.
(b) Two threads running synchronized instance methods on two separate instances of a class are guaranteed to access static fields of the class object in a mutually exclusive fashion.
(c) The Java Language Spec guarantees that every Java program will generate identical results when run on different platforms.
Explain clearly the conditions under which a Java run-time system is not permitted to immediately recycle memory that is unreachable from the roots (reference variables on run-time stack, registers, static data structures, etc) of the program, to avoid dangling references.