• 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

Use of static in abstract class..

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly is the use of static methods in abstract classes. They can never be overloaded. Are there any classes in java api where a class is abstract and all its methods are static
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having ALL methods static in an abstract class seems kinda weird.
Having SOME static members (but of course no abstract members) is perfectly acceptable.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are classes that cannot be instantiated, but all the methods are static, like java.lang.System and java.lang.Math. These aren't abstract, but have private, unused constructors.
One reasonable use of a static method in an abstract class would be a factory methods. You would call Foo.createNew(arguments) to create an instance of some concrete subclass.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. An abstract class is a class that is incomplete, or to be considered incomplete. Classes C1 and C2 implement the methods of I.
2. A method declared in an interface must not be declared static. The static methods common to all subclasses of A are declared in A.
[ October 10, 2003: Message edited by: Marlene Miller ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marlene Miller:
An abstract class is a class that is incomplete, or to be considered incomplete.

Abstract is an inheritance hierarchy tool. It allows you to create classes that you don't want to be instantiated but you do want to use as models for child classes. Abstract classes do not have to have abstract methods. This is a perfectly valid abstract class:
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to the previous posts, I offer two other thoughts.
First, it is perfectly normal and valid for an abstract class to contain non-abstract (i.e., fully implemented) methods, static or otherwise. An abstract class can include as much implementation as is appropriate. This is one thing that differentiates an abstract class from an interface.
Second, defining a static method inside an abstract class enables that method to be called independently of any object. Thus, a static method of an abstract class can be called without there being any instances of a concrete subclass in existence.
Here is a rather contrived example that illustrates these two points.

The output is shown here.
Number of objects that include A: 0
Number of objects that include A: 1
Number of objects that include A: 2
[ October 10, 2003: Message edited by: Herb Schildt ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic