Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract method

 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why abstract method can't be static

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There should be similar questions asked on this forum. I did a search on google for the same question and this is the link from the search results- http://stackoverflow.com/questions/370962/why-cant-static-methods-be-abstract-in-java
 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Mohamed Sanaulla for your quick reply

 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By reading several forums i understand that
static method is not inherited and we can't
override it... this is just opposite to abstract method
which must be overridden..so we can't use abstract
with static ...
but is there any reason to avoid static method from
inherit to subclass and overridding ?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saravanan ragunathan wrote:
but is there any reason to avoid static method from
inherit to subclass and overridding ?


Please read:OverridingVsHiding
 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i am clear that we can't override static method..
but we can hide any implementaion of any static method
in super classes whithin subclasses..

still i have a doubt that why static method is not inherited..
i think its scope is only within the class in which it is declared(encapsulation)

is this right???
 
Elchin Asgarli
Ranch Hand
Posts: 222
Chrome Eclipse IDE Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static method - can't be overriden, since they belong to given class, thus they are not dynamically invoked.
abstract methods - have to be overriden

thus:

static + abstract = impossible
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saravanan ragunathan wrote:
still i have a doubt that why static method is not inherited..
i think its scope is only within the class in which it is declared(encapsulation)

is this right???


They are inherited. Did you try the code given in the link I had provided in the last post? You can change the example and see if they are inherited or not.

And declaring it as static doesn't affect the scope. The scope depends on declaring it as- public, private, protected or default.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Elchin Asgarli wrote:static + abstract = impossible

you know nested interfaces are static implicitly!
 
Elchin Asgarli
Ranch Hand
Posts: 222
Chrome Eclipse IDE Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:you know nested interfaces are static implicitly!

Yes, small correction, what I said applies only to methods
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Where do you see the usage for interface inside interface, nested class does make sense if Inner or nested class is part of outer class but with the interface ............. , let me know if you come across any convincing scenario ?

Thanks
Javin
 
Elchin Asgarli
Ranch Hand
Posts: 222
Chrome Eclipse IDE Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javin Paul wrote:
Where do you see the usage for interface inside interface, nested class does make sense if Inner or nested class is part of outer class but with the interface ............. , let me know if you come across any convincing scenario ?

Thanks
Javin


This is theoretically feasible in Java, however in real-world situations you will mostly get fired for such a perverted code. Or at least you should get fired..
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javin Paul wrote:
Where do you see the usage for interface inside interface

java.util.Map.Entry
 
Elchin Asgarli
Ranch Hand
Posts: 222
Chrome Eclipse IDE Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:java.util.Map.Entry


I'd say that was a bad OOP usage, it would be way better to put Entry class outside. That interface for example could also server as a skeleton to Pair class, a structure that holds two objects of any type, something like http://www.sgi.com/tech/stl/pair.html
 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks gentlemans...
i understand that static method can't be overridden because
jvm takes only reference type not an real object they refers

so static methods are used for method hiding..
but same rules are applied to method hiding as in overridding
like (can't mention weaker privileges,can't mention
different return type)

why?
 
Elchin Asgarli
Ranch Hand
Posts: 222
Chrome Eclipse IDE Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saravanan ragunathan wrote:thanks gentlemans...
i understand that static method can't be overridden because
jvm takes only reference type not an real object they refers

so static methods are used for method hiding..
but same rules are applied to method hiding as in overridding
like (can't mention weaker privileges,can't mention
different return type)

why?


Not really, static methods are class-wide methods, and not instance-wide methods. First you have to think of difference between a class and an instance of that class, thus instance methods are methods for instances of a class, and static methods are for class itself. For example this is the reason you cannot call instance methods inside static methods, because in static methods there is no instance, it is class-wide.
 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really, static methods are class-wide methods, and not instance-wide methods. First you have to think of difference between a class and an instance of that class, thus instance methods are methods for instances of a class, and static methods are for class itself. For example this is the reason you cannot call instance methods inside static methods, because in static methods there is no instance, it is class-wide.


i know that static=class level
non static =instance level

my doubt is why they apply the same rules(can't mention
weaker privileges,can't mention different return types)
for method hiding as in method overridding...
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saravanan ragunathan wrote:
my doubt is why they apply the same rules(can't mention
weaker privileges,can't mention different return types)
for method hiding as in method overridding...


If the rules of overriding are not followed- then it becomes method overloading and that will surely not participate in the kind of polymorphism we are talking when we consider overriding.
 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If the rules of overriding are not followed- then it becomes method overloading and that will surely not participate in the kind of polymorphism we are talking when we consider overriding.


thanks for your answer...

 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

static + abstract = impossible

abstract +static can be only possible in case of inner classes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic