Skip to main content

Posts

Showing posts with the label inheritance

Inheritance vs. composition depending on how much is same and how much differs

I am reading the excellent Django book right now. In the 4th chapter on Django templates , there is an example of includes and inheritance in Django templates. Without going into details about Django templates, the include is very similar to composition where we can include the text of another template for evaluation. Inheritance in Django templates works in a way similar to object inheritance. Django templates can specify certain blocks which can be redefined in subtemplates. The subtemplates use the rest of the parent template as is. Now we have all learned that inheritance is used when we have a is-a relationship between classes, and composition is used when we have a contains-a relationship. This is absolutely right, but while reading about Django templates, I just realized another pattern in these relationships. This is really simple and perhaps many of you may have already have had this insight... We use inheritance when we want to allow reuse of the bulk of one object in other ...

Inheritance

Logging frameworks like Log4J allow us to send logs to a multitude of destinations. The code responsible for logging to a destination is usually encapsulated into seperate classes. Without inheritance With inheritance When to use inheritance: IS-A relationship An Employee is a Person A BumperSale is a Sale A Square is a Shape IS-LIKE-A relationship But if the relationship between them is HAS-A (eg: Car has an Engine), then we use composition instead of inheritance. Reflections: What is the relationship between the following objects? Bathroom - Bathtub Car - Engine Person - Professor Vehicle - Car Discuss this post in the learning forum . Note: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net   Commercial Links Buy programming books from our Amazon.com powered storefront . Earn as you browse. Sign up for Algoco .

Using inheritance and composition

Inheritance is used for Code resuse To support polymorphism Disadvantages of inheritance Difficult to change the interaface of the subclass Superclass is always instantiated Can get complex and unmanageable Composition is used for code reuse Disadvantages of composition We cannot use polymorphism We cannot extend the system by adding subclasses May have a slight performance overhead Usage Inheritance: IS- A Composition: HAS - A Example  1 :  public class  Car  {      private  Engine engine;      public  void  start () {}      public  void  stop () {} }  public class  SUV  extends  Car {      public  void  start () {}  //overrides the start method from Car        public  void  fourByFourMode () {} } Examp...

Inheritance

Logging frameworks like Log4J allow us to send logs to a multitude of destinations. The code responsible for logging to a destination is usually encapsulated into separate classes. Without inheritance With inheritance When to use inheritance: IS-A relationship An Employee is a Person A BumperSale is a Sale A Square is a Shape IS-LIKE-A relationship But if the relationship between them is HAS-A (eg: Car has an Engine), then we use composition instead of inheritance. Reflections: What is the relationship between the following objects? Bathroom - Bathtub Car - Engine Person - Professor Vehicle - Car Note: This post was originally posted on my blog at http://www.adaptivelearningonline.net