File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Can abstract class has static member? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can abstract class has static member?" Watch "Can abstract class has static member?" New topic
Author

Can abstract class has static member?

SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
Hi,
1. I have two question. Can a static member exist in an abstract class? If yes, why do we do so in general?
2. Can a private method in super class be overwritten in subclass? Thanks a lot.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Yes, you may have static member (fields/methods) in an abstarct class.
You can override a private method of the superclass in your subclass.However, the term override doesnot make sense in this context, as the subclass is not inheriting that method.
Hope this helps,
Sandeep

<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Howdy hoo!
1) Yes. An abstract class can have a static variable. And why we do so? Well, why do we use static variables in general?
2) A private method cannot be overridden in any subclass. Even so, this is possible:
class A {
private void a() {}
}
class B {
public void a() {}
}
This seems like an override, but a call to super.a() in B.a() would fail. This is because a() is private in A and cannot be seen outside this class, even though B inherits from A. This is how private always works!
a() method in class B is therefore a completely new method, and has not got anything to do with private method a() in class A.
Hope this helps!
/Kaspar

Ashwin Desai
Ranch Hand

Joined: Jul 17, 2000
Posts: 124
Hi,

Static Variables can be used in an abstract class to define constants that could be used througout the class hierarchy.
Ashwin.
SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
Thanks a lot
Rex Rock
Ranch Hand

Joined: Jun 25, 2001
Posts: 82
On the same token,
are private members not inherited or is it they are inherited but are not accessible???
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ashwin- static members can be used in abstract classes exactly as they can in regular classes. They don't have to be constants.
Rex- I'd say private members are inherited but inaccessible. The data in a private field is there in the JVM object, and if you deserialize the subclass object to a file, you can see the private parent data in the file, but you can't access that data from the child object. But depending on exactly what people mean by "inherited", other people may answer this differently. If a tree falls in a forest and no one is there to hear it, does it make a sound? It depends if you define "sound" as the vibration of air molecules, or the perceptions of such a vibration by a listener. Same problem here - if data is there but you can't use it directly, is it "inherited"?


"I'm not back." - Bill Harding, Twister
Joshua Bloch
Author and "Sun God"
Ranch Hand

Joined: May 30, 2001
Posts: 124
Originally posted by Jim Yingst:

I'd say private members are inherited but inaccessible...

Hi. Private members really aren't inherited (JLS 8.2, P. 148). The JLS defines the term carefully.
Regards,

------------------
Joshua Bloch
Author of Effective Java


Joshua Bloch <br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0201310058/ref=ase_electricporkchop" target="_blank" rel="nofollow">Effective Java</a> and coauthor of <a href="http://www.amazon.com/exec/obidos/ASIN/032133678X/ref=ase_electricporkchop" target="_blank" rel="nofollow">Java Puzzlers</a>
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Oops - quite right. Serves me right for bringing in silly philosophy questions.
SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
So, can i make a conclusion that when a method or a member of a class has private modifier, it means the member or method is not modifiable and accessible. Even we can give the same method name in the subclass, this method does not have anything to do with the same method with the same name (with private modifier) in the super class. Am i correct?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can abstract class has static member?