Showing posts with label inheritance. Show all posts
Showing posts with label inheritance. Show all posts

Sunday, July 28

Item 16 : Favor composition ovr inheritance


                 Favor composition over inheritance


How inheritance can be dangerous ?

 
Lets take an example

 public class X{

   public void do(){

    }

}

Public Class Y extends X{


   public void work(){

do();

}

}


1. As clear in above code , Class Y has very strong coupling with class X. If anything changes in superclass X , Y may break dramatically. Suppose In future class X implements a method work with below signature

public int work(){
}


Change is done in class X but it will make class Y uncompilable. SO this kind of dependency can go up to any level and it can be vary dangerous. Every time superclass might not have full visibility to code inside all its subclasses and subclass may be keep noticing what is happening in suerclass all the time. So we need to avoid this strong and unnecessary coupling.


How does composition solves this issue?

Lets see by revising the same example

public class X{

public void do(){

}

}

Public Class Y{
X x=new X();

public void work(){

x.do();

}

}


Here we are creating reference of X class in Y class and invoking method of X class by creating an instance of X class.
Now all that strong coupling is gone. Superclass and subclass are highly independent of each other now. Classes can freely make changes which were dangerous in inheritance situation.


2. Second very good advantage of composition in that It provides method calling flexibility For example

class X implements R
{}
class Y implements R
{}

public class Test{

R r;

}


In Test class using r reference I can invoke methods of X class as well as Y class. This flexibility was never there in inheritance

3. Another great advantage : Unit testing

public class X{

public void do(){

}

}

Public Class Y{
X x=new X();

public void work(){

x.do();

}

}


In above example If state of x instance is not known ,it can easily be mocked up by using some test data and all methods can be easily tested. This was not possible at all in inheritance As you were heavily dependent on superclass to get the state of instance and execute any method.

4. Another good reason why we should avoid inheritance is that Java does not support multiple inheritance.

Lets take an example to understand this :


Class Deposit implements Banking{

 boolean deposit(){
}
}

class Credit implements Banking{
boolean credit(){
}
}
Public class Transaction {

}


In class Transaction , for complete transaction I have to invoke both deposit as well as credit method. I can't inherit both the classes to achieve this . But I can easily use composition to make it happen simply by doing this :

Public class Transaction {
Banking b;
public static void main(String a[])

{

  b=new Deposit();

   if(b.deposit()){

     b=new Credit();
     c.credit();

    }
 }
}




Good to know : 

        1. composition is easily achieved at runtime while inheritance provides its features at                      compile time 

        2. composition is also know as HAS-A relation and inheritance is also known as IS-A                     relation


So make it a habit of always preferring composition over inheritance for various above reasons.