4

This could be a useless question, Just out of my curiosity.

Why are we forced to add the keyword abstract in front of abstract method of abstract class, when we don't need to add the keyword in the case of interface?

The possible answer could be:
As an abstract class may be a mix of concrete and abstract methods, the 'abstract' keyword is used to identify which method(s) are abstract.
But
When we don't give any body to any methods, and use ; at the end of a declaration, then what could go wrong if it is considered as an abstract method automatically?

1
  • 2
    Methods declared in interfaces are public abstract by default, that is why. They are in fact abstract. Commented Apr 30, 2015 at 12:32

4 Answers 4

5

Why we are forced to add the keyword abstract in front of abstract method of abstract class, when we don't need to add the keyword in case of interface.

Within interfaces, all method definitions are implicitly abstract. You can provide the keyword, though, but there won't be any difference.

Within abstract classes, however, when you want to denote a method as abstract, you're required to type the abstract keyword. This actually makes the code more readable and easy to be understood.

Sign up to request clarification or add additional context in comments.

4 Comments

readability is a very valid point, but it's till don't explain why it required instead of optional.
can we conclude to this is how the language is designed ?
@Saif, because if it was optional, everybody would skip it. :)
liked that point too. and thanks for spending your time on a useless question :)
4

Not all methods in an abstract class need be abstract. In fact, you can have an abstract class without any abstract methods.

On the other hand, all methods defined in an interface are implicitly public abstract, and so you can drop the qualifier.

6 Comments

well i understand why i can drop the qualifier in case of interface. My question is why it is required in case of abstract class. because abstract method can be identified by no body and the ; at the end.
Kocko mentioned readability. The simple inclusion or absence of a single-character ; would be far to easy to miss when you quickly read code, especially at the end of a large message signature.
@vikingsteve the OP's point is: the abstract method has no method body... how could anyone misread that? ; at the end of the line is unimportant. So i agree, abstract seems redundant.
Well, imagine that you added an extra semi-colon at the end of your function, accidentally, before the method body. Then the compiler would interpret your method as being abstract, followed by an instance initializer. e.g. void myMethod; { /* code */ } - this would be legal, and confusing.
@vikingsteve void myMethod; will be legal? how can you explain ?i didn't get it. Why would compiler consider void myMethod; as correct as it not a variable not method (don't have () at the end )?
|
1

You mention it explicitly so that anyone extending the class will know that he should implement those methods compulsorily and need not bother about the methods without the abstract keyword if he doesn't need to.

2 Comments

so you are talking about readability as mentioned by @kocko
Yes, to think if it relates to understanding and readability.
0

Two answers

  1. Thats just the way language is designed
  2. Abstract methods are meant to be overriden in child classes and that means some VM's can do various optimizations when they see abstract keyword

Btw. In java 4 there was actually abstract keyword in interfaces. Now it is considered as obsolete

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.