SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Chapter:
Object-oriented Design
Exercise:
Exercises
Question:8 | ISBN:9781118771334 | Edition: 6

Question

Can two interfaces mutually extend each other? Why or why not?

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Can two interfaces mutually extend each other? Why or why not?

 

short answer: No.


Two interfaces can not extend to each other, However an interface can extend multiple interfaces.

 

Why:

 

When an interface extends another interface, first one becomes child interface and the later becomes parent interface

in object oriented programming, parent interface doen't mean to inherit child interface methods, if parent interface extends child interface and vice versa, it throws error saying hierarachy of interfaces is inconsistent.

parent interface contains universal methods and child inteface have relatively specific methods.

 

Let's take an example for better understanding

 

public interface Animal {

  public String animalName();
  public void animalSound();
  public short animalAge();

}

public interface Bird extends Animal {
	public short flyingSpeed();
	public int flyingAltitude();
	public byte numberOfEggsLaid();
}

 

in the above example, Bird can extend Animal as all birds are animals and can have name, age, sound. Those are universal.

But Animal can't extends Bird. As It doesn't make sense for Dog to have flying speed, flying altitude, number of eggs it laid.

 

So that explains why two interfaces can't extend each other.

0 0

Discussions

Post the discussion to improve the above solution.