Skip to main content

Posts

Showing posts from September, 2024

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...

method hiding

*■ Method Hiding*  • Method hiding in C#, also known as method shadowing. • It is an approach of re-implementing the parent class method under the child class with the same name.  • Method hiding is a technique that allows a derived class to hide the methods of a base class. This is done by defining a new method in the derived class with the same name as a method in the base class, but using the new keyword.  • The derived class method must be marked with 'new' keyword.  • The derived class method can have a different return type or method signature than the base class method. But it’s generally a good practice to keep the signature the same to avoid confusion. • Method hiding does not participate in polymorphism. That is, if you have a base class reference pointing to a derived class object, it will call the base class method if the method is hidden, regardless of the derived class method’s implementation. Only methods that are overridden using the override keyword ...

overloading and overriding

■ Overloading •Method overloading is the process of defining multiple methods with the same name but different signatures (parameter lists) within the same class • Overloading is all about defining multiple behaviour to a method.. • Method Overloading is the common way of implementing polymorphism. It is the ability to redefine a function in more than one form. • Method overloading is a type of compile-time polymorphism.  • If method signature (number, types, and order of parameters) is same and the return type is different, then we can't overload method it will throw compile time error      Example -:       class Demo{     //adding three integer values      public int Add(int a, int b, int c){         int sum = a + b + c;         return sum;     }       // adding three double values.     public double Add(double a,        ...