• 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

Static Methods in an Interface

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fail to understand Why we cannot have 'static methods' in an Interface?
We can have a static method in an abstract class then why not an Interface?
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can have implementation code in an abstract class but in an interface you only declare a method but cannot define it.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The implementation of a static method goes with the class, so you would have to implement it inside the interface, but as Arulk Pillai has told you, you mustn't implement methods in an interface.

The whole idea of an interface is to provide functionality which is implemented differently. A static method would be implemented the same in all instances.
 
jaspreet atwal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
The implementation of a static method goes with the class, so you would have to implement it inside the interface, but as Arulk Pillai has told you, you mustn't implement methods in an interface.



Yes, I understand that we cannot implement a method in an interface. But we can have a class define code for a static member in an interface. e.g:
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are not inherited - not from superclasses and not from interfaces.

That code doesn't work because it tries to inherit the static method from myInterface into myClass.
 
jaspreet atwal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:
Static methods are not inherited - not from superclasses and not from interfaces.

That code doesn't work because it tries to inherit the static method from myInterface into myClass.



I think they are inherited. Look at the code below. I can compile this and get an output:



 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In computer science, you have to define your terms very carefully. The Java Language Specification defines "inherited" carefully this doesn't count as inheritance.

The important thing is that for static methods, the compiler figures out while the program is being compiled where the static method is actually defined, and uses that class's name in the generated code. Even though you wrote "Test.method()", the bytecode that gets generated uses "SuperClass.method()".

Anyway, the real answer as to why this isn't allowed is that it's not. At some point you just have to accept that Java is what Java is; the folks who designed the language made some choices, and you have to live with those choices.
 
jaspreet atwal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
Anyway, the real answer as to why this isn't allowed is that it's not. At some point you just have to accept that Java is what Java is; the folks who designed the language made some choices, and you have to live with those choices.



Thanks for your reply Ernest! I can live with that...
I was just wondering if there is some logic behind all this, something that will help me keep this in my mind instead of cramming/memorizing it.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[EFH]: In computer science, you have to define your terms very carefully. The Java Language Specification defines "inherited" carefully this doesn't count as inheritance.

I believe if you look at the JLS definition, static methods are inherited. From JLS 6.4.3: "The members of a class type (�8.2) are classes (�8.5, �9.5), interfaces (�8.5, �9.5), fields (�8.3, �9.3, �10.7), and methods (�8.4, �9.4). Members are either declared in the type, or inherited because they are accessible members of a superclass or superinterface which are neither private nor hidden nor overridden (�8.4.8)." The italics on inherited indicate this is the JLS definition of the term. There's nothing here about a method or field being static - any accessible member of a superclass or superinterface is inherited as long as it's not private, hidden, or overridden. Static methods certainly qualify.

Of course this particular definition has no effect on the rest of this discussion, which has been addressed well enough by others already.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jaspreet atwal:


Thanks for your reply Ernest! I can live with that...
I was just wondering if there is some logic behind all this, something that will help me keep this in my mind instead of cramming/memorizing it.



Hmmm....

Jaspreet,

I have slightly different answer to this, [may be would help you to understand it and not cram],

Interface methods are already/implicitly abstract, i.e. there is no implementation available, and abstract and static can't go to-gather.
Lets assume its possible, now consider abstract static method declared in an interface "MyInterface", now this gives a great chance to the caller to call the method by doing "MyInterface.myStaticMethod()", right ? which ofcourse would lead no-where/doesn't make sense.

To block such usage, you will have to block "static" modifier to methods in interfaces.
BTW, this is the same case with abstract methods in an abstract class too. Once again, abstract and static can't go to-gather due to the reason above.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jaspreet,

static and abstract can not come together

example: abstract static void myMethod();//not possible

i.e, abstract (antonymous) static
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't possibly override a static method, which is something interface methods are all about.



Will print "A".
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Putting it in different words -
Even an abstract class cannot have a method which is static.
All these variations leads to the same question - "where can we define the implementation of the method".
Since static are tied with class/interface - if we cant implement the method in the class - we cant do so anywhere else - thats why compiler doesnt let us do so.
 
ashishs Sinha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops!! I meant static "abstract" method inside an abstract class
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Modified the code a little

This prints Hello B.

After reading the thread I expected it to print Hello A

Question for Ronald:
Doesn't this indicate that the printHello() method of A was overridden by B?


Question for Ernest:
If the compiler looks at b.printHello() as SuperClass.printHello() shouldn't it traverse all way up the hierarchy tree, until it did not find a SuperClass.printHello()?

Shikhar
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Doesn't this indicate that the printHello() method of A was overridden by B?

Not in the conventional sense no - static methods can do something called "shadowing". This means any class may possess a static method with the same name (but possibly with different return type and arguments) as any other class, and this is legal. The interpreter figures which one to invoke by the declared class type on which it is invoked. In your case, you're invoking it on a declared type of class B (b.printHello()). Hence it really invokes B.printHello() so you get the output "Hello B".

If this were true inheritence, it would print "Hello C" since the printHello() method in C would override that of B and, for instance methods it is the actual object type on which the method is invoked.
 
Charles Lyons
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may also find this topic useful: what linking mechanism uses java (early or late)
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see what you mean, it may be confusing to think that the static method is overriden, because:



Prints "A", while if class B would have its printStuff() method uncommented, the program would print "B". But this is because the mechanism that handles static methods is different than that which handles non static methods. In the above case, the reference is used to find the class that it is based on. The compiler searches up the class chain to see if it can find a match. Objects do not come into the picture here at all, and neither does the dynamic that occurs for non static methods at runtime that allows us to use polymorphism (invoking a method that behaves differently than the method defined in the class that the reference is based on, because the actual method found is one overriden by a subclass that the actual object was instantiated from, to which the reference is referring).

As Charles mentioned very well, this is shadowing instead of overriding.
 
Shikhar Madhok
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very interesting. I personally had not come across "shadowing" as a term in java until this thread.
Did I miss a few chapters, or is the term generally not used with java very often?

Anyway, will take it as a learning. Never thought abt the question though. Thanks a ton to the topic starter.

Shikhar
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shikhar Madhok:
Did I miss a few chapters, or is the term generally not used with java very often?

Have a look through your book and see how much is actually printed about shadowing and overriding.
This delightful little book has some related puzzles (no 71, 73, 79, etc) in.
 
Charles Lyons
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Did I miss a few chapters, or is the term generally not used with java very often?

It's not used that often. In practise almost all of what you do with Java will (and should) be OOP - so you'll not use static methods and variables much, less in such confusing situations.

Shadowing is the official terminology - as mentioned in the Java Language Specification: 6.3.1 Shadowing Declarations
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In .NET shadowing (I think) is where you have a variable that has the same instance name as a default function/varaible name (maybe only one case).

does that apply for java as well?

Justin
 
Charles Lyons
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is the same definition of shadowing in .NET: that a method or variable in the subclass 'masks' one of the same name from a superclass from the compiler and is referenced in preference to the superclass one. However, .NET provides more functionality than Java in that respect - like the VB.NET "Shadows" keyword which also allows instance methods to be shadowed rather than overridden by inheritance (though I'm not sure why anyone would want to do that really).

In Java as in C++, (both instance and static) variables can only shadow each other - there is no inheritance of variables. The idea of shadowing methods in both Java and .NET is basically using non-virtual C++ methods, while overriding instance methods by inheritance is equivalent to virtual C++ methods, if that helps at all.
[ July 02, 2008: Message edited by: Charles Lyons ]
 
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was extremely helpful. I am referring it after 2 years or so . Thank you everyone
reply
    Bookmark Topic Watch Topic
  • New Topic