Inheritance

  • The evolution of successful simple general systems into more complex and specialised ones is part of the OO model. It is described as Inheritance
  • Inheritance helps to model the abstraction. Eg: Abstract Classe & Interfaces
  • Inheritance may be of little or no importance when object composition is considered in the context of distributed objects and components.

Inheritance vs Delegation

Delegation is meant to obtain the same effect as that of inheritance when we operate at object level in classless languages

Inheritance Delegation
Between Classes Between Objects
In Class-based languages In Prototype-based languages
Every instance of derived class has internal parent chain Chaining of objects is explicit
Cannot share parent objects, but can share parent classes Can share parent instances
Reuse of parent classes Reuse of parent objects

Also refer: Inheritance vs. delegation: Is one better than the other?

Refer the material A. Taivalsaari. On the notion of inheritance. ACM Computing Surveys, 28(3):438-479, September 1996 for in depth view of inheritance


Polymorphism

In object-oriented programming theory, polymorphism is the ability of objects belonging to different types to respond to method calls of the same name, each one according to an appropriate type-specific behavior.

Varieties of polymorphism:

Varieties of Polymorphism

Source: Luca Cardelli and Peter Wegner. On understanding types, data abstraction, and polymorphism. Computing Surveys, 17(4):471-522, 1985.

  • Overloading: integer constants may have both type integer and real. Operators such as + are applicable to both integer and real arguments.
  • Coercion: an integer value can be used where a real is expected, and vice versa.
  • Subtyping: elements of a subrange type also belong to superrange types.
  • Value sharing: nil in Pascal is a constant which is shared by all the pointer types.

In contrast to overloading and coercion, subtyping is an example of true polymorphism: objects of a subtype can be uniformly manipulated as if belonging to their supertypes.
Programming Example:

Interface IShape {
  void draw() {
  }
}
class Circle implements IShape {
  void draw() {
    System.out.println("It is a Circle");
  }
}
class Square implements IShape {
  void draw() {
    System.out.println("It is a Square");
  }
}
...\\
IShape shape = new Circle();\\
shape.draw(); // calls the draw method of Circle class\\
shape = new Square();\\
shape.draw(); // calls the draw method of Square class\\
...

<< Back to Tech Archives