1

I am learning java and confused about interface inheritance. example from a book

 public interface Singer {
      void sing();
      void setRate(double rate);
      double getRate();  
  }  

  public interface Player {
      void play();
      void setRate(double rate);
      default double getRate() {
         return 300.0;
      }
  }

public interface SingerPlayer extends Singer, Player{
      // Override the getRate() method with a default method that calls the
      // Player superinterface getRate() method
     @Override
     default double getRate() {
          double playerRate = Player.super.getRate();
          double singerPlayerRate = playerRate * 3.5;
          return singerPlayerRate;
     }
 }


public class Employee implements Singer, SingerPlayer {

}

The books says Employee will inherit SingerPlayer.setRate() method because it overrides the Singer.setRate() method.

I do not see where it overrides this method.

6
  • 1
    By default, methods in interface are abstract and abstract methods do not specify a body. So first of all you have to correct 'Player' interface. Commented Jun 24, 2016 at 12:30
  • 1
    @Kartic Default methods in interfaces are a thing now tutorialspoint.com/java8/java8_default_methods.htm Commented Jun 24, 2016 at 12:32
  • Oh, yeah! My mistake. Got to know just now. Commented Jun 24, 2016 at 12:34
  • Player interface has default method getRate and the compiler does not complain about that. I understand default methods. Commented Jun 24, 2016 at 12:35
  • I agree that at the moment SingerPlayer does not override setRate(). Maybe the book just meant that, in the event that it did override that method, then Employee would inherit that overridden version. Just checking, but does the book definitely say setRate() and not getRate()? Could be a misreading (happens to me a lot), or even a typo in the book. Commented Jun 24, 2016 at 12:38

1 Answer 1

1

getRate() in SingerPlayer will override getRate() in Singer. And SingerPlayer inherits getRate(), sing() and play().

Sign up to request clarification or add additional context in comments.

2 Comments

Which setRate method does Employee inherit?
A class has to provide a body. And since setRate is the same in Both interfaces Employee needs to only implement void setRate(double rate).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.