• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Static methods in interfaces

 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:and actually that is why abstract class cannot have private, static or final methods because you couldn't override them by subclass. I know because I have and read Java in a Nutshell book



Hi,

What you say is true for interfaces, not for abstract classes. As a matter of fact, interfaces don't even have to declare their methods or fields public because they are all public by default.
 
Saloon Keeper
Posts: 14499
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Original thread: https://coderanch.com/t/656741/java/java/Creating-objects-abstract-classes
 
Sheriff
Posts: 8523
615
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A.J. Côté wrote:What you say is true for interfaces, not for abstract classes.

You're not fully correct. And what is the interface? It is abstract entity by default, implicitly. All methods in interface are implicitly abstract too. So I would say you're not fully correct what you said.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

A.J. Côté wrote:What you say is true for interfaces, not for abstract classes.

You're not fully correct. And what is the interface? It is abstract entity by default, implicitly. All methods in interface are implicitly abstract too. So I would say you're not fully correct what you said.



When in doubts, you can always test it for yourself before arguing. Never mind I did it for you ;-)

Notice the interface and the class are in different packages. All interface fields and methods are public by default!





output: test String


Here is a more complex example that shows you that even if the interface is package only all fields and method are still public! In fact, some consider it as bad practice to declare fields and method public in interfaces because they always are!







output: test String

 
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


A.J. Côté wrote:[What you say is true for interfaces, not for abstract classes. As a matter of fact, interfaces don't even have to declare their methods or fields public because they are all public by default.



no A.J.Cote
its not true for either interface or abstract class because
interface can have static methods
and one thing already have been pointed out by you.(public thing)
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And of course, these won't compile because you can't reduce the visibility of inherited public methods:





 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote: It is abstract entity by default, implicitly. All methods in interface are implicitly abstract too. So I would say you're not fully correct what you said.




Sorry but it wrong or more precisely outdated because its really not necessary for a method to be abstract to be in interface
default and static methods had found their way in the picture a long before.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Tripathi wrote:

A.J. Côté wrote:[What you say is true for interfaces, not for abstract classes. As a matter of fact, interfaces don't even have to declare their methods or fields public because they are all public by default.



no A.J.Cote
its not true for either interface or abstract class because
interface can have static methods
and one thing already have been pointed out by you.(public thing)



Please test for yourself before arguing on the forum, what I say is true. Interfaces cannot have static methods. They can have static fields and it is actually good practice to declare constants in Interface but they are still public by default, see my other examples above:

In fact, some consider it as bad practice to declare fields and method public in interfaces because they always are!










output: test String

This does not compile:


 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is so nice of you to ask me to check myself as this leads me checking javase tutorial again,where I found this and thought of sharing with you all:Static Methods

In addition to default methods, you can define static methods in interfaces. (A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.)

So try to be cool and enjoy coding
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Tripathi wrote:It is so nice of you to ask me to check myself as this leads me checking javase tutorial again,where I found this and thought of sharing with you all:Static Methods

In addition to default methods, you can define static methods in interfaces. (A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.)

So try to be cool and enjoy coding



Could you please provide an example of what you are saying that compiles? Books are great but ultimately it has to compile.

Thank you in advance,
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This compiles for me (in Java 8):

 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:This compiles for me (in Java 8):



I see, I just tested and only supported in java 8, I changed my compliance settings. Nice, thanks ;-)

This still hold although:


In fact, some consider it as bad practice to declare fields and method public in interfaces because they always are!









output:
test String
test String
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A.J. Côté wrote:Could you please provide an example of what you are saying that compiles? Books are great but ultimately it has to compile.

Thank you in advance,



With a little modification in your code,even your code compiles and runs:


OUTPUT:


Do you want more examples?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Tripathi wrote:With a little modification in your code,even your code compiles and runs:


Ooof. I thought I'd heard something about interfaces allowing static methods now; and I have to admit that my first thought was: "at last".
But looking at that code, I'm not so sure now.

It seems to me that you could accidentally "overload" a method more easily now without meaning to (and yes I know that, strictly speaking, it's not overloading; but the effects are similar).

Can someone give me an example of when it might be a really good thing to do?

Winston
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quoting javase tutorial:

static method in interface makes it easier for you to organize helper methods in your libraries; you can keep static methods specific to an interface in the same interface rather than in a separate class.



for example i don't think i can explain better than this article
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:Ooof. I thought I'd heard something about interfaces allowing static methods now; and I have to admit that my first thought was: "at last".
But looking at that code, I'm not so sure now.

It seems to me that you could accidentally "overload" a method more easily now without meaning to (and yes I know that, strictly speaking, it's not overloading; but the effects are similar).


I had clearly mentioned in my above posts that interfaces allow static methods ,now.
I just tried to modify A.J Cote code, so that he could easily understand (repeating again)interfaces allow static methods

As i have always been told that:One learns quickly,when explained in his/her own words
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Tripathi wrote:As i have always been told that:One learns quickly,when explained in his/her own words


And my post was no criticism of your effort - in fact it highlighted a (potential) problem that I wasn't aware of before.

It's funny that many of the recent developments to Java are what I would term "double-edged swords" - good for some things, but not necessarily for everything - and I wonder if this isn't one of them.

There's always a trade-off between old farts like me who say: "we managed without it for 15 years, and life was a lot simpler", and those who believe - as I also do, within reason - that Java needs to keep abreast of the times in order to remain relevant.

Josh Bloch, when is "Effective Java version 8" coming out?

Winston
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Tripathi wrote:

A.J. Côté wrote:Could you please provide an example of what you are saying that compiles? Books are great but ultimately it has to compile.

Thank you in advance,



With a little modification in your code,even your code compiles and runs:



Congratulations but if you had read the thread before posting this reply, you would have seen that I already posted an example that compiles about 12 hours ago thanks to Paul Clapham who warned me that I needed java 1.8 compliance settings for this to compile (which you should have done yourself in the first place)

Nevertheless, it is going to take a while for me to start declaring static methods in interface because as opposed to books, in real life, many of my corporate customers are not running java 1.8 yet and they will just laugh at me if I tell them that they need to upgrade to 1.8 in order to run my code. A lot of times, upgrading is not your decision and there is nothing you can do about it.

I use java 1.8 but I set my IDE to java 1.6 and even java 1.5 compliance and you need 1.8 compliance in order for this to compile and run....

Cheers,
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


A.J. Côté wrote:
Congratulations but if you had read the thread before posting this reply, you would have seen that I already posted an example that compiles about 12 hours ago thanks to Paul Clapham who warned me that I needed java 1.8 compliance settings for this to compile (which you should have done yourself in the first place)


yes,i admit ,i posted even after reading your reply ,because you yourself asked for it

A.J. Côté wrote:Could you please provide an example of what you are saying that compiles? Books are great but ultimately it has to compile.


So i was not able to stop myself from giving you the example.

As: Lannisters are not the only one who always pay their debts,and do whatever they are asked for .

A.J. Côté wrote:
Nevertheless, it is going to take a while for me to start declaring static methods in interface because as opposed to books, in real life, many of my corporate customers are not running java 1.8 yet and they will just laugh at me if I tell them that they need to upgrade to 1.8 in order to run my code. A lot of times, upgrading is not your decision and there is nothing you can do about it.

I use java 1.8 but I set my IDE to java 1.6 and even java 1.5 compliance and you need 1.8 compliance in order for this to compile and run....



It is debatable, whatever you need at your work is something different from whatever you need to know.You might not be using it,but you should be knowing it.
Because for me:knowledge pays you the best.

Happy learning!!
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Tripathi wrote:

It is debatable, whatever you need at your work is something different from whatever you need to know.You might not be using it,but you should be knowing it.
Because for me:knowledge pays you the best.

Happy learning!!



I agree, Here's the link... ;-)

Thanks for bringing it up so I now know about it!

P.S. Did you know it was only available in 1.8 when you posted your first reply on this thread?
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


A.J. Côté wrote:P.S. Did you know it was only available in 1.8 when you posted your first reply on this thread?


Yup,i was knowing that since 1.8 is released.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Tripathi wrote:

A.J. Côté wrote:P.S. Did you know it was only available in 1.8 when you posted your first reply on this thread?


Yup,i was knowing that since 1.8 is released.



Well then in the future, please specify it. I searched this thread and you never mentioned it. Paul Clapham mentioned it first. Don't take for granted everybody has time to look at all the nifty features when a new java version is released.

This will avoid confusion.

Thanks in advance!

Cheers,
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A.J. Côté wrote: nifty features


Yes surely,I will.
Sorry I just love being updated(
 
Campbell Ritchie
Marshal
Posts: 76862
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A.J. Côté wrote:. . . Don't take for granted everybody has time to look at all the nifty features when a new java version is released. . . .

It is one of the big features of Java8 which makes Streams possible. It is surprising that you missed it.
 
Paul Clapham
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A.J. Côté wrote:in real life, many of my corporate customers are not running java 1.8 yet



Now that I'm retired I have the freedom to use whatever Java version I please. But that used to be me too... "We should really upgrade to Java 6 soon... Okay, that means we have to upgrade Websphere to version (whatever it was)... OMG, a Websphere upgrade? Can we tentatively pencil that in for next year some time?"
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At work we use Java 8 and the latest JBoss when we start a new project, and we switch when we have to upgrade anyway, but yeah, a lot of projects stick to Java 7...
 
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A.J. Côté wrote:In fact, some consider it as bad practice to declare fields and method public in interfaces because they always are!





By that logic, surely it should be considered bad practice to declare fields in interfaces static and final as well, as they are implicitly declared so anyway.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A.J. Côté wrote:Don't take for granted everybody has time to look at all the nifty features when a new java version is released.



Well, to be fair, it has been 18 months.
Even I, grumpy old git that I am, have managed to pick up on most of it in that time, if only via slacking off keeping up to date on forums like this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic