What is the point of having an abstract class without abstract methods?

It may have an abstract class that implements all its methods , without abstract methods.

For instance:

public abstract class someClass { int a; public someClass (int a) { this.a = a; } public void m1 () { /* do something */ } private void m2 () { /* do something else */ } } 

What advantage, if any, is having such an abstract class compared to the same class as a particular one?

I can think that when I declare it abstract, it will not be created. however, I can have the same effect, making it concrete and its constructor (parts).

TIA.

// ===================

EDIT: Another use I can think of:

it can extend another abstract class or implement an interface without implementing the abstract methods of the class, although it implements all its methods. for whatever it costs.

+5
source share
9 answers

It has conceptual meaning: this class has behavior that in itself does not make sense.

Of course, it is difficult to imagine such a scenario without clearly defined extension points (i.e. abstract methods), but sometimes it will be a fairly accurate model of your problem.

You might have something like this:

 public abstract class ObjectWithId { private final String id; public ObjectWithId( String id ) { this.id = id; } public final String getId() { return id; } } 

And then you can expand it to declare different types of objects with identifiers. Here you have a completely defined and implemented behavior, but no restrictions on any other subclasses of behavior may appear.

Note that a much simpler way to model the same thing is to use composition instead of inheritance.

 public final class ObjectWithId<T> { private final String id; private final T ob; public ObjectWithId( String id, T ob ) { this.id = id; this.ob = ob; } public String getId() { return id; } public T getObject() { return ob; } } 

But before generics were introduced (up to Java 1.4), it would not be so elegant and clearly better than an abstract class solution, because you would have to trade type security.

+2
source
  • you can declare the implementation of the interface and not provide an implementation, and then each child implicitly receives an extended interface

  • you prevent instantiation of this class

  • in the future you will provide a common realization for all children.
+2
source

As you pointed out, you can prevent an instance of the class from being created by making it a private constructor. In contrast, there is no benefit. This is probably supported only for language completeness.

+2
source

Usually we use the concept of abstraction with inheritance

Consider using abstract classes if any of these statements apply to your situation:

  • You want to share code between several related classes.

To answer your question,

Why declare a class with concrete methods Abstract?

One possible reason is to support inheritance without actually creating objects


Suppose you have two classes: one abstract and the other concrete

Abstract class: AbsClass

 abstract class AbsClass { int a = 5; //Constructor public AbsClass() { System.out.println(a); } void methodA() { System.out.println(a + 10); } } 

and Concrete Class: ConcreteClass

 class ConcreteClass { int a = 10; //Made the constructor Private to prevent from creating objects of this class private ConcreteClass() { System.out.println(a); } void methodA() { System.out.println(a + 10); } 

}

The above two classes should work similarly (?) Until you try to subclass them

 class AbsImplementer extends AbsClass { //Works fine } class ConcImplementer extends ConcreteClass { //Compilation Error Implicit super constructor ConcreteClass() is not visible } 
+2
source

This can be useful if you consider it a utility class .

+1
source

The practical difference is that you cannot instantiate it. You will have to subclass it and instantiate the subclass.

As for WHY, you would like to do this, in practice ... I find it difficult to think of a good reason. We can say that a class makes sense only if someone creates a subclass that implements some function. But why not make this function abstract in the superclass?

I would not rule out that someone might come up with some example where this makes sense, but I cannot think about it. Just because you can write a piece of code and successfully compile the code does not mean that it makes sense. I mean, I can write "total_price = item_price * zip_code + customer_height_in_cubits - 7.879", but this does not mean that such a line of code would make sense.

+1
source

Suppose you don’t care if the methods of the abstract class are implemented or abstract, but by design it should be abstract, so when someone extends it, they should add more methods or override the existing ones or use as is. If they do not want to override the methods, then the default behavior is already provided for in this abstract class.

In this abstract class, the only criteria that you observe is that there simply cannot be an instance of this class, and before use, it must have its only version of the class.

Thus, in general, an abstract class with several or all implementable methods is much better than with an interface that has no implemented methods at all. This is based on the assumption that you use it as one inheritance.

+1
source

Consider something similar to the NVI pattern (not sure what you would call it in Java):

 public abstract class A { public final void doSomething() { System.out.println("required"); doOptional(); } protected void doOptional() { System.out.println("optional"); } } public class B extends A { @Override protected void doOptional() { System.out.println("overridden"); } } 

For your public API, you publish a public final method that cannot be overridden. It does some necessary work inside and an extra method. When extending this class, you can only override doOptional ().

A call to B.doSomething () will always print β€œrequired” before continuing.

Since doOptional () is not abstract, there is no purely coded reason that class A should be abstract. But this may be desirable for your specific project. For example, a basic service that always extends to specific subprojects.

+1
source

This can be useful for cases when classes derived from an abstract base class must have some kind of behavior that is different from each other, but this behavior cannot be abstracted as being inside a method that has the same signature for all classes. An inability to exchange a signature can occur if other behavior requires methods that are passed using different primitive types. Since they use primitive types, you cannot use generics to express similarities.

An abstract base class, without any abstract methods, acts a little like a marker interface, since it declares that implementing classes should provide some behavior without this behavior being encapsulated in a new method with a signature that is the same for all implementations. You should use an abstract base class rather than a marker interface if the implementation classes have common behavior, especially if the base class can implement it for derived classes.

For instance:

 abstract class Sender { protected final void beginMessage() { ... } protected final void endMessage() { ... } protected final void appendToMessage(int x) { ... } } final class LongSender extends Sender { public void send(int a, int b, int c) { beginMessage(); appendToMessage(a); appendToMessage(b); appendToMessage(c); endMessage(); } } final class ShortSender extends Sender { public void send(int a) { beginMessage(); appendToMessage(a); endMessage(); } } 
+1
source

Source: https://habr.com/ru/post/1201940/


All Articles