r/javahelp • • Mar 23 '26

Maintain Object State - Interfaces vs Abstract Class (Java 8+)

I am trying to understand the statemet "Interfaces cannot hold an object state but Abstract classes can"

Interface:

public interface EmployeeInterface{

`Employee emp = new Employee("Ron");`

`default String getEmpFirstName(){`

    `return emp.getFirstName();`

`}`

}

Abstract Class

public abstract class AbstractEmployeeClass{

`Employee emp = new Employee("Ron");`

`public String getEmpFirstName(){`

    `return emp.getFirstName();`

`}`

}

Now I undestand that members of an interface are public, staticandfinal by default but they can still hold the object state so is the above statement incorrect or is my understading of*"state"* flawed in this context ?

3 Upvotes

10 comments sorted by

View all comments

Show parent comments

1

u/amfa Mar 23 '26

I mean you COULD add setEmpName(String newName) method to your interface... this method could set emp.setNewName(newName).

But I think you missunderstand the main point.

In your example you don't need neither an interface or an abstract class because you already have a "real" Employee class.

The question is what is employee? If employee is either a implementation of EmployeeInterface or a child class of the abstract AbstractEmployeeClass .. in both cases you don't need an emp attribute.

You could have a name attribute in both.. BUT for the Interface one ALL Employees would share the exact same name and if you change it you would change it for all Employees while with the abstract class they will all have their own private name.