• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Abstract Methods Vs. Static Methods

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 160
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Posts: 22783
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 79178
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic