Object Oriented Programming in C#


Encapsulation

  • Wrapping up of data under a single unit
  • Promotes data hiding, as variables in a class are private by default

Abstraction

  • Only essential details are exhibited to users
  • Can be achieved with either abstract classes/Functions or interfaces
  • Access Modifiers:
    • public
    • private
    • protected : accessible within derived classes
    • internal : accessible within the same namaspace
    • protected internal

Inheritance

  • A new class can inherit the methods and properties of an existing class and can also add new ones of its own
  • Promotes code reuse, simplifies code maintenance and improves code organisation
  • Types of Inherirtance
    • Multi-level
    • Mutliple inheritance
      • Derived class having multiple parents, this CAN NOT be achieved in C# with the help of classes
      • This can be achieved in C# using interfaces
      • Multiple inheritance is not possible with classes because if a class inherits from multiple classes and those multiple parent classes each have a method with the same name and different implementations, then if the object of the derived class calls that method, which implementation to run now? So, there is a conflict
      • Multiple inheritance is possible with intefaces because there is no implementation of declared functions in any of the parent interfaces because interfaces simply do not allow us to define function definitions and interfaces are fully unimplemented classes , so no conflictClasses
  • A class is a blueprint or a template for creating objets
  • It defines the properties and behaviours of an object
  • Every class inherits from System.Object class
  • There can be multiple constructors in the same class and this is called constuctor overloading (polymorphism)

Polymorphism

  • Poly = Multi
  • Morph = Form
  • In C# polymorphism can be acieved in two ways
    • Compile time Polymorphism or Static Polymorphism
      • Achieved by method overloading
        • Methods with same name but different parameters
        • aka early binding or static binding
        • It is decided at compile time that which method would be called
        • In C# we can overlaod methods, constructors and indexed properties
        • We can define different overloads of the same fucntion in the same class and those are differentiated by the parameter types and numebers
    • Runtime Polymorphism or Dynamic Polymorphism
      • Runtime polymorphism is achieved with method overriding
      • Method overriding can not be implemented without inheritance
      • It is the technique by which we can override the behaviour of methods of a parent class
        • Only if parent class's method is marked virtual
        • In the derived class, we use override keyword in the overriding implementation of the parent class's virtual function
      • We can also override abstract methods of the parent class

Classes

  • A class is a blueprint or a template for creating objects
  • Can be used to represent real life entities with thier properties and behavious

Objects

  • A dynamicaly created instance of a class
  • It is created at runtime so it can also be called a runtime entity
  • It can be created by using the new keyword for the class we are creatig it for

Comments