Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract Methods Vs. Static Methods

 
Kalyan Naveenan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Walter Gabrielsen Iii
Ranch Hand
Posts: 158
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static method is outside the flow of objects. You don't need an object to call a static method. You call a static method with the class name, .(dot), and method name, example: Math.random().

An abstract method is empty, like an interface. You must create a subclass and override the method to make it work.
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For abstract methods, you will need to instantiate an object. Just not an object of that class, but of some class that extends the class and implements the method.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20529
    54
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 48954
    60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic