Java find output programs (Inheritance) | set 1

Find the output of Java programs | Inheritance | Set 1: 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 {
  public Base() {
    System.out.println("Base ctor called");
  }
  public void Method1() {
    System.out.println("Method1() called");
  }
}

class Derived: Base {
  public Derived() {
    System.out.println("Derived ctor called");
  }
  public void Method2() {
    System.out.println("Method2() called");
  }
}

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

    D.Method1();
    D.Method2();
  }
}

Output:

/InheritEx.java:10: error: '{' expected
class Derived: Base {
             ^
1 error

Explanation:

The above program will generate syntax error because we cannot use a colon ":" operator for an inheritance, we need to use extends keyword for inheritance in Java.

Question 2:

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

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

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

    D.Method1();
    D.Method2();
  }
}

Output:

Base ctor called
Derived ctor called
Method1() called
Method2() called

Explanation:

In the above program, we created three classes Base, Derived, and InheritEx. The Base class contains a constructor and a method Method1(). The Derived class contains a constructor and a method Method2.

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

Now look to the main() method of InheritEx class. Here, we created the object of Derived class then here constructor of Base class will be called before calling the constructor of Derived class, and then Method1() and Method2() will be called respectively.

Question 3:

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

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

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

    D.Method1();
    D.Method2();
  }
}

Output:

Base ctor called
Derived ctor called
Method1() called
Method2() called

Explanation:

In the above program, we created three classes Base, Derived, and InheritEx. The Base class contains a constructor and a method Method1(). Here, we defined both constructor and Method1() with a protected modifier.

The Derived class contains a constructor and a method Method2.Here, we inherited Base class into Derived class using extends keyword.

Now look to the main() method of the InheritEx class - Here, we created the object of Derived class then here constructor of Base class will be called before calling the constructor of Derived class, and then Method1() and Method2() will be called respectively.

Question 4:

class Base {
  Base() {
    System.out.println("Base ctor called");
  }
  ~Base() {
    System.out.println("Base dtor called");
  }
  void Method1() {
    System.out.println("Method1() called");
  }
}

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

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

    D.Method1();
    D.Method2();
  }
}

Output:

/InheritEx.java:5: error: illegal start of type
  ~Base() {
  ^
/InheritEx.java:17: error: illegal start of type
  ~Derived() {
  ^
2 errors

Explanation:

The above program will generate syntax errors. Here, we defined destructor in Base and Derived class. But Java does not support destructors.

Question 5:

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

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

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

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

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

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

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

    D.Method1();
    D.Method3();
  }
}

Output:

/InheritEx.java:12: error: Base() has private access in Base
  Derived1() {
             ^
/InheritEx.java:22: error: Base() has private access in Base
  Derived2() {
             ^
2 errors

Explanation:

The above program will generate compile-time errors because we defined a private constructor in the Base class that cannot be accessed in InheritEx class because we created the object of Derived2 class in the InheritEx class.





Comments and Discussions!

Load comments ↻





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