- Inheritance and composition plays major roles in the design of object-oriented systems.
- Both inheritance and composition provides mechanisms for RE-USE.
- Inheritance as its name implies, involves inheriting attributes and behaviors from other classes.
- Inheritance represents the is-a relationship between two entities or objects.
- There is a true parent/child relationship and The child (or subclass) inherits directly from the parent (or superclass).
- Remember that Composition is also as its name implies, involved building objects by using other objects.
- One of the most powerful featureof Object Oriented programming is code Reusability.
- Structured design provides code reuse to a certain extent hence you can write a procedure and then use it as many times as you want.
- Inheritance also helps by organizing classes and factoring in commonalties of various classes.
- Inheritance is a primary means of providing this functionality.
- Inheritance allows a class to inherit the attributes and methods of another class.
- This allows creation of brand new classes by abstracting out common attributes and behaviors.
- One of the major design issues in OO programming is to factor out commonality of the various classes.
- You have a Dog class and a Cat class,and each will have an attribute for eye color.
- In a procedural model,the code for Dog and Cat would each contain this attribute.
- In an OO 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 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, Dog object has the following methods:
getEyeColor // inherited from Mammal
bark // defined only for Dogs
- For example there is a parent class (Shape Class) and Circle,Square and Star
- This relationship is often referred to as an is-a relationship because a circle is a shape, Square is a shape and Star is a shape.
- When a subclass inherits from a super class, it can do anything that the superclass can do.
- In below figure, the name on each of the objects represents the Draw method for the Circle,Star and Square objects respectively.
- When we design this Shape system it would be very helpful to standardize how we use the various shapes.
- This way we could decide that if we want to draw a shape, No matter what shape, we will invoke a method called draw.
- There are two major benefits to inheritance.
- The First, we can specialize existing classes (types) and write only the code that is changed.
- In this way, we can reuse our existing classes (as base classes) and capitalize on the work already done.
- We no longer have to "copy and paste" code.
- Copying and pasting is particularly problematic because changes in one section of the code must be replicated in another—a dangerous and failure-prone model.
- Instead, by using Inheritance, we can make changes in the base class that automatically update all the derived classes.
- The second significant benefit of inheritance is that it allows us to treat the derived objects polymorphically.