I'm reading "Effective Java", here is a quote from this book:
The interface defines the type, perhaps providing some default methods, while the skeletal implementation class implements the remaining non-primitive interface methods atop the primitive interface methods. Extending a skeletal implementation takes most of the work out of implementing an interface. This is the Template Method pattern.
The author is talking about some abstract classes like AbstractCollection
, AbstractSet
, etc, which implement an interface and provide some basic implementations. However, I don't know what non-primitive interface methods
and primitive interface methods
mentioned in the quote are. I know "primitive types" in Java, but what is "primitive methods"?
Interface PrimitiveType Represents a primitive type. These include boolean , byte , short , int , long , char , float , and double .
An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types.
The interface body can contain abstract methods, default methods, and static methods. An abstract method within an interface is followed by a semicolon, but no braces (an abstract method does not contain an implementation).
The main reason primitive data type are there because, creating object, allocating heap is too costly and there is a performance penalty for it. As you may know primitive data types like int, float etc are most used, so making them as Objects would have been huge performance hit.
In this context, a "primitive method" doesn't have anything to do with primitive types -- the meaning of "primitive" is different here. According to "Method Properties in Java" (p. 3), a primitive method performs a basic task that doesn't rely on any other method to help do its work.
A primitive method is a method that carries out one specific task, usually by directly referring to the fields of the object. It does not rely on any (non-primitive) methods of the class that defines the primitive method.
This is opposed to a "composed method" that relies on calling other methods to perform subtasks. It appears that a primitive method performs tasks that are not or should not be broken down into smaller subtasks represented by other methods.
As an example, you may have a Time
class that has hours and minutes. Primitive methods may be individual setters for each of the hour and minutes fields, e.g. setHour
and setMinutes
. A composed method, e.g. setTime
, may call setHour
and setMinutes
to do its work.
The template method pattern involves creating a composed method that defines the order and structure of a workflow of tasks to be done, calling other methods that may be primitive. With the advent of default
methods in Java 8, it is possible for these methods to be in interfaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With