Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Easy
Question 5
Consider below code of Child.java file:
- package com.udayan.ocp;
- class Parent {
- public void m() {
- System.out.println("Parent");
- }
- }
- public abstract class Child extends Parent { //Line 9
- public static void main(String [] args) { //Line 10
- new Parent().m(); //Line 11
- }
- }
What is the result?
- A Compilation error at Line 10
- B Compilation error at Line 11
- C Compilation error at Line 9
- D Parent
Reveal correct answer
Correct answer: D
Explanation
Parent is a concrete class.
In java it is possible for abstract class to not have any abstract methods.
Class Child is abstract, it extends Parent and doesn't contain any abstract method. It contains special main method, so JVM can invoke this method.
Rest of the code is normal java code. new Parent().m(); creates an instance of Parent class and invokes method m() on that instance. "Parent" is printed on to the console.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
