Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between an abstract class and a class that has all its methods abstract?

I wonder what is the difference in Java between an abstract class and a class that has all its methods abstract? I mean, is an abstract class just a class whose methods automatically get abstract?

like image 851
Albus Dumbledore Avatar asked Aug 05 '10 20:08

Albus Dumbledore


People also ask

What is difference between abstract class and abstract method?

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

What is a better abstract class with all abstract methods or interfaces?

An interface is a better choice for defining an abstract API. Java allows classes to extend at most one class, but implement multiple interfaces.

How abstract class is different from other methods?

Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.


1 Answers

Absolutely not. Indeed, a class can be abstract without any methods being abstract, although that's relatively rare (see Mark's comment below for an example). On the other hand, if a class has any abstract methods, then it must be declared abstract.

Generally speaking, the purpose of an abstract class is to provide a skeleton with some non-abstract behaviour, but other bits still to be filled in by subclasses. This can be used with the template method pattern, for example.

like image 168
Jon Skeet Avatar answered Sep 23 '22 20:09

Jon Skeet