Hello everyone, can anyone here help me understand the difference between an abstract method and a static methods. As i understand in both these type of methods an object instantiation is not allowed. So why use two terms for the same function.
The Campbell Ritchie classification of methods classifies them into 8 categories. Every method falls in 4 categories, giving 16 different combinations, 1357, 1358, 1367, 1368, 1457, 1458, 1467, 1468, 2357, 2358, 2367, 2368, 2457, 2458, 2467, 2468
1: Those which require information from the outside world to operate. You can recognise these because they have method parameters.
2: Those which do not require information from outside; they have empty () in their heading.
3: Those which send (back) information to outside. They have a return type.
4: Those which do not send information (back); they have void instead of a return type.
5: Those which require, seek, inspect or use information from inside their own object.
6: Those which do not require any information from inside their own object.
7: Those which alter, insert or modify information inside their own object.
8: Those which do not modify insert or alter any information inside their own object.
You can add a category 1½ for methods which take an "output parameter".
Now, if you have a method which falls both into category 6 and category 8, it doesn't do anything with an object or instance. So why does it need an object or instance? Why not label it static? My idea of a static method is one which takes no information from its object and sends no information to its object. So you associate it with a class rather than an object.
The Java™ definition of an abstract method is that it is an instance method, ie it falls in category 5 or category 7 or both, doing something with information inside its own object. But the form it is given in is incomplete and different classes must implement it differently.
Campbell Ritchie wrote:Now, if you have a method which falls both into category 6 and category 8, it doesn't do anything with an object or instance. So why does it need an object or instance? Why not label it static? My idea of a static method is one which takes no information from its object and sends no information to its object. So you associate it with a class rather than an object.
Unless you implement the method because the class implements an interface. Often the implementing class becomes a singleton. A good example is String.CASE_INSENSITIVE_ORDER. The class is an unknown class that implements java.util.Comparator. Because the method is defined by an interface it cannot become static. Instead, there is only one instance of the implementing class.
Rob Spoor wrote:. . . Unless you implement the method because the class implements an interface. . . .
I think that still applies even though Java8 permits static methods in interfaces. They are often already implemented, so there is no need to bother writing them in your class and the question often doesn't arise.
Many of those static methods are factory methods and factory methods are usually static because they have to be called before any instances of the class exist.
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad: