Set - 8

Question 1 :

The superclass constructor runs before the subclass constructor. The subclass's version of the overridable method will be invoked before the subclass's constructor has been invoked. If the subclass's overridable method depends on the proper initialization of the subclass (through the subclass constructor), the method will most likely fail. Is that true?

Answer :

Yes. It is true

Question 2 :

Why are the interfaces more flexible than abstract classes?  

Answer :

An interface-defined type can be implemented by any class in a class hierarchy and can be extended by another interface. In contrast, an abstract-class-defined type can be implemented only by classes that subclass the abstract class.
An interface-defined type can be used well in polymorphism. The so-called interface type vs. implementation types.
Abstract classes evolve more easily than interfaces. If you add a new concrete method to an abstract class, the hierarchy system is still working. If you add a method to an interface, the classes that rely on the interface will break when recompiled.
Generally, use interfaces for flexibility; use abstract classes for ease of evolution (like expanding class functionality).

Question 3 :

What are new language features in J2SE 5.0?

Answer :

1. generics
2. static imports
3. annotations
4. typesafe enums
5. enhanced for loop
6. autoboxing/unboxing
7. varargs
8. covariant return types

Question 4 :

What is covariant return type?

Answer :

A covariant return type lets you override a superclass method with a return type that subtypes the superclass method's return type. So we can use covariant return types to minimize upcasting and downcasting.

class Parent {
  Parent foo () {
    System.out.println ("Parent foo() called");
    return this;

class Child extends Parent {
  Child foo () {
   System.out.println ("Child foo() called");
   return this;

class Covariant {
  public static void main(String[] args) {
    Child c = new Child();
    Child c2 =; // c2 is Child
    Parent c3 =; // c3 points to Child

Question 5 :

What is the result of the following statement?

Answer :

int i = 1, float f = 2.0f;
i += f; //ok, the cast done automatically by the compiler
i = i + f; //error

The compound assignment operators automatically include cast operations in their behaviors.