Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "primitive interface method" in Java?

Tags:

java

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"?

like image 430
Searene Avatar asked May 23 '19 15:05

Searene


People also ask

Which is a primitive type of interface?

Interface PrimitiveType Represents a primitive type. These include boolean , byte , short , int , long , char , float , and double .

What is interface method in Java?

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.

What are the different methods of interface?

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).

Why does Java have primitives?

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.


1 Answers

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.

like image 72
rgettman Avatar answered Oct 28 '22 13:10

rgettman