• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

abstract and static

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can we have abstract and static methods? why not?
can we override static methods?
thanks.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
In the first place when u declare a method/variable as static u are saying "no matter what happens this thing belongs to my class let go others use my instance variables".
static methods boast they are independent of any instance of a class ..ie..all the objects share one copy of this method. whereas while defining abstract method its like saying " i'm not sure for what this method can be used for but i'm sure it will be of some use" and thus u defer its implementation to subclasses.

i'm a bit sceptic abt the other part of your question ..overriding static ..
i think static methods can be overloaded and cannot be overriden.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there:
Let us deal with abstract and static separately for a minute:
abstract: When a method is declared abstract it mean that in this class we do not know how this method will be coded. However we know inputs and outputs and their types and any exception thrown. Beside that it is a black box in this class. However, any non-abstract sub-class will override ( not shadow -- two different things)it and provide the code while honouring its declaration in super abstract class.
static: These methods provide code as well as their declaration. However, a sub-class can only shawdow (not override) it and provide different code.
As static methods can not be overridden while abstract have to be overridden, therefore compiler will not allow method that is declared abstract and static at the same time.
The difference between overriding and shawding is very subtle but very important. If your reference type is animal, and at run time you pass an object whose type is cat while reference type is still animal, the compiler will be nice enough to call method from cat class and not from animal class. This is done only if you have overridden the method in cat. If it is not overridden but shadowed, the method from animal class will be called.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barkat:
In the above you give a good explanation except that you mean "hiding", not "shadowing". There are four ways in which one entity gets eclipsed by another:
  • Overriding - only applies to instance methods: the VM decides at runtime which method to invoke based on the object's type. Note this is an inheritance issue.
  • Hiding - applies to static methods and all member variables: the compiler decides which method or variable to reference based on declared type. This is also an inheritance issue.
  • Shadowing - a field, method, label, or type can be "shadowed" by another declaration within its scope, e.g., a local variable shadowing an instance variable, or a specific-type import statement shadowing a type declared elsewhere. Note that this is scope, not inheritance.
  • Obscuring - a package can be obscured by the declaration of a type or variable with the same name, and a type can be obscured by the declaration of a variable with the same name. This is about scope and name collision between different types of entities.
  •  
    Barkat Mardhani
    Ranch Hand
    Posts: 787
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Steve,
    Thanks for pointing it out. I had two books close to me right now. K&B calls it declaring or redefinging what I called shawdowing. Java in Nutshell by David Flanagan calls it 'class method shawdoing'.
    In many threads in this forum it is referred to as shadowing, if I recall correctly.
    So I looked up the JSL. Lo and Be hold, it calls it hidding as you pointed out.
    I guess I learned something new today. thanks
    Barkat
     
    What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic