• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of static in abstract class..

 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • 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
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • 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 ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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:
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic