Java Questions 41 - 50  «Prev  Next»

Java Constructor Characteristics(Questions)

  1. Are Java constructors inherited?

    Answer:
    No. A subclass inherits all the members (fields, methods, and nested classes) from its superclass.
    Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

  2. Do constructors have return types?

    Answer:
    No. Only methods have return types.

  3. How many constructors will be invoked in an object's inheritance tree?

    Answer:
    All of the constructors in an object's inheritance tree will be invoked when the object is instantiated using new.


  4. How can a constructor invoke another constructor of the same class?

    Answer:
    A constructor can invoke another constructor of the same class using the this keyword .
    In Java, the `this()` keyword can be used within a constructor to call another constructor of the same class. This is useful for constructor chaining, where one constructor can invoke another to reduce redundancy. Here’s an example using a parent class `Animal` and a subclass `Mammal`, where the `this()` keyword is used in the `Mammal` class to invoke another constructor of the same class.

    Example:
    // Parent class
    class Animal {
       String name;
       int age;
    
       // Constructor 1
       public Animal(String name) {
           this.name = name;
           System.out.println("Animal constructor with name: " + name);
       }
    
       // Constructor 2
       public Animal(String name, int age) {
           this(name);  // Calls the other constructor of the same class
           this.age = age;
           System.out.println("Animal constructor with name and age: " + name + ", " + age);
       }
    }
    
    // Subclass
    class Mammal extends Animal {
       String type;
    
       // Constructor 1
       public Mammal(String name, int age, String type) {
           super(name, age);  // Calls the parent class constructor
           this.type = type;
           System.out.println("Mammal constructor with type: " + type);
       }
    
       // Constructor 2
       public Mammal(String name, String type) {
           this(name, 0, type);  // Calls the other constructor of the same class
           System.out.println("Mammal constructor with default age");
       }
    }
    
    public class Main {
       public static void main(String[] args) {
           // Creating a Mammal object using the second constructor
           Mammal mammal = new Mammal("Lion", "Carnivore");
           // Creating a Mammal object using the first constructor
           Mammal mammal2 = new Mammal("Elephant", 10, "Herbivore");
       }
    }
    

    Output:
    Animal constructor with name: Lion
    Animal constructor with name and age: Lion, 0
    Mammal constructor with type: Carnivore
    Mammal constructor with default age
    Animal constructor with name: Elephant
    Animal constructor with name and age: Elephant, 10
    Mammal constructor with type: Herbivore
    

    Explanation:
    1. Parent Class (`Animal`):
      • The `Animal` class has two constructors:
        • One that accepts a `name`.
        • Another that accepts both `name` and `age`, and it invokes the first constructor using `this(name)`.
    2. Subclass (`Mammal`):
      • The `Mammal` class has two constructors:
        • One that accepts a `name`, `age`, and `type`, and it invokes the parent class's constructor using `super(name, age)`.
        • Another that accepts a `name` and `type` but invokes the other constructor in the same class using `this(name, 0, type)`.

    In this way, constructor chaining is achieved both in the parent class (`Animal`) and the subclass (`Mammal`).

  5. What must every constructor have in the first line of its body?

    Answer:
    Every constructor must have
    1. this or
    2. super
    as the first statement (although the compiler can insert this for you).
    Have both 1) this and 2) super in the first line of a constructor will cause a compiler error.

  6. What is characteristic of class variables?

    Answer:
    Static members are tied to a class and not to an instance.

  7. How do you access a static member?

    Answer:
    Use the class name with the dot operator.


  8. How is the IS-A relationship expressed?

    Answer:
    The IS-A relationship is expressed with the keyword extends.

  9. What is true about reference variables in Java?

    Answer:
    A reference variable is always of a single unchangeable type, but it can refer to a subtype object.

  10. Can constructors be overridden in Java?

    Answer:
    No.Constructors can be overloaded but not overridden because constructors are not inherited.
    Notes: 1. A reference variable can refer to a subtype object.