Constructor Inheritance and Overriding - rahul00773/JavaConcepts GitHub Wiki

Constructor inheritance and overriding concepts are not applicable. But Overloading concept is applicable.

Every class in java including abstract class can contain constructor. But interface can not contain constructors.

class Test{

Test(){ }

}

abstract class Test{

Test(){ } }

interface Test{ Test(){ Not applicable }

}

Case1:

Recursive method call is a run time exception saying StackOverflow error. But In our program if there is chance of recursive constructor invocation then the code won’t compile and we will get compile time error.

class Test{

public static void m1(){ m2(); }

public static void m2(){ m1(); }

public static void main(String[] args){. /// run time exception saying StackOverflow error.

m1();

System.out.println(“hello”);

}

}

class Test{

Test(){

this(10);

}

Test(int 1){

this();

}

public static void main(String[] args){. /// Compile time error:Recursive constructor invocation

System.out.println(“hello”);

} }

Case2:

// Valid class P{

}

class C extends P{

}

// Valid class P{ P(){

}

}

class C extends P{

}

// Invalid : Compile time error : Can not find symbol : Symbol: constructor P(), Location: class P class P{ P(int i){

}

}

class C extends P{

}

Note:

  1. If parent class contains any argument constructor then while writing child classes we have to take special care with respect to constructors.
  2. Whenever we are writing any argument constructor it is highly recommended to write no args constructor also.

Case3

// Compile Time Error: Unreported exception java.io.IOException in default constructor

class P{

P() throws IOException{

}

class C extends P{

} }

class P{

P() throws IOException{

}

class C extends P{

C() throws IOException{

super(); }

} }

If parent class throws any checked exception compulsory child class constructor should throw the same checked exception or its parent.Otherwise the code won’t compile.

Which of the following statements are valid:

  1. The main purpose of constructor is to create an object (false)
  2. The main purpose of constructor is to perform initialisation of an object (true)
  3. The name of the constructor need not be same as class Name (false)
  4. Written type concept applicable for constructors but only void (False)
  5. We can apply any modified for constructor. (False)
  6. Default constructor generated by JVM( False)
  7. Compile is responsible to generate default constructor(True)
  8. Compile will always generate default constructor (False)
  9. If we are not writing any arg constructor then compile will generate default constructor (False)
  10. Every no-argument constructor is always default constructor(false)
  11. A default constructor is always no-arg constructor (true)
  12. The first line inside every constructor either super() or this().(true)
  13. If we are not writing anything then the compiler will generate this(); (false)
  14. For constructor for overloading and overriding concepts are applicable(false)
  15. For constructor inheritance applicable. But not overriding(false)
  16. Only concrete classes can contain constructors but abstract class can not (False)
  17. Interface can contains constructors (False)
  18. Recursive constructor invocation is a run time excception (False)
  19. If parent class constructor throws checked exception then compulsory child class constructor should through the same checked exception or its child(False)