Skip to main content

Posts

Inheritance

*■ Inheritance*  • Inheritance is a concept in OOP that allows a class to inherit properties and behaviors (methods) from another class. • A class that inherits from another class is called a derived class (or subclass) • The class which gets inherited by another class is called the base class (or superclass). • Inheritance is possible only if there is is-a relationship between parent and child class. • constructors are not inherited in derived class, however the derived class can call default constructor implicitly and if there's a parameterised constructors in bass class then derived class can call it using 'base' keyword.  ____________________________________________  *➤ Rules of Inheritance*  1) C# supports single inheritance, meaning a class can inherit from only one base class. 2) A parent class constructor must be accessible in child class otherwise  inheritance will be not possible. 3) every class, whether user-defined or predefined implicitly derives fr...

Thread

*■ Thread*   • a thread is a unit of execution within a process which executes an code under an application. It allows multiple operations to run concurrently.   • thread is a light weight process.  • A thread is a single sequence of instructions that a process can execute • When a C# program starts, it’s a single threaded process by default. • This “main” thread is responsible for executing your code line by line, creating what is known as a single threaded application. • Every application has some logic in it and to execute that logic this thread come into picture. • By default every application contain one thread to execute the program, and that is known as Main Thread. • by default, a thread in C# does not have a name unless you explicitly set it.  • Drawback - in a single-threaded program, the entire logic runs sequentially, one task at a time. This means the program executes each line of code in the order it appears. If a piece of code takes a long ti...

delegate

*■ Delegate*  • a delegate is a type that represents references to methods with a specific parameter list and return type. • It is similar to a function pointer in C or C++, but it is type-safe and object-oriented. • It allows you to store the address of a method and call it later, even without knowing which method you're calling at compile time.  • Method Reference : A delegate can hold a reference to a method, and you can invoke that method through the delegate. The method can be static or an instance method. • Type-Safe : Delegates ensure that the method signature (parameters and return type) matches the delegate's signature, providing type safety. • Delegates are type-safe, meaning they ensure that the method being called matches the expected signature, reducing runtime errors. • Multicast : A delegate can reference more than one method at a time, meaning it can invoke multiple methods in a chain. This is useful for event handling. ➤ Rules for creating delegate  • met...

abstract class

■ *Abstract class*  • A class which is declared with 'abstract' keyword are abstract class.  • An abstract class is a class that cannot be instantiated directly. • It serves as a base class for other classes. • Abstract classes are used to provide a common definition of a base class that multiple derived classes can share. • Abstract class can have both abstract and non-abstract methods.  • An abstract class can also have non-abstract methods with method body(implementation) that can be inherited by derived classes.  • Abstract classes can have constructors and fields. However, they are not intended to be instantiated directly.  • An abstract class must be inherited by a derived class, and all its abstract methods must be implemented by the derived class. ➤ Abstract methods • abstract methods are are methods without an method body (implementation) .  • Derived classes(non-abstract class) must provide an implementation for these methods otherwise there will...

operator overloading

■ operator overloading • Operator overloading gives the ability to use the same operator to do various operations. • It allows you to define how operators work with your custom types (classes or structs). • This can make your custom types more intuitive and easier to use. Let's say we have a Complex class to represent complex numbers. We want to overload the + and - operators to add and subtract complex numbers.     Example :- _____________________________ public class Complex {     public double Real { get; }     public double Imaginary { get; }     public Complex(double real, double imaginary)     {         Real = real;         Imaginary = imaginary;     }     // Overloading the + operator     public static Complex operator +(Complex c1, Complex c2)     {         return new Complex(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);   ...

polymorphism

■ Polymorphism • The word polymorphism means having many forms. • Ability to create many form/Achieving multiple behaviour with same  method/object. • polymorphism is a fundamental OOP concept that allows objects of different types to be treated as objects of a common base type.  • It allows objects to be treated as instances of their parent class rather than their actual class.  ➤ Example Consider a scenario where you have a base class Animal and several derived classes such as Dog, Cat, and Bird. Each derived class will override a method Speak to provide its specific implementation. Every animal can speak, but each type of animal speaks in different way so each animal need to implement speak method according to their behavior, this is what polymorphism is one name many form  class Animal {     // Virtual method in the base class     public virtual void Speak()     {         Console.WriteLine("Animal makes a sound")...

constructor

■ Constructor • A constructor is a special method that is used to initialize objects.   • it is called when an object of a class is created.  • It is used to set initial values for fields(variables) • without constructor we can't create instance of a class (if we don't create a constructor explicity then compiler create the default constructor implicitly )  • the constructor name must match the class name, and it cannot have a return type (like void or int). • All classes have constructors by default if you do not create a class constructor yourself, C# creates one for you. • A static constructor cannot be a parameterized constructor because it get called automatically by the runtime. ➤ Types of Constructor 1) Default Constructor 2) Parameterized Constructor 3) Copy Constructor 4) Static Constructor 1) Default Constructor or parameter-less constructor • A constructor with no parameters is called a default constructor.  • The default constructor initializes all n...