Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the JLS specify the terms "abstract method", "concrete method" and "default method"?

I have seen "divergent" definitions of the terms abstract method, concrete method and default method in some StackOverflow answers.

What are the real definitions, as given by the Java Language Specification? Please include the relevant supporting JLS references in your answer.

like image 643
Stephen C Avatar asked Jan 28 '20 14:01

Stephen C


People also ask

What is abstract method and concrete method?

An abstract method is declared by abstract keyword, such methods cannot have a body. If a class contains an abstract method, then it also needs to be abstract. Concrete Class: A concrete class in Java is a type of subclass, which implements all the abstract method of its super abstract class which it extends to.

What is the difference between default method and abstract method?

The abstract class can have a state, and its methods can access the implementation's state. Although default methods are allowed in an interface, they can't access the implementation's state.

What is a concrete method?

A concrete method means, the method has complete definition but it can be overridden in the inherited class. If we make this method "final" then it can not be overriden. Declaring a method or class "final" means its implementation is complete.

What is the abstract method?

Abstract methods are those types of methods that don't require implementation for its declaration. These methods don't have a body which means no implementation. A few properties of an abstract method are: An abstract method in Java is declared through the keyword “abstract”.


1 Answers

The links / section numbers below are taken from the Java 11 version of the Java Language Specification.

According to JLS 8.4.3.1:

"An abstract method declaration introduces the method as a member, providing its signature (§8.4.2), result (§8.4.5), and throws clause if any (§8.4.6), but does not provide an implementation (§8.4.7). A method that is not abstract may be referred to as a concrete method."

According to JLS 9.4:

"A default method is an instance method declared in an interface with the default modifier. Its body is always represented by a block, which provides a default implementation for any class that implements the interface without overriding the method. Default methods are distinct from concrete methods (§8.4.3.1), which are declared in classes, and from private interface methods, which are neither inherited nor overridden."

So according to this taxonomy there are really 4 types of methods:

  • abstract methods,
  • concrete methods,
  • default methods and
  • private interface methods

Note that JLS 8.4.3.1 makes no mention of final or static in the distinction between abstract and concrete methods.

These modifiers cannot be used with the abstract keyword. This implies that methods that are static or final must be concrete methods. This reinforces the 8.4.3.1 definition of a concrete method.

like image 103
Stephen C Avatar answered Sep 19 '22 12:09

Stephen C