Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Inheritance of static method in interface  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


interface Jumpable {
    int height = 1;
    default void  worldRecord(){
        System.out.print(height);
    }
}

interface Moveable{
     int height =2;
    static void worldRecord(){
        System.out.print(height);
    }
}

public class Chair implements Jumpable, Moveable{
    int height = 3;
    Chair() {
        worldRecord();
    }
    public static void main(String[] args){
        Jumpable j = new Chair();
        Moveable m = new Chair();
        Chair c = new Chair();
    }
}

this code prints 111 , why the variable height in class Chair is not used?
 
Saloon Keeper
Posts: 1800
74
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Jilson.

I hope that you find the community here welcoming, knowledgeable, friendly and sharing.

You get 111 due to the fact that static methods belong to interfaces. If you wanted to call the worldRecord() method from the Moveable interface you would need to do something likeThe current code is calling the default instance method of Jumpable interface.

Default methods of interfaces are inherited in classes and must be called from class instances. Default methods can also be overridden.
Static methods of interfaces are not inherited in classes and must class from interfaces. Static methods can not be overridden.

Here is an Oracle tutorial attempting to explain both Static and Default methods in interfaces https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html which may help you out.

Looks like you attempted to use the code tags when posting your question, however it did not work out as expected.
Here is a quick write up on how codes can be used https://coderanch.com/wiki/659781/Code-Tags
 
Jilson Jo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pete Letkeman wrote:Welcome to CodeRanch Jilson.

I hope that you find the community here welcoming, knowledgeable, friendly and sharing.

You get 111 due to the fact that static methods belong to interfaces. If you wanted to call the worldRecord() method from the Moveable interface you would need to do something likeThe current code is calling the default instance method of Jumpable interface.

Default methods of interfaces are inherited in classes and must be called from class instances. Default methods can also be overridden.
Static methods of interfaces are not inherited in classes and must class from interfaces. Static methods can not be overridden.

Here is an Oracle tutorial attempting to explain both Static and Default methods in interfaces https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html which may help you out.

Looks like you attempted to use the code tags when posting your question, however it did not work out as expected.
Here is a quick write up on how codes can be used https://coderanch.com/wiki/659781/Code-Tags



Thanks !
yeah, about the code tags, that was little stupid of me!

ok now i get it default method from interface Jumpable is only inherited to the class , but why the inherited  method worldRecord() uses variable height from the interface Jumpable even though there is variable height already available in the class?  and why height variable from the interface Movable and Jumpable  not hidden in class as a new height variable is declared in the class? (i assume variables are by default static in interface).
 
Pete Letkeman
Saloon Keeper
Posts: 1800
74
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jilson Jo wrote:i assume variables are by default static in interface

I think a little history lesson may help out here.
Before Java 8 there was no such thing as "default" methods in Java interfaces.
This meant that variables were static and final and all methods were abstract and needed to be overridden.
With Java 8, default methods were added to interfaces to give some code/libraries new functionality.
All variables in declared in an interface are still static and final, variables declared in default methods inside of interfaces are not.

As to why/how the use of the variable height is used from the static method there are a few other things to keep in mind.
Generally speaking first, when Java tries to find the variable it looks for it in the closest code block and moves out from there a code block at a time.
There is also the order on initialization for variables which you should know. For instance, do you know if static variables are initialized before or after instance variables?

The interface Jumpable defines it's own version of height just before the default method is defined so that in is used.
In fact the Jumpable interface has no idea about any other variables which are not defined in the interface. This has to do with variable/member scope.
If you wanted to let the default method know about a variable defined outside of the interface scope you would need to do something likeIf you were to use codeYou would get an error saying something about height being an undefined variable. This code snippet may not produce the expected result either. I encourage you to try it out as you could defiantly see something like this on the exam.

Oh and don't beat yourself up about the code tags. We all make mistakes...well I know that I make mistakes at least.
I do suggest that before posting you press the "Preview" button which is next to the "Submit" button to see if your posting looks and reads the way you want it to.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!