oop - Java Multiple Inheritance -


In an attempt to understand completely how to solve many heritage problems of Java, I have a classic question that makes me clear should do.

Let's say that I have sub class birds and horses in class animal and I have a class Pegasus is a bird and a horse from both bird and horses to pegasus .

I think this is the problem of classic diamond animal , bird and horses creating class interfaces and Pegasus from them

I was thinking that there is another way to solve this problem in which I can still make things for birds and horses. There was also a way to be able to make animals which would be very good but would not be necessary.

You can create interfaces for animal classes (class in biological sense), such as Public Interface Compound

and then

  P> 
  public class horse commissions {}   

and

 < Code> public class implementation of the Pegasus avialaya, Equity {}>  

Linking to the plates:

To reduce the duplicate code, you can create an abstract class that contains the general code of those animals that you want to apply.

  Public abstract class AbstractHorse implemented Equidae {} Expansion of public square horse AbstractHorse {} Expansion of the public class poet's excellence Sarasher Aubiali {}   

I want to add another extension. As, it opens the OP The only thing I knew was.

However, I want to emphasize that I recommend bypassing the "multi-heritage" problem with the interface and I do not recommend using the interface which is already a solid Type (e.g. Bird) represents but more behavior (other people refer to duck-typing, which is also good, but I mean: biological class of birds, aviala). I also do not recommend using 'I' such as the interface names starting with IBird , which just tells you why you do not need an interface. There is a difference for this question: Using the interface, build a heritage hierarchy, use useful sections, where necessary, where to apply solid sections and if possible, use the delegation.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -