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.
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Answer Question 1:
Output:
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.
Answer Question 2:
Output:
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,
And then we finally called Method1() and Method3() respectively.
Answer Question 3:
Output:
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.
Answer Question 4:
Output:
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.
Answer Question 5:
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 instead of Method() of Base class.
need an explanation for this answer? contact us directly to get an explanation for this answer