oop - Inheritance vs Object Creation in Java -


why use inheritance, when can create objects of other class , use it's methods , instance variables? (also vice versa- why create objects of other class when can use inheritance?)

this not homework question. asked question teacher when teaching inheritance , didn't know answer. asked tuition teacher , couldn't me. read textbook multiple times, searched internet simple explanation, had no luck. please me.

inheritance , composition both can used alternatively in code reuse-ability.

why composition preferred on inheritance? preferable use composition rather inheritance serves purpose of code reuse-ability without having need worry trickle down effect of change. provides more loosely coupling not have worry changing code causing other dependent code requiring change.

for example: consider have following classes:

    class person     {         public string name;         public int age;          public person(string name,int age)         {             this.name = name;             this.age = age;         }     }       class student : person     {         public string rollnumber;         public double cgpa;           public student(string rollnumber,double cgpa,string name,int age):base(name,age)         {             this.rollnumber = rollnumber;             this.cgpa = cgpa;         }     }      //and main call these     static void main(string[] args)     {         student student = new student("roll1234", 3.5, "john doe", 32);     } 

now if person class change , following:

class person {     public string firstname;     public string lastname;     public int age;      public person(string firstname, string lastname, int age)     {         this.firstname = firstname;         this.lastname = lastname;         this.age = age;     } } 

this change cannot achieved without changing classes inherit person class, in our class student class.

now consider case in have used composition in our following code:

class person {     public string name;     public int age;      public person(string name,int age)     {         this.name = name;         this.age = age;     } }  class student  {     public string rollnumber;     public double cgpa;      public person person;      public student(string rollnumber,double cgpa,person person)     {         this.rollnumber = rollnumber;         this.cgpa = cgpa;         this.person = person;     } }  //and main static void main(string[] args) {     person person = new person("john doe", 32);     student student = new student("roll1234", 3.5, person); } 

now in case if make change in person class follows:

class person {     public string firstname;     public string lastname;     public int age;      public person(string firstname, string lastname, int age)     {         this.firstname = firstname;         this.lastname = lastname;         this.age = age;     } } 

we not require change student class @ all. composition makes our design more flexible when comes change.

however rule of thumb stands, if there "is a" relation go inheritance , go composition "has a" relation.

yet keep in mind if want achieve code reuse composition better choice.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -