Which code fragment is illegal?

Which code fragment is illegal?

Which code fragment is illegal?

A.
class Base1 {
abstract class Abs1 { }}

B.
abstract class Abs1 {
void doit () { }
}

C.
class Basel {
abstract class Abs1 extends Basel {

D.
abstract int var1 = 89;

Explanation:
The abstract keyword cannot be used to declare an int variable. The abstract keyword is used to declare a class or method to be abstract[3]. An abstract method has no implementation; all classes containing abstract methods must themselves be abstract, although not all abstract classes have abstract methods.



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Abed Abu Alhalawa

Abed Abu Alhalawa

D

James

James

The Answer is D.

The non-access modifier abstract can be applied to classes and methods. Interfaces are implicitly abstract. Adding abstract to an interface is redundant.

The abstract keyword cannot be used to modify variables in Java.

If a class contains abstract methods then the class should be declared abstract. Otherwise a compile error will be thrown.

An abstract class may contain both abstract methods as well normal methods. An abstract class can be nested within a normal class. Also a normal class can be nested within an abstract class.

Note: Answer C is missing two right curly braces (} }).