12

During a discussion one of my friend tell me that concrete methods would be allowed in java 1.8 in interfaces then at that time a question came into my mind i-e If they are allowed then How will we distinguish the methods.For example
I have two Interface Animal.java and Pet.java and both have same concrete method i-e 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 implement both of these and didn't override

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

Now here is my confusion.How can I call a specific method on inteface animal using 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 as I am unable to find its answer.

Freak
  • 6,721
  • 5
  • 35
  • 51
  • but I didn't find this answers in these question.If it is a duplicate then please guide me regarding this issue resolvation – Freak Apr 12 '13 at 10:34
  • this might be of itnerest too, static non-inherited methods in interfaces http://stackoverflow.com/q/14646027/106261 – NimChimpsky Apr 12 '13 at 10:36
  • your original question is answered here http://stackoverflow.com/a/7857884/106261 – NimChimpsky Apr 12 '13 at 10:36
  • @NimChimpsky may be its my bad but still i am not able to find (1)what would be the output of `zoo.eat();` and (2) How can I call the interface specific method? – Freak Apr 12 '13 at 10:42
  • @freak yoru syntax is wrong btw I will try it out usign jdk 8, give me 5 mins – NimChimpsky Apr 12 '13 at 10:45
  • @NimChimpsky ok yes I also want to know is there any syntax changing or what would be the real syntax for this method calling.by the way thanks :) – Freak Apr 12 '13 at 10:50
  • I've attempted to answer your question to your very good and specific question with an example. Please follow the link: stackoverflow.com/q/14646027/106261and – Mark Burleigh Dec 26 '18 at 11:48

1 Answers1

11

You get a compile time error, unless you override eat 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 ....");
}
Pradeep
  • 3,262
  • 1
  • 21
  • 35
NimChimpsky
  • 44,999
  • 57
  • 192
  • 304