Monday, December 14, 2015


   >>> In the real world there are many objects that can be specialized. We know the children have some characteristics are common to their parents. Child depends face, features, nature, symptoms etc. In OOP, a parent class can inherit its behaviour and state to children classes.

   >>> Inheritance is the process by which objects can acquire the properties of objects of other class.

   >>> In some cases, a class will have "Subclasses", more specialized versions of  a class.

   >>> In OOP, inheritance provides re - usability, like, adding additional features to an existing class without modifying it.

   >>> Classes that serve as a basis for new classes are called base or super or parent classes.

   >>> Classes derived from base classes are called derived or sub or child classes.

   >>> Inheritance represented by a is - a relationship.

   >>> Derived classes inherit all the fields, properties, methods, and events defined in the base class. This means you can develop and debug a class ones and then reuse it as the basis for other classes.

   >>> The most common real world sample to explain inheritance is the geometric shapes object model. Squares, circles, triangles, rectangles, pentagons, hexagon, and octagons are geometric shapes. The following figure shows a sample set of geometric figures:

   >>> The concept of generalization in OOP means that an object encapsulates common state and behaviour for a category of objects.

  >>> The general object in this sample is the geometric shape. Most geometric shapes have area, perimeter and color.

   >>> The concept of specialization in OOP means that an object can inherit the common state and behaviour of a generic object; however each object needs to define it's own special and particular state a behaviour. In above figure, each shape has its own color. each shape has also particular formulas to calculate its area and perimeter.

   >>> Inheritance makes code elegant and less repetitive.

   >>> If we know that all shapes have color should we program a color attribute for each shape? The answer is no! Would it be a better idea to create a shape class that has a color attribute and to make all the specialised shapes to inherit the color attribute? The answer is yes!

   >>> A shape parent class and a derived class for each specific shape. The following UML class diagram shows the set of classes needed to model the geometric shapes sample. Observe the field, properties and methods for each class:

   >>> For example 2, say you have a Dog class and a Cat class and each will have an attribute for every eye color. In an Object Oriented design, the color attribute could be moved up to a class called Mammal along with any other common attributes and methods. In this case, both dog and cat inherit from the Mammal class as shown in figure below.

   >>> The Dog and Cat classes both inherit from Mammal. This means that a Dog class actually has the following attributes:
                              eyeColor                       // inherited from Mammal
                              barkFrequency             // defined only for Dogs

   >>> In the same vein, the Dog object has the following methods:

                             getEyeColor                 // inherited from Mammal   
                             bark                               // defined only for Dogs

   >>> When the Dog or the Cat object is instantiated, it contains everything in its class as well as everything from the parent class, Thus, Dog has all the properties of its class definition as well as the properties inherited from the Mammal class.

No comments:

Post a Comment