Quiz About Java Inheritance
Quiz About Java Inheritance
1, 2 and 4
Only 1 and 2
1, 2 and 3
2, 3 and 4
Discuss it
Question 1 ‒ Explanation
See http://www.geeksforgeeks.org/can-override-private-methods-java/ and htt
p://www.geeksforgeeks.org/comparison-of-inheritance-in-c-and-java/
Question 2
Java
class Base {
public void Print() {
System.out.println("Base");
}
}
class Main{
public static void DoPrint( Base o ) {
o.Print();
}
public static void main(String[] args) {
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}
Base
Derived
Derived
Base
Base
Derived
Base
Derived
Base
Compiler Error
Discuss it
Question 2 ‒ Explanation
See question 1 of
http://www.geeksforgeeks.org/output-of-java-program-set-2/
Question 3
Predict the output of following program. Note that fun() is public in base and
private in derived.
Java
class Base {
public void foo() { System.out.println("Base"); }
}
Base
Derived
Compiler Error
Runtime Error
Discuss it
Question 3 ‒ Explanation
It is compiler error to give more restrictive access to a derived class function w
hich overrides a base class function.
Question 4
Java
// filename Main.java
class Grandparent {
public void Print() {
System.out.println("Grandparent\'s Print()");
}
}
Grandparent\'s Print()
Parent\'s Print()
Child\'s Print()
Runtime Error
Discuss it
Question 4 ‒ Explanation
In Java, it is not allowed to do super.super. We can only access Grandparent\'s
members using Parent. For example, the following program works fine.
class Main {
public static void main(String[] args) {
Child c = new Child();
c.Print();
}
}
Question 5
Java
class Base {
public void show() {
System.out.println("Base::show() called");
}
}
Derived::show() called
Base::show() called
Discuss it
Question 5 ‒ Explanation
In the above program, b is a reference of Base type and refers to an abject of De
rived class. In Java, functions are virtual by default. So the run time polymorph
ism happens and derived fun() is called.
Question 6
Which of the following is true about inheritance in Java. 1) In Java all classes
inherit from the Object class directly or indirectly. The Object class is root of all
classes. 2) Multiple inheritance is not allowed in Java. 3) Unlike C++, there is
nothing like type of inheritance in Java where we can specify whether the
inheritance is protected, public or private.
1, 2 and 3
1 and 2
2 and 3
1 and 3
Discuss it
Question 6 ‒ Explanation
See Comparison of Inheritance in C++ and Java
Question 7
class Main {
public static void main(String args[])
{
Complex c = new Complex(10, 15);
System.out.println("Complex number is " + c);
}
}
Discuss it
Question 7 ‒ Explanation
See http://www.geeksforgeeks.org/overriding-tostring-method-in-java/
Question 8
Java
class Base {
final public void show() {
System.out.println("Base::show() called");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
Base::show() called
Derived::show() called
Compiler Error
Runtime Error
Discuss it
Question 8 ‒ Explanation
Final methods cannot be overridden. See the compiler error
here
.
Question 9
\Java\
class Base {
public static void show() {
System.out.println("Base::show() called");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();
b.show();
}
}
Base::show() called
Derived::show() called
Compiler Error
Discuss it
Question 9 ‒ Explanation
Like C++, when a function is static, runtime polymorphism doesn\'t happen.