Last Updated : 21 Apr, 2025
Prerequisite:Overriding in Java
1. What is the output of the following program?
Java
class Derived
{
protected final void getDetails()
{
System.out.println("Derived class");
}
}
public class Test extends Derived
{
protected final void getDetails()
{
System.out.println("Test class");
}
public static void main(String[] args)
{
Derived obj = new Derived();
obj.getDetails();
}
}
Options:
a) Derived class
b) Test class
c) Runtime error
d) Compilation error
Answer: (d) Compilation error
Explanation: Final and static methods cannot be overridden. For more details Check: Can we Overload or Override static methods in java ?
2. What is the output of the following program?
Java
class Derived
{
public void getDetails(String temp)
{
System.out.println("Derived class " + temp);
}
}
public class Test extends Derived
{
public int getDetails(String temp)
{
System.out.println("Test class " + temp);
return 0;
}
public static void main(String[] args)
{
Test obj = new Test();
obj.getDetails("GFG");
}
}
Options:
a) Derived class GFG
b) Test class GFG
c) Compilation error
d) Runtime error
Answer: (c) Compilation error
Explanation: The overriding method must have same signature, which includes, the argument list and the return type. For more details, See: Overriding in Java
3. What is the output of the following program?
Java
class Derived
{
public void getDetails()
{
System.out.println("Derived class");
}
}
public class Test extends Derived
{
protected void getDetails()
{
System.out.println("Test class");
}
public static void main(String[] args)
{
Derived obj = new Test();
obj.getDetails();
}
}
Options:
a) Test class
b) Compilation error due to line xyz
c) Derived class
d) Compilation error due to access modifier
Answer: (d) Compilation error due to access modifier
Explanation: A subclass cannot override a method and reduce its visibility. The base method is public, but the subclass method is protected, which is more restrictive. This causes a compilation error.
4. What is the output of the following program?
Java
import java.io.IOException;
class Derived {
public void getDetails() throws IOException { // Line 4
System.out.println("Derived class");
}
}
public class Test extends Derived {
public void getDetails() throws Exception { // Line 10
System.out.println("Test class");
}
public static void main(String[] args) throws IOException { // Line 14
Derived obj = new Test();
obj.getDetails();
}
}
Options:
a) Compilation error due to line 4
b) Compilation error due to line 10
c) Compilation error due to line 14
d) All the above
Answer: (b) Compilation error due to line 10
Explanation: The exception thrown by the overriding method should not be new or more broader checked exception. In the code above, Exception is more broader class of checked exception than IOException, so this results in compilation error.
5. What is the output of the following program?
Java
class Derived
{
public void getDetails()
{
System.out.printf("Derived class ");
}
}
public class Test extends Derived
{
public void getDetails()
{
System.out.printf("Test class ");
super.getDetails();
}
public static void main(String[] args)
{
Derived obj = new Test();
obj.getDetails();
}
}
Options:
a) Test class Derived class
b) Derived class Test class
c) Compilation error
d) Runtime error
Answer: (a) Test class Derived class
Explanation: Since getDetails() is overridden in the Test class and super.getDetails() is called inside it, both methods are executed in order.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4