Output of Java program | Set 18 (Overriding)

03 Jun, 2017
Prerequisite – Overriding in Java
1) What is the output of the following program?
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();    }}
a) Derived classb) Test classc) Runtime errord) Compilation error
Ans. (d)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?
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");    }}
a) Derived class GFGb) Test class GFGc) Compilation errord) Runtime error
Ans. (c)Explanation: The overriding method must have same signature, which includes, the argument list and the return type. For details, See : Overriding in Java
3) What is the output of the following program?
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();  // line xyz        obj.getDetails();    }}
a) Test classb) Compilation error due to line xyzc) Derived classd) Compilation error due to access modifier
Ans: (d)Explanation: The overriding method can not have more restrictive access modifier.
4) What is the output of the following program?
import java.io.IOException;  class Derived {    public void getDetails() throws IOException //line 23    {        System.out.println("Derived class");    }}  public class Test extends Derived{    public void getDetails() throws Exception //line 24    {        System.out.println("Test class");    }    public static void main(String[] args) throws IOException //line 25    {        Derived obj = new Test();        obj.getDetails();    }}
a) Compilation error due to line 23b) Compilation error due to line 24c) Compilation error due to line 25d) All the above
Ans. (b)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?
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();    }}
a) Test class Derived classb) Derived class Test classc) Compilation errord) Runtime error
Ans. (a)Explanation: super keyword is used to invoke the overridden method from a child class explicitly.
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Java-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.