If you look up the java docs, you will get the actual statements from there.
Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.
Which should you use, abstract classes or interfaces?
Consider using abstract classes if any of these statements apply to your situation:
- You want to share code among several closely related classes.
- You expect that classes that extend your abstract class have many common
methods or fields, or require access modifiers other than public (such as protected and private).
- You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Consider using interfaces if any of these statements apply to your situation:
1.You expect that unrelated classes would implement your interface. For example, the
interfaces Comparable and Cloneable are implemented by many unrelated classes.
2.You want to specify the behavior of a particular data type, but not concerned about who
implements its behavior.
3.You want to take advantage of multiple inheritance of type.
However, based on your question first edit, you could define a field in an interface, however there is a caveat. its has to be public, static and final.
In other words, define only constants ;)
public interface TimeClient {
public static final String TIME_ZONE = "Europe/Amsterdam"; //Defines a static field
static public ZoneId getZoneId(String zoneString) {
try {
return ZoneId.of(zoneString);
} catch (DateTimeException e) {
System.err.println("Invalid time zone: " + zoneString
+ "; using default time zone instead.");
return ZoneId.systemDefault();
}
}
//Defines a default method
default public ZonedDateTime getZonedDateTime(String zoneString) {
return ZonedDateTime.of(LocalDateTime.MAX, getZoneId(zoneString));
}}
The above code will compile.