Code Reusability
Object oriented programming styles
How to reuse code in a class again?
INHERITANCE
COMPOSITION
Before we start
 Composition Vs Aggregation
 Generalization vs Specialization
 Inheritance Vs composition
Association types
 Composition
 Is a strong association
 In UML diagram, composition is denoted by a
filled diamond.
 Aggregation
 Is a weak association
 In UML diagram, composition is denoted by an
empty diamond.
 Children can exist independently.
Composition Example
Aggregation Example
Generalization vs Specialization
 Generalization
 Is a mechanism for combining similar classes of
objects into a single, more general class.
 Specialization
 Is the reverse process of Generalization, which
means creating new sub-classes from an existing
class.
Generalization Example
Inheritance Vs
composition
 Both can help you reuse code.
 Inheritance establishes common behavior and interfaces
for your classes, while composition combines and reuses
existing classes to create more complex objects.
 Inheritance (is-a relationship), composition (has-a
relationship).
 Inheritance increases the coupling between a base type
and its derived types. If you change your base class, all
subclasses are also affected.
 prefer composition over inheritance
Thank you

Code Reuseability

  • 1.
  • 2.
    How to reusecode in a class again? INHERITANCE COMPOSITION
  • 3.
    Before we start Composition Vs Aggregation  Generalization vs Specialization  Inheritance Vs composition
  • 4.
    Association types  Composition Is a strong association  In UML diagram, composition is denoted by a filled diamond.  Aggregation  Is a weak association  In UML diagram, composition is denoted by an empty diamond.  Children can exist independently.
  • 5.
  • 6.
  • 7.
    Generalization vs Specialization Generalization  Is a mechanism for combining similar classes of objects into a single, more general class.  Specialization  Is the reverse process of Generalization, which means creating new sub-classes from an existing class.
  • 8.
  • 9.
    Inheritance Vs composition  Bothcan help you reuse code.  Inheritance establishes common behavior and interfaces for your classes, while composition combines and reuses existing classes to create more complex objects.  Inheritance (is-a relationship), composition (has-a relationship).  Inheritance increases the coupling between a base type and its derived types. If you change your base class, all subclasses are also affected.  prefer composition over inheritance
  • 10.