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();
}
}
Answer Question 1:
Output:
Explanation:
The above program will generate syntax error because base keyword does not exist in Java.
Answer Question 2:
Output:
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:
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:
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:
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