Jilson Jo

Greenhorn
+ Follow
since Jun 15, 2018
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jilson Jo

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).
2 years ago


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?
2 years ago