This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.