Q:

Java find output programs (Inheritance) | set 3

belongs to collection: Java find output programs

0

Find the output of Java programs | Inheritance | Set 3: Enhance the knowledge of Java Inheritance concepts by solving and finding the output of some Java programs.

Question 1:

class Base {
  Base() {
    System.out.println("Base ctor called");
  }

  void Method() {
    System.out.println("Base.Method() called");
  }
}

class Derived extends Base {
  Derived() {
    System.out.println("Derived ctor called");
  }

  void Method() {
    base.Method();
    System.out.println("Derived.Method() called");
  }
}

public class InheritEx {
  public static void main(String[] args) {
    Derived D = new Derived();

    D.Method();
  }
}

Question 2:

class Base {
  Base() {
    System.out.println("Base ctor called");
  }

  void Method() {
    System.out.println("Base.Method() called");
  }
}

class Derived extends Base {
  Derived() {
    System.out.println("Derived ctor called");
  }

  void Method() {
    super.Method();
    System.out.println("Derived.Method() called");
  }
}

public class InheritEx {
  public static void main(String[] args) {
    Derived D = new Derived();

    D.Method();
  }
}

Question 3:

class Base {
  Base() {
    System.out.println("Base ctor called");
  }

  void Method() {
    System.out.println("Base.Method() called");
  }
}

class Derived extends Base {
  Derived() {
    System.out.println("Derived ctor called");
  }

  void Method() {
    super.Method();
    System.out.println("Derived.Method() called");
  }
}

public class InheritEx {
  public static void main(String[] args) {
    new Derived().Method();
  }
}

Question 4:

class Base {
  Base(int num) {
    System.out.println("Base ctor called: " + num);
  }

  void Method() {
    System.out.println("Base.Method() called");
  }
}

class Derived extends Base {
  Derived() {
    System.out.println("Derived ctor called");
  }

  void Method() {
    super.Method();
    System.out.println("Derived.Method() called");
  }
}

public class InheritEx {
  public static void main(String[] args) {
    new Derived().Method();
  }
}

Question 5:

class Base {
  Base(int num) {
    System.out.println("Base ctor called: " + num);
  }

  void Method() {
    System.out.println("Base.Method() called");
  }
}

class Derived extends Base {
  Derived() {
    super(100);
    System.out.println("Derived ctor called");
  }

  void Method() {
    System.out.println("Derived.Method() called");
  }
}

public class InheritEx {
  public static void main(String[] args) {
    new Derived().Method();
  }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Question 1:

Output:

/InheritEx.java:17: error: cannot find symbol
    base.Method();
    ^
  symbol:   variable base
  location: class Derived
1 error

Explanation:

The above program will generate syntax error because base keyword does not exist in Java.

Answer Question 2:

Output:

Base ctor called
Derived ctor called
Base.Method() called
Derived.Method() called

Explanation:

In the above program, we created 3 classes Base, Derived, and InheritEx. The Base class contains a constructor and a method Method(), and the Derived class also contains a constructor and a method Method().

Here, we created Method() in both Base and Derived class, Then we created an object of Derived class in the main() method of InheritEx class,

D.Method();

In the above statement Method() of Derived class will be called. Here, we called the Method() of Base class using super keyword.

Answer Question 3:

Output:

Base ctor called
Derived ctor called
Base.Method() called
Derived.Method() called

Explanation:

In the above program, we created 3 classes Base, Derived, and InheritEx. The Base class contains a constructor and a method Method(), and the Derived class also contains a constructor and a method Method().

Here, we inherited Base class into Derived class using the extends keyword.

Now look to the main() method of InheritEx class, here we created an anonymous object then the constructor of Base and Derived called and then Method() of Derived class called that will call the Method() of Base class.

Answer Question 4:

Output:

/InheritEx.java:12: error: constructor Base in class Base 
cannot be applied to given types;
  Derived() {
            ^
  required: int
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

Explanation:

The above program will syntax error because the Base class contains a parameterized constructor but Derived class contains no-argument constructor, and we know that when we create the constructor of derived class then it will call the constructor of Base class. But we cannot call parameterized constructor through no-argument constructor directly, here we need to use the super keyword to pass parameters to the Base class.

Answer Question 5:

Output:

Base ctor called: 100
Derived ctor called
Derived.Method() called

Explanation:

In the above program, we created 3 classes Base, Derived, and InheritEx. The Base class contains a parameterized constructor and a method Method(), and Derived class also contains a no-argument constructor and a method Method().

Here, we inherited Base class into Derived class using the extends keyword. We called the parameterized constructor of Base class from Derived class constructor using the super keyword.

Now look to the main() method of InheritEx class, here we created an anonymous object then the constructor of Base and Derived called and then Method() of Derived class called that will call the Method() of Base class.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java find output programs (Interface) | set 1... >>
<< Java find output programs (Inheritance) | set 2...