Specific Methods in Java1.8 Interfaces

During the discussion, one of my friends told me that concrete methods would be allowed in java 1.8 in interfaces , then at that time I came up with the question ie If they are allowed, how will we distinguish between the methods. For example, I have two interfaces Animal.java and Pet.java , and both have the same concrete method ie eat()

  public interfaces Animal{ void eat(){ System.out.println("Animal Start eating ...."); } } public interfaces Pet{ void eat(){ System.out.println("Pet Start eating ...."); } } 

Now my Zoo.java implements both of them and does not override

  public class Zoo() implements Pet , Animal{ //Now name method is a part of this class } 

Now here is my confusion. How can I name a specific method for inteface animal using the Test object

 public class Demo{ public static void main(String[] args){ Zoo zoo = new Zoo(); zoo.eat(); //What would be the output } } 

Any suggestions? or is there any solution for this in java1.8 since I cannot find his answer.

+7
source share
1 answer

You get a compile-time error, unless you cancel the power in your Zoo class.

 java: class defaultMethods.Zoo inherits unrelated defaults for eat() from types Pet and Animal 

The latest and geatest jdk is here btw . And the syntax should be

 default void eat(){ System.out.println("Animal Start eating ...."); } 
+5
source

All Articles