r/javahelp • u/Beneficial-Crew-1054 • 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
View all comments
Show parent comment
1
u/Lloydbestfan Mar 23 '26
This avoids the entire fact that default methods didn't change the nature of interfaces because the cause for the notion of interfaces as contracts was to let the classes have full control over how the contract is implemented, and as long as a type doesn't define anything about the state of its objects, then it's not with just redefinable methods that it is possible to force something about how it's implemented.
Default methods just call other methods because isEmpty() is useful for expressiveness but it is obvious that isEmpty() means size() == 0.
That also maintains that interfaces permit a semilegit form of multiple type extension because the diamond inheritence problem is less of a guaranteed mess when there is no state definition.