Define an abstract property in java

Abstract keyword is normally applied on classes and methods in order to delegate the implementation of some behavior to subclasses. Java doesn’t support abstract properties, if you try to mark a class property as abstract, you get a compilation error.

In this tutorial, we introduce 2 ways for defining abstract properties which are set by subclasses without using the abstract keyword.

1. Use case

Suppose we want to implement a transaction logging module which logs information about a particular transaction. We want our module to be abstract so that we’re able to support different ways of logging: file system logging, database logging … etc.

Our engine concatenates all the transaction information in one String using a predefined separator which depends on the logging mechanism, for example the comma character “,” is used for logging a comma-separated string in the file system.

So the separator looks as abstract to our engine and needs to be defined explicitly by each logging mechanism.

2. Define abstract property

In this section, we provide 2 ways for delegating the definition of the separator to subclasses.

2.1 Define a parameterized constructor in the abstract class

The first way of delegating the definition of a dynamic property in an abstract class is through defining a parameterized constructor.

So we create our engine as the following:

When defining a parametrized constructor in an abstract class, the subclasses are forced to define their own constructors and call the super constructor. Hence, we forced the separator attribute to be dependent on the used logging mechanism.

As noticed, our engine provides implementation for the static behaviors which are common between all logging mechanisms like: startTransaction(), endTransaction(), while it delegates the dynamic behavior writeTransaction() to children classes.

Now, if we want to create a transaction manager which logs to a file system, we would define it as the following:

Let’s do some test to see how the above implementation works:


3. Passing separator through getter method

Another way to delegate the definition of a dynamic property is through defining an abstract getter method, which retrieves the required separator based on the used logging mechanism. In our engine, we then use the getter method whenever we want to use the separator.

So we modify our engine to be as the following:

respectively TransactionManagerFS becomes as the following:

We then update our main class to use the new implementation, and we make sure that the same result is obtained.


That’s it, if you have any other way just share it with me here in the comments section below.

0 0 votes
Article Rating

Hussein Terek

Owner of, I have a passion for software engineering and everything related to Java environment.

Newest Most Voted
Inline Feedbacks
View all comments
Ahmed Ragab
Ahmed Ragab
3 years ago

You can use Apache common CSVPrinter and add a setter so that you can replace the default CSVPrinter hence nothing is abstract and the seperator and the quoting and all the other stuff are dynamic

Larry L Creager
Larry L Creager
1 year ago

In your final example, how was getSeparator invoked?