Search This Blog

Showing posts with label OOPs. Show all posts
Showing posts with label OOPs. Show all posts

Wednesday, 23 July 2025

OOPs in Python

Python supports rich object-oriented programming (OOP) features that promote modularity, scalability, and readability. In this article, we’ll explore core OOP concepts using Python’s syntax and conventions—ideal for building GUI apps, REST APIs, and creative projects.

Classes: They are like a template to create new objects. Similar to the blueprint of a house plan created by the architect.


Constructor: 

Every class has a constructor. This ensures that the object is created and loaded in the application memory with its settings as provided in the constructor. This is similar to the architect of the house instructed the contractor how to lay the foundation with which material and how it needs to be placed etc.

This is done using python's built-in function __init__(self).
This is a special function which will be invoked by python at runtime. You can see the above example with the __init__(self, <parameters>)
where,
self : here indicates the current context of the object created

Inheritance
Inheritance lets one class absorb properties and behaviors of another.


Syntax: 

class Child(Parent):
            ... your code here ...

Let's take the example of senior developer:



** NOTE: Please note, the above class does not have __init__()

Here, we need to understand, that until the parent class is constructed the child class cannot be constructed. Hence order of construction of object in execution, in the case of inheritance is
1.  parent __init__() 
2. child __init__()

CASE: In case the parent constructor has parameters apart from self, then how can these be supplied in inhheritance case?

ANS: Use super().__init__(parent-parameter-values)

Hence the above code will be modified to call the parent class constructor as follows:



Access Specifiers:
The following conventions are used to specify access.


See the following example:
Here instance properties are created where values assigned are native to that specific object only.



Statics:These are shareable variables and function. They are invoked using

ClassName.<static_member>

See the following example:


Overriding: The concept of  changing the behaviour of a function (logic of the function) in the child class is known as overriding




Overloading: The concept of creating a function with the same name but different parameters is known as overloading. Python does not support overloading. This can be done by using *args, **kwargs kind of parametersin the function

Play around with the following trinket showing all the above explained operations as an example



Thursday, 9 March 2023

C# Basic Boiler plating code for absolute beginners

 The following syntaxes are helpful in boiler-plate coding of any application. Here, we shall focus on the C# language. Please note, we are speaking of the common syntaxes that are used in every application. This is not a cheatsheet of all possible C# syntaxes.

We have seen in the post: Coding Best Practices - Part 1 article, about writing boiler-plate code.

Boiler-plate code is just creating the structure of an application, without a logic.

In .Net, everything is an object, hence the basic operating paradigm happens to be OOPs.

C# is a modern language created for .Net and also operates on OOPs.

In any application developed using the OOPs paradigm, following entities become very important

  • Namespaces: Logical grouping of code entities such as class, interface, enum, delegate, events
  • Class: A blueprint of a real-world object
    • Properties: Special variables with getters, setters. The main purpose is to share information with other classes, assemblies (executable version of a project)
    • Methods: Functions that contain logic.
  • Interface: A code contract, that is agreed upon by classes as required
  • Relationships:
    • Inheritance: Parent-Child Relations. In C#, we use ":" to indicate an inheritance / contract implementation
      • Eg: class Circle : Shape{...}
        • Here Shape is a normal class
      • Eg: class Circle: Shape, IPaintContract{...}
        • Here, IPaintContract is an interface
    • Association: Here one class contains a property, parameter of another class
      • class Circle

        {

           //Property of type Compass. Here Compass is a class

           public Compass DrawingTool {get; set;}

        }

         

        Class Compass{ ... }

Lets take an example of an application such as Employee Management System

  • It would contain
    • Classes like Person, Employee, Project, Department etc.
    • Namespaces for grouping a category of classes
    • Relationships
    • Contracts like IEmployeeContract

Here are the syntaxes for

  • Namespace: Syntax: namespace <namespace_name>{ ... }

    • namespace EMP

      {

      }

  • Class: Syntax: <access-specifier> class <class_name>{ ... }

    • Here, <access-specifier> can be one out of
      • public
        • Shareable and accessible in a given assembly and across different assemblies
      • private
        • Not shareable / accessible outside the class scope
      • protected
        • Shareable / accessible only to classes related through inheritance
      • internal
        • Shareable and accessible only within the assembly where it is created.

    • public class Person {

                         

       }

       

      public class Employee {

                         

       }

       

  • Interface

    • interface IEmpContract

      {

      }

       

  • Relationship. In this case, the relationship of inheritance

    • Class Employee : Person, IEmpContract{

      ...

      }

Thursday, 16 February 2023

Polymorphism In OOPs Paradigm

 Polymorphism can be visualized as a person who wears multiple disguises, at different times.

Technically, it means that a method / property behaves differently, at different times. These times could be captured at compile time / runtime

Polymorphism forms one of the most important pillars of OOPs paradigm. At an enterprise level, this principle is used on a day-in-and-out basis. There are 40 design patterns at the minimum that use the concept of polymorphism. The most common and obvious pattern is the "Factory" design pattern, which can be found even in the simplest and most complex enterprise projects.

In an interview, polymorphism questions come up in the first 5 questions.

There are two types of Polymorphism namely

  1. Compile-Time Polymorphism
    1. Overloading is the technique used to implement Compile-time polymorphism
  2. Run-Time Polymorphism
    1. Overriding is the technique used to implement Run-time polymorphism
    2. In overriding, the base class gives the permission to the derived classes to allow change in the implementation logic.
      1. This permission is given using special keywords
        1. virtual
        2. abstract
        3. interface

You can visualize Polymorphism in the following manner

OVERLOADING
  • Think of a person named "Kunal". Technically, "Kunal" is an object of the  "Person" class, right?!
  • A person can work. Hence, technically, "work" is a method inside the person class.
  • Now a person can work on a single task, sometimes the person may work on multiple tasks. Technically, it means we can have two flavours of "work" viz.
    • bool work(string task)
    • bool work(string[] tasks)
  • Both these methods are essentially having the same name and return type. The only change is the change in the parameter types and count. Also these methods are in the same class.
  • Hence, in this case we can say that Work() is an overloaded method.
OVERRIDING

  • Let's say the same Person "Kunal" is also an Employee.
  • Technically, it means there is an IS-A relationship between Employee and Person, both of which are classes.
    • For overriding, it is important that the classes involved are in an inheritance relationship.
  • In this case, the Person object "Kunal", when started working in a company also became an object of "Employee" class, right?!
  • The Person also works, and the Employee also works. This is a common behaviour between the two classes. But the way (implementation) work is done at office is different from the non-office environment. Both work on tasks, but the way they work and deliver is different.
  • Technically, the logic is different for work() in Person, and different for works() in Employee
  • work() is a method that returns bool. This declaration will remain the same whether the work is done at home or office, only the logic will differ.
  • Hence, we are "overriding" only the implementation for work(), when we instantiate "Kunal" as person Vs when we instantiate "Kunal" as an Employee.
  • In overriding, the method signature remains the same in both the base, derived classes. Only the logic is different.
  • There are three ways of implementing overriding
    • "virtual" keyword in base class --- "override" keyword in derived class
    • "abstract" keyword  in base class --- "override" keyword in derived class
    • Interfaces  also known as Code Contracts

SUMMARY WITH SYNTAX

1. Compile Time Polymorphism: Done by "overloading"

   Same class, same function name & return type, multiple flavours of method by changing parameters

class Person{

public void Works(){}

public void Works(string pTask){}

    }


2. Runtime Polymorphism: Done by "overriding"

   Pre-requisite: Inheritance relation between classes

a. Using virtual

Mark a method as "virtual" in base class

Use "override" keyword to change the logic in derived class

** A method marked as virtual can have default implementation logic

Eg: class Shape{

public virtual PrintDetails(){

...default logic

}

}

    class Circle : Shape{

public override PrintDetails(){

... different logic

}

}

        b. Using abstract

   Pre-requisite: Inheritance relation between classes

** Abstract means Incomplete

** A method / property can be marked as abstract

** If a method / property is marked as abstract, the class MUST be marked as abstract

** An abstract class can NEVER be instantiated

** Hence, use base = new derived() formula

i. Mark a method / property as abstract in base class.

ii. Mark the base class as abstract

iii. In derived class use "override" keyword

Eg: abstract class Shape{

public abstract void Draw();

}


    class Circle : Shape{

public override void Draw(){

...logic

}

}