File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes abstract class and static methods qs 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 » Beginning Java
Bookmark "abstract class and static methods qs" Watch "abstract class and static methods qs" New topic
Author

abstract class and static methods qs

Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
Hi Guys!

1. Is constructor for an abstract class really necessary?
2. Why do we have a via-object-access to invoke methods when we can, and are preferred to do it by class name?


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Not an Advanced question. It's about the basic language.

Originally posted by Akhilesh Trivedi:
1. Is constructor for an abstract class really necessary?


Assuming the abstract class is going to be subclassed by a concrete subclass, then the abstract class constructor is called when the concrete subclass is instantiated. The subclass cannot know how to initialise its superclass, so this makes sense.

For "utility classes", which are declared abstract because they contain only static methods and are not expected to be instantiated, Java's insistence on having a constructor (default or explicit) is a bit annoying. It might have been nice if the language allowed classes to be declared "abstract final" and such classes were excused from having a constructor. Perhaps Java's designers were trying to discourage "utility classes", which aren't good OO design.

Originally posted by Akhilesh Trivedi:
2. Why do we have a via-object-access to invoke methods when we can, and are preferred to do it by class name?


You mean for static methods? As far as I can see, it's just a stupid feature of the language. No good reason.
[ October 04, 2006: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
Thanks peter. I guess you are right, the constructor call on hierarchy would need to go up and up, roots of parent(abstract class) further may be too complex up ahead. Abstract class may itself inherit some members and do its own work out.
For the second one, yes i did mean static method. Thanks again.
Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199
Hi Akhilesh


2. Why do we have a via-object-access to invoke methods when we can, and are preferred to do it by class name?


I am sure you are aware of the fact that there exist two kind of members in a java class
1) Static Members
2) Instance Members

Instance members are associated with an instance/object and hence must always be invoked w.r.t object they are attached to.
Static Members however are not associated to any object, they can be thought as Global var/methods. Thus a facility to invoke them with Class name is provided. You can however always invoke a static method/variable w.r.t an instance/object. You will not get an error, though the best way to invoke a static member is former one.

hopefully below link should clarify some of your doubts-

http://blogs.sun.com/prats/entry/java_oops_session_pesit
[ October 05, 2006: Message edited by: Pratibha Malhotra ]

~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Pratibha Malhotra:
You can however always invoke a static method/variable w.r.t an instance/object. You will not get an error


You won't get a compile-time error, but you may not get the run-time behaviour you expected. The method that gets called is chosen based on the compile-time type of the object instance, not its run-time type (which could be a subclass).
Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199
Couldn't agree more on this with you Peter. The onus of using correct class in hierarchy however lies on the coder. What say
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
Static methods are meant to be invoked by Class name, why not restrict their access by class name alone. I agree with peter for this being an unnecessary feature.
Provision of invoking them by instance method would actually not be a facility rather a not-so-necessary thing. And this extra-feature may make things flexible for code-writers, but there are times like static methods can not be overridden (only be hidden) and this feature there does nothing less than consuming code-readers time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract class and static methods qs