Java find output programs (Inheritance) | set 3

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.
Submitted by Nidhi, on February 03, 2021

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();
  }
}

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.

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();
  }
}

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.

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();
  }
}

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.

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();
  }
}

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.

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();
  }
}

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.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.