Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why it is required to add abstract keyword in front of abstract methods

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?

like image 623
Saif Avatar asked Jan 09 '23 11:01

Saif


2 Answers

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.

like image 143
Konstantin Yovkov Avatar answered Jan 22 '23 23:01

Konstantin Yovkov


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.

like image 44
vikingsteve Avatar answered Jan 22 '23 22:01

vikingsteve