File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract method" Watch "abstract method" New topic
Author

abstract method

saravanan ragunathan
Ranch Hand

Joined: Aug 02, 2010
Posts: 84
why abstract method can't be static



"I Love Java Ranch"
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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


Mohamed Sanaulla | My Blog
saravanan ragunathan
Ranch Hand

Joined: Aug 02, 2010
Posts: 84
thanks Mohamed Sanaulla for your quick reply

saravanan ragunathan
Ranch Hand

Joined: Aug 02, 2010
Posts: 84
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

Joined: Sep 08, 2007
Posts: 3071
    
  33

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

Joined: Aug 02, 2010
Posts: 84
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

Joined: Mar 08, 2010
Posts: 222

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


Personal page, SCJP 6 with 91%, SCWCD 5 with 84%, OCMJD
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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

Joined: Jan 28, 2008
Posts: 5575

Elchin Asgarli wrote:static + abstract = impossible

you know nested interfaces are static implicitly!
Elchin Asgarli
Ranch Hand

Joined: Mar 08, 2010
Posts: 222

Seetharaman Venkatasamy wrote:you know nested interfaces are static implicitly!

Yes, small correction, what I said applies only to methods
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


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


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Elchin Asgarli
Ranch Hand

Joined: Mar 08, 2010
Posts: 222

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

Joined: Jan 28, 2008
Posts: 5575

Javin Paul wrote:
Where do you see the usage for interface inside interface

java.util.Map.Entry
Elchin Asgarli
Ranch Hand

Joined: Mar 08, 2010
Posts: 222

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

Joined: Aug 02, 2010
Posts: 84
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

Joined: Mar 08, 2010
Posts: 222

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

Joined: Aug 02, 2010
Posts: 84
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

Joined: Sep 08, 2007
Posts: 3071
    
  33

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

Joined: Aug 02, 2010
Posts: 84


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

Joined: Jun 23, 2010
Posts: 432


static + abstract = impossible

abstract +static can be only possible in case of inner classes.


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract method