How to declare java abstract class?

Which of the following declaration will compile without errors?
Choose 2

if a class contains an abstract method , then it must be marked abstract.

An abstract method has no implementation .

Total comments (14)

if an abstract class contains concrete methods it still compiles fine
Even if an abstract class has all concrete methods we can still mark it abstract.. it will compile fine
Java Developer
4site Technology
 10 months
option 5 is correct
Nitendra Dhaked
 1 year
non abstract mehod must have body in abstract class.
- a method without a body MUST be marked as abstract => then also its class MUST be marked as abstract else compilation error. Else the method must be given a body!
- an abstract METHODis a method with no body(so it must end with a semicolon(;) to mark its end) + it must be declared as "abstract" + its class must also be declared "abstract"

- an abstract CLASS is a class declared "abstract" and it can contain or not: abstract AND/or concrete(with a body) methods

- a class that has AT LEAST 1 abstract method MUST be declared abstract

Amrish JhaveriStudentIndia
 2 years
only 1 is correct!!! no. 4 is incorrect since it has no body and no. 5 is incorrect since it has a semicolon which is incorrect.
Anonymous User
 2 years

Yeah, and public void print(); has no body. 4th is invalid.

 3 years
The question is incorrect, answer 4 is valid. A class marked as abstract doesn't actually have to have any abstract members. However, if a class has any abstract members, it HAS TO BE marked as abstract. These 2 statements are not the same.
Senior Java Developer
 4 years
The wording was amended, thanks spencetj
Anonymous User
 4 years
The question wording doesn't make sense. Is it asking for compilation errors or error-free compilations?
Java DeveloperUSA
 4 years
The braces in a method with implementation, them alone indicate the start and end of a method to a compiler, with no need to a semi colon. But it's presence doesn't cause any compilation error, because the compiler sees the end of the method by the last brace , then see emptiness (no statement) then semi colon.

In an abstract method, you must state for the compiler the end of the method declaration by a semi colon , since no braces are there.

Java DeveloperUSA
 4 years
As you said, it is unneeded, but it's presence doesn't cause any compilation error. Actually you can add 10 semi colons after the method and still no errors produced.

A semi colon in Java indicates the end of a statement.

I don't believe number five is correct, though I could be wrong. Wouldn't there be a compilation error on number 5 due to the unneeded semicolon?
Copy and paste the following HTML code into your website or blog.
<iframe src="" width="600" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="yes" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px" allowfullscreen webkitallowfullscreen mozallowfullscreen> </iframe>