Principles of OOP - Part 2
Telerik Academy Alpha

 

 Table of contents

Polymorphism

 Polymorphism

  • Polymorphism is a Greek word that means "many-shaped"
  • Polymorphism = ability to take more than one form(objects have more than one type)
  • A class can be used through its parent interface/base class
  • A child class may override some of the behaviors of the parent class

 

 Polymorphism

  • Why handle an object of given type as object of its base type?
    • To invoke abstract operations
    • To mix different related types in the same collection
      • E.g. List <object> can hold anything
    • To pass more specific object to a method that expects a parameter of a more generic type
    • To declare a more generic field which will be initialized and "specialized" later

 

Virtual, abstract, override, new

 Virtual

  • Virtual member is
    • Defined in a base class and can be changed (overridden) in the descendant classes
    • Can be called through the base class' interface
  • Virtual methods are declared through the keyword virtual

 

public virtual void Draw { // do something }
  • Methods declared as virtual in a base class can be overridden using the keyword override

 

public override void Draw { // do something ELSE }

 Abstract

  • Abstract methods are purely virtual
    • If a method is abstract → it is virtual as well
    • Abstract methods are designed to be changed (overridden) later
  • Interface members are also purely virtual
    • They have no default implementation and are designed to be overridden in a descendant class
  • Virtual methods can be hidden through the new keyword
    • ​Use it with caution

 

public new void Draw { // do something ELSE }

 Override

  • Using override we can modify a method or property
    • An override method provides a replacement implementation of an inherited member
    • You cannot override a non-virtual or static method
  • The overridden base method must be virtual, abstract, or override

 

 How polymorphism works

  • Polymorphism ensures that the appropriate method of the subclass is called through its base class' interface
  • Polymorphism is implemented using a technique called late method binding
    • The exact method to be called is determined at runtime, just before performing the call
    • Applied for all abstract/virtual methods
  • Note: Late binding is a bit slower than normal (early) binding

 

 Polymorphism types

  • Static (compile time) Polymorphism Early binding -
    • methods with same name but different signatures because of this we will perform different tasks with same method name
    • overloaded methods, overloaded operators and overridden methods that are called directly by using derived objects

 

 

 Polymorphism types

  • Runtime Polymorphism / Late Binding
    • override a method in base class by creating similar function in derived class
    • ​overridden methods that are called using base class object

 

 

 Polymorphism - live demo

Cohesion and Coupling

Cohesion

 Cohesion

  • Cohesion describes
    • How closely the routines in a class or the code in a routine support a central purpose
  • Cohesion must be strong
    • Well-defined abstractions keep cohesion strong
  • Classes must contain strongly related functionality and aim for single purpose
  • Cohesion is a powerful tool for managing complexity

 

 Cohesion

  • Strong cohesion is when you have a class that does a well defined job

 Cohesion

  • Strong cohesion (good cohesion) example
    • Class Math that has methods:
      • Sin(), Cos(), Asin()
      • Sqrt(), Pow(), Exp()
      • Math.PI, Math.E
  • Weak cohesion (bad cohesion) example
    • Class that makes many unrelated tasks
      • ​Create Document
      • Format Document
      • Send Document as email

Coupling

 Coupling

  • Coupling describes how tightly a class or routine is related to other classes or routines
  • Coupling must be kept loose
    • Modules must depend little on each other
      • Or be entirely independent (loosely coupled)
    • All classes / routines must have small, direct, visible, and flexible relationships to other classes / routines
    • One module must be easily used by other modules

 Coupling

  • Loose Coupling
    • Easily replace old HDD
    • Easily place this HDD to another motherboard
  • ​Tight Coupling
    • ​Integrated video card on a motherboard
      • ​What happens if the video card fails?

Questions?

[C# OOP] Principles of OOP - Part 2

By telerikacademy

[C# OOP] Principles of OOP - Part 2

  • 1,266