Thursday, July 2, 2015

Composition VS Inheritance

Inheritance:
Inheritance in Java is way to define relationship between two classes. Inheritance defines Parent and Child relationship between two classes. Similar to real world where a Child inherit his parents Surname and other qualities, In Java programming language, Child class inherit its Parent’s property and code.
If a Class B inherits from a Class A then A is Super Class of B and B is Sub Class of A. Inheritance is a way to generalize data and behavior of related classes a way to extend the behavior of a particular class.

Rules Concerning Inheritance:
1. A subclass constructor must make use of one of the superclass constructors , but does not need the same signature as any of these constructors

2. A class can have multiple (overloaded) constructors. To call one constructor from another, “this” is used (must be the first line of the constructor).
Example:
public Employee(String name) {
     this(name, 0.00);
}

3. A constructor can call a superclass constructor using “super”. See Manager class (also notice super is used in another way to call a superclass method).

4. To prevent a class from having any subclasses, the class can be declared final..

5. If A is a subclass of class B, when the constructor of A is invoked, there is a specific sequence of steps by which the static/instance variables are initialized and the bodies of the two constructors are executed.

1-Initializes superclass static variable
2-Executes superclass static block
3-Initializes subclass static variable
4-Executes subclass static block
5-Initialzes superclass instance variable
6-Runs superclass object initialization block
7-Runs superclass constructor
8-Runs subclass object initialization block
9-Initialzes subclass instance variable

10-Runs subclass constructor

Benifits of Inheritance:
Inheritance suppports Code Reusability.
It reduces code redundancy
Subclasses are much more succinct (smaller class file) than they would be without inheritance.
You can derive a new class from an existing class even if we don’t own the source code for the latter!

Criteria to Check whether inheritance is appropriate:

IS-A Principle Class C may extend class D if C IS-A D. Example: Manager IS-A Employee Example: Secretary IS-A StaffPerson

Liskov Substitution Principle (LSP): C may extend D if an object of type C may be used during execution where an object of type D is expected without breaking the code. Example: We may use a Manager instance wherever an Employee instance is expected, so having Manager as a subclass of Employee adheres to LSP


Problem while using Inheritance:

Fragility Of Inheritance:
Subclasses of a superclass – even when the IS-A criterion is met – may use the superclass in unexpected ways leading to broken code.
Rectangel Square Problem: Although Square is a rectangle but it has 4 sides all equal in length but rectangle has two different pair of sides. Inheritance in this case can cause the two different side for Square too and is vulnarable for changes.
Inheritence Violates Encapsulation:
If A is a subclass of B, even if A is not modified in any way, a change in B can break A.



Bloch's principle: Either design for inheritance or prevent it
First, the class must document precisely the effects of overriding any method.
In other words, the class must document its self-use of overridable methods.
For each public or protected method or constructor, the documentation must
indicate which overridable methods the method or constructor invokes, in what
sequence, and how the results of each invocation affect subsequent processing.
(By overridable, we mean nonfinal and either public or protected.) More
generally, a class must document any circumstances under which it might invoke
an overridable method.

To support inheritance, a class must document which overridable methods it uses in its own internal operations.
Preventing Inheritance:
Make the class final, OR
Make all constructors private and provide static factory methods to create instances.

Composition:
using instance variables that are references to other objects.

Using composition with inheritance for more flexibility:
To avoid the pitfalls of inheritance, it is always possible to use composition instead of inheritance. To illustrate the technique, imagine two classes, Person and PersonWithJob. Instead of asking PersonWithJob to inherit from Person, you can compose Person in PersonWithJob and forward requests for Person functionality to the composed class. We still get the benefit of reusing Person.

No comments:

Post a Comment