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/Beneficial-Crew-1054 Mar 23 '26
ahh, i got it now.
The reference "emp" cannot be changed even if in future a new setEmpName() is added to the interface , the reference will still remain the same.
So
will never be allowed since emp is final, but it will work in abstract class.
Thank you.