Can I create an object with an abstract class

An abstract class is a class that cannot be used to create objects. Why do you need a class from which you cannot create objects? At first glance, that seems pretty pointless. At second glance, however, not at all. When you are working on a project, you will very quickly get to the point where you are designing different classes that arise from the problem at hand. You will try to put them into a meaningful inheritance hierarchy and then you will soon have put together half a dozen or more classes. Methods in different classes that have similar tasks will be given the same name and then more or less automatically come up with the idea of ​​creating these methods high up in the hierarchy. They will then inherit the lower-level classes and overwrite them if necessary, i.e. adapt them according to their needs. The idea of ​​making these methods mandatory is only one step further, so that as a user of the class hierarchy one can simply be sure that these methods exist in all derived classes. The same can be done with the properties of a class. This is how you come up with the idea of ​​creating a class that is responsible, so to speak, for the design of the entire hierarchy. You don't necessarily need objects from such a class, it is "only" there to make a class hierarchy consistent. For this reason the concept of the abstract class was introduced. Most of the time, abstract classes are very high in the hierarchy.

As shown in the following example, the abstract modifier alone is sufficient to make a class abstract. You cannot create objects with the constructor now.

// ------------------------ file -------------------- ---- \ public abstract class Abstract {int a; public Abstract () {} public void nichtAbstract () {}} // ---------------------- File -------- -------------- \ public class TestAbstract {public static void main (String args []) {Abstract abs; // that works // abs = new Abstract (); // that doesn't work // compiletime error: // Abstract is abstract; cannot be instantiated abs = new Abstract (); }}

In the above case, the developer explicitly forbids the instantiation of objects of this class. If he were to leave out the word abstract, objects could easily be created from this class.

Somewhat more often it is the case that a class has to be made abstract. In addition to the term abstract class, there is also the term abstract method. An abstract method is a method without a body, i.e. without an implementation. The following example contains an abstract method and a non-abstract method.

public class Abstract2 // here the modifier abstract => compiletime error {public void not abstract () {} public abstract void already abstract (); // Don't forget the semicolon !! }

If a class contains (at least) one abstract method, the class must also be abstract. If you derive a class that contains abstract methods, then the derived class itself is abstract again. However, it can become real if (all) of the inherited abstract methods are implemented. If, on the other hand, one derives from an abstract class that does not contain any abstract methods, then this derivation is automatically real. The last situation is not as outlandish as one might think. Take a look at the java.awt.Component class, it's abstract, but none of its methods are abstract.
Again the above example, but this time without errors:

public abstract class Abstract2 // OK {public void notAbstract () {} public abstract void alreadyAbstract (); // Don't forget the semicolon !! }

There is one more thing to remember: an abstract method cannot be static or private.
Here is a little summary.

  • Objects cannot be created from an abstract class (no constructor call possible)

  • A class becomes abstract if the modifier abstract is added before the keyword class.

  • A class must be declared abstract if it contains at least one abstract method.

  • An abstract method has no body. The declaration ends with a semicolon.

  • A class derived from an abstract class can only become real if it implements all of the inherited abstract methods. If it doesn't, it must also be declared abstractly.

  • The parent class of an abstract class can be real or abstract.

  • A constructor cannot be abstract.

  • The abstract modifier cannot be used on variables.

  • The combination of abstract and static is not permitted.

  • The combination of abstract and private is not permitted.

The last limitation immediately makes sense after a moment's thought. Think about it.

Another application of abstract classes are classes that are almost finished, so to speak. What this means is that the abstract class already implements almost all methods that are necessary for a specific purpose and only leaves one or two abstract methods. Such classes are used in such a way that a derivation is made from them and the abstract methods are implemented according to one's needs. An example of this is the AbstractAction class from the javax.swing package. Here a user only has to write the method actionPerformed () in a derivation and his class is real.

The abstract class AbsPers