• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Can abstract class has static member?

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the same token,
are private members not inherited or is it they are inherited but are not accessible???
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"?
 
Author and "Sun God"
Posts: 185
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops - quite right. Serves me right for bringing in silly philosophy questions.
 
SoonAnn Lim
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic