aspose file tools*
The moose likes Java in General and the fly likes Use of static in abstract class.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Use of static in abstract class.." Watch "Use of static in abstract class.." New topic
Author

Use of static in abstract class..

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
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


Thanks,
Shekar
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Having ALL methods static in an abstract class seems kinda weird.
Having SOME static members (but of course no abstract members) is perfectly acceptable.


42
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391

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

Joined: May 05, 2000
Posts: 13974
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:


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
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 ]

For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of static in abstract class..