Java find output programs (Inheritance) | set 2

Find the output of Java programs | Inheritance | Set 2: 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 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:

Base ctor called
Derived2 ctor called
Method1() called
Method3() called

Explanation:

In the above program, we created Four classes Base, Derived1, Derived2, and InheritEx. The Base class contains a constructor and a method Method1().

Here, we inherited Base class into Derived1 and Derived2.

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

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

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

Question 2:

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

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

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

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

class Derived2 extends Derived1 {
  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:

Base ctor called
Derived1 ctor called
Derived2 ctor called
Method1() called
Method3() called

Explanation:

In the above program, we created Four classes Base, Derived1, Derived2, and InheritEx. The Base class contains a constructor and a method Method1().

Here, we inherited Base class into Derived1 and Derived1 inherited into Derived2 class.

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

The Derived2 class contains a constructor and a method Method3. Here, we inherited Derived1 class into Derived2 class using extends keyword.

Now look to the main() method of InheritEx class - Here, we created the object of Derived2 class then constructors will be called in the below sequence,

  • Base class constructor
  • Derived1 class constructor
  • Derived2 class constructor

And then we finally called Method1() and Method3() respectively.

Question 3:

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

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

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

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

class Derived2 extends Derived1 {
  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:33: error: cannot find symbol
    Derived2 * D = new Derived2();
    ^
  symbol:   variable Derived2
  location: class InheritEx
/InheritEx.java:33: error: cannot find symbol
    Derived2 * D = new Derived2();
               ^
  symbol:   variable D
  location: class InheritEx
/InheritEx.java:35: error: cannot find symbol
    D.Method1();
    ^
  symbol:   variable D
  location: class InheritEx
/InheritEx.java:36: error: cannot find symbol
    D.Method3();
    ^
  symbol:   variable D
  location: class InheritEx
4 errors

Explanation:

The above program will generate syntax errors because we used pointer in the main() method but Java does not support pointers that's why errors get generated.

Question 4:

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

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

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

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

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

    D.Derived();

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

Output:

/InheritEx.java:25: error: cannot find symbol
    D.Derived();
     ^
  symbol:   method Derived()
  location: variable D of type Derived
1 error

Explanation:

The above program will generate syntax error because of the below statement,

D.Derived();

Here, we called the constructor of Derived class explicitly. We cannot call the constructor of any class explicitly in Java.

Question 5:

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() {
    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
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 instead of Method() of Base class.





Comments and Discussions!

Load comments ↻





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