Java OCA OCP Practice Question 2598

Question

Consider the following program and predict the behavior of this program:

class Base {//  w  w  w .  j  a v a  2  s. c  o m
        public void print() {
                System.out.println("Base:print");
        }
}

abstract class Main extends Base { //#1
        public static void main(String[] args) {
                Base obj = new Base();
                obj.print(); //#2
        }
}
  • a) Compiler error "an abstract class cannot extend from a concrete class" at statement #1.
  • b) Compiler error "cannot resolve call to print method" at statement #2.
  • c) The program prints the following: Base:print.
  • d) The program will throw a runtime exception of AbstractClassInstantiationException.


c)

Note

It is possible for an abstract class to extend a concrete class (though such inheritance often doesn't make much sense).

Also, an abstract class can have static methods.

Since you don't need to create an object of a class to invoke a static method in that class, you can invoke the main() method defined in an abstract class.




PreviousNext

Related