Monday, February 5, 2018

Object Oriented Programming

Object Oriented Programming is a methodology to design a program using classes and objects. It simplifies the software development and maintenance with the help of:

  • Object: Any Entity that has state and behavior is known as an Object which can be physical and logical
  • Class: Collection of Objects is called class. It is a logical entity
  • Inheritance: This allows one to define a class in terms of another class, which makes it easier to create and maintain applications. This provides code reusability and is also be used to achieve runtime polymorphism.

    Terminology used for Inheritance:
    • Base Class: The class whose features are inherited is knows as base class / super class / parent class
    • Sub Class: The class that inherits the base/super/parent class is known as subclass. This can add it's own fields and methods on top of base class
    • Reusability: When we want to create a new class and if there is a class already existing with some methods, we can derive that as a new class which is called reusability
    Syntax:
    The symbol used for Inheritance is :

    public class derived-class : base-class {
    // code goes here . . .
    }
    Types of Inheritance:
    • Single Inheritance: In this sub classes will inherit features, methods and fields from one super class
    • Multilevel Inheritance: In this, a derived class will inherit from a base class as well as the derived class will also be a base class for other classes
    • Hierarchical Inheritance: In this, once class will be served as super class for more than one sub class
    • Multiple Inheritance: The derived class can be derived from more than one base class. Remember, C# does not support multiple inheritance at class level, but will support multiple inheritance at interface level
    • Hybrid Inheritance: This is a mix of two or more inheritance types. This also, can be achieved in C# using interfaces and not with classes
  • Polymorphism: Means having many forms. In OOP paradigm, this is often referred as, "One interface, multiple functions"

    Types of Polymorphism:
    • Static Polymorphism: The mechanism of linking a function with an object during compile time is called static polymorphism which is also called as early binding. Below are the techniques used to implement static polymorphism in C#:
      • Function Overloading: We can have multiple definitions for the same function in the same scope and must differ from each other by the types or the number of arguments. And, cannot be differentiated by the return type
      • Operator Overloading: Most of the built-in C# operators can be overloaded. These are the functions with special names with Operator keyword followed by the symbol for the operator being defined. These also have a return type and a parameter list
    • Dynamic Polymorphism: This is implemented in C# by Abstract classes and Virtual Functions
      • Abstract classes provide partial class implementation of an interface. Implementation is completed when a derived class inherits from it. These classes contain abstract methods which are implemented by the derived class. You cannot create an instance of an abstract class. You cannot declare an abstract method outside an abstract class. And, when a class is declared as Sealted, then, it cannot be inherited and abstract classes cannot be sealed
      • You use virtual functions defined when you want the function to be implemented differently as part of inherited classes. This will help deciding the function behavior at runtime instead of compile time.
  • Abstraction: Hiding internal details and showing functionality is known as Abstraction. Using Abstract class and Interfaces one can achieve Abstraction in C#. An abstract class is never intended to be instantiated directly. And, these classes are typically used to deifne a base class in the class hierarchy. This class must contain atleast one abstract method which is marked by the keyword abstract in the class definition. The keyword abstract modifier indicates the incomplete implementation.

    Points to remember:
    • Abstraction is used at the time of Inheritance
    • One must use the override keyword before the method which is declared as abstract in child class, the abstract class is used to inherit in the child class
    • An abstract class cannot be inherited by structures
    • Contains constructors and destructors
    • Can implement methods with non-abstract mehtods
    • Cannot support multiple inheritance
    • Cannot be static
  • Encapsulation: Wrapping Data and Code together into a single unit is called as Encapsulation. Technically, the variables or data of a class are hidden from any other class and this can be shared or accessed only through access modifiers. As the data in a class is hidden for other classes, this is also called as data hiding

    Advantages:
    • Data Hiding: User will have no idea about the inner implementation of a class. He only knows the we are passing values to accessors and variables are getting initialized to that value
    • Increased Flexibility: The variables of the class are either read-only or write-only depending on requirement. By using Get and Set accessor this can be achieved
    • Reusability: This also improves reusability to change the behavior
    • Unit Testing: This is easy to unit test code

Advantages

  • Simple: The programmes written with OOP are really easy to understand. Since everything is treated as Objects, one can model a real-world concepts using OOP
  • Modular: Since the parallel development of classes is possible in OOP concept, it results in the quick development of the complete programmes. Each object forms a separate entity whose internal workings are decoupled from other parts of the system
  • Secured: It is a secured development technique since data is hidden and can't be accessed by external functions
  • Extensible: Adding new features or responding to changes in operating environments can be solved by introducing few new object and modifying existing ones
  • Maintainable: Programmes written using OOP technique are marginally easier to test, manage as well as maintain
  • Reusable: This approach offers the reusability of classes. We can reuse the classes that are already created without writing them again and again

DisAdvantages

  • Sometimes the relation between classes become artificial
  • Designing a program using OOP concepts is little bit tricky
  • One should have a proper planning before designing a program using OOP approach
  • As everything is treated as objects in OOP, the programmers need proper design, programming and thinking skills in terms of objects
  • The size of programmes developed with OOP is larger than the procedural approach
  • Since large in size, more instructions need to be executed, which results in slower execution of programmes

No comments:

Post a Comment