Wednesday, September 18, 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 from the 'Object' class, which is defined in the 'System namespace
4) constructors are not inherited. 
If a base class has a parameterized constructor, the derived class must call this constructor explicitly.
5) C# supports multiple inheritance of interfaces, meaning a class can implement multiple interfaces. 
6) A derived class can access members of its base class but base class member cannot access member of derived class.
7) Static members of the base class are not inherited by derived classes but can be accessed using the base class name.

____________________________________________

 *➤ Access Modifiers in Inheritance* 

1) *public* : Members are accessible from any code.
2) *protected* : Members are accessible within the same class and derived classes.
3) *private* : Members are accessible only within the same class.
4) *internal* : Members are accessible within the same assembly.
5) *protected internal* : Members are accessible within the same assembly and derived classes(whether same or different assembly). 

____________________________________________

 *➤ sealed class and methods* 

➔ A class marked with the 'sealed' keyword cannot be inherited.
    public sealed class ClassName { }

➔ A method marked with sealed in a derived class cannot be overridden further
     public sealed override void MethodName() { }

____________________________________________

• A class (derived class) inherits from a single base class
• A class is derived from another derived class, forming a chain of inheritance.
• Each class inherits from its immediate base class, creating a multi-level hierarchy.
• A single base class is inherited by multiple derived classes.

 *C# does not support multiple inheritance & hybrid inheritance of classes, a class can implement multiple interfaces.* 

____________________________________________

➤ Advantages
2. Less time 

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 time to run (like reading a file or waiting for user input), the whole program will pause and wait until that task is finished before moving on to the next one.
So to overcome this problem multithreading concept come into picture. 

 *➤ Thread life cycle* 
• each thread has a life cycle. 
• The life cycle of a thread is started when the instance of System.Threading.Thread is created. 
• and when the task execution of the thread is completed, it's life cycle is ended. 
• here are the following states in the life cycle of thread. 
1) Unstarted: The thread is created but hasn’t started yet. You’ve created a Thread object, but you haven’t called the Start() method.
2) Ready (Runnable): Once you call the Start() method, the thread moves to the ready state. It's ready to run but waits for the CPU to allocate time to it.
3) Running: The thread is actively executing its task. The CPU has assigned time for this thread to run. 
4) Blocked/Waiting: The thread might move to a blocked state if it’s waiting for some resource (like I/O operations or a lock). The thread is paused and waiting for an event to occur before it can continue running.
Example - A thread might be blocked if it’s waiting for input or sleeping. 
5) Terminated (Dead): Once the thread completes its execution, it enters the terminated state and cannot be restarted.
This happens when the method that the thread was executing has completed.

 *➤ Thread classes* 
Thread class provides properties and methods to create and control threads. It is found in System.Threading Namespace. 
➔ Key members of the thread class
1) creating a thread
2) Starting a Thread: Use the Start() method to begin thread execution. 
3) Properties
Name: Gets or sets the name of the thread.
IsAlive: Returns true if the thread is running or in a blocked state.
IsBackground: Indicates whether a thread is a background thread (can be set to true or false).
Priority: Gets or sets the priority of a thread (can be Lowest, BelowNormal, Normal, AboveNormal, Highest).
ThreadState: Indicates the current state of the thread.


 *➤ Multithreading* 

• Multithreading is a programming technique where multiple threads are created within a single process to perform different tasks simultaneously.
• Multithreading allows for the concurrent execution of threads, meaning multiple threads can be in progress at the same time, although not necessarily simultaneously.
• Threads within the same process share the same memory and resources, making communication between threads easier but also introducing potential issues like race conditions and deadlocks.

 *➔ Common Multithreading Issues* 
• Race Conditions: Occur when two or more threads access shared data at the same time, leading to inconsistent results.
• Deadlocks: Occur when two or more threads are waiting indefinitely for resources locked by each other.
• Thread Safety: Ensuring that shared data is accessed in a thread-safe manner to avoid conflicts
➔ Benefits of Multithreading
• Improved Performance: By running multiple threads, you can take full advantage of CPU resources, especially on multi-core processors.
• Responsiveness: In UI applications, multithreading allows the user interface to remain responsive while performing background tasks.
• Efficient Resource Use: Threads can handle I/O-bound tasks (like reading a file or making a network request) while other threads continue processing, making better use of system resources.

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 
• method must have the same return type and parameter types as the delegate.
• Method name and delegate name can be same but it's generally not recommended as it can lead to confusion. 
• We generally define delegate under namespace. 

1) Defining a Delegate:

[modifier] delegate [return type] [delegate-name] ([parameter-lists])

For ex :- 
public delegate int MathOperation(int x, int y);


2) creating a method matching the delegate signature

class Calculator {
public static int Add(int a, int b)
{
    return a + b;
}

3) Instantiating a delegate
• Create an instance of the delegate and assign it the method you want it to reference. 
There are two ways to do this 
1)
MathOperation mo = new MathOperation(Add);

If method is non static 
Calculator obj=new Calculator();
MathOperation mo = new MathOperation(obj. Add);


2) 
You can assign a method directly to a delegate without using the 'new' keyword

MathOperation mo = Add;

4) Invoke the Delegate
• Call the method through the delegate using the delegate instance.
int result = mo(5, 3);    // Calls the Add method
Console.WriteLine(result); // Output: 8




using System;

// 1. Delegate for methods with a return type
public delegate int CalcDelegate(int x, int y);
    
// 2. Delegate for void methods
public delegate void CalcVoidDelegate(int x, int y);




            *➤ Example* 
___________________________________


// declaring delegate for  method with return type
public delegate int CalcDelegate(int x, int y);

// declaring delegate for void method 
public delegate void CalcVoidDelegate(int x, int y);

// class
public class Calculator
{
    // 1. Static method with return type
    public static int Add(int a, int b)
    {
        return a + b;
    }

    // 2. Void static method
    public static void Subtract(int a, int b)
    {
        Console.WriteLine("Subtract: " + (a - b));
    }

    // 3. Non-static method with return type
    public int Multiply(int a, int b)
    {
        return a * b;
    }

    // 4. Void non-static method
    public void Divide(int a, int b)
    {
            Console.WriteLine("Divide: " + (a / b));
        
    }
    
      // Main method 


public static void Main()
  {
        /* Static methods  */

  CalcDelegate addObj = Add;   // Static method with return type

        CalcVoidDelegate subObj = Subtract;           // Void static method


        /*Non-static methods */

   Calculator obj = new Calculator();
   CalcDelegate mulObj = obj.Multiply;     // Non-static method with return type


        CalcVoidDelegate divObj = obj.Divide;               // Void non-static method




        /*  Invoke static methods     */

        Console.WriteLine("Add: " + addObj(10, 5)); // Output:  Add: 15
       
  subObj(10, 5);      // Output: Subtract: 5

        /* Invoke non-static methods */

        Console.WriteLine("Multiply: " + mulObj(10, 5));   // Output: Multiply: 50

        divObj(10, 5);            // Output: Divide: 2
    }
    
    
}
___________________________________

Delegates Can Be Multicast:

➤ Multicast Delegate
• A multicast delegate in C# is a delegate that can hold references to more than one method.
• When you invoke a multicast delegate, all methods it references are invoked in the order they were added.
• All of the functions that the multicast delegate references will be called when the delegate is called. 
• All method signatures should match if you want to use a delegate to call multiple methods.
   
   ➤ Key points about multicast delegate

➔ return type handling 
• If the delegate has a return type (non-void), only the result of the last method in the invocation list is returned. Previous return values are discarded. (below, after few lines are the example see & understand ) 
• If the delegate returns void, all methods in the invocation list are executed, and each method can affect the program independently.
➔ combining methods
• Methods are added to delegates using the + or += operator.
➔ Removing methods
• You can remove a method from a multicast delegate using the -= operator.
➔ Use 
• Multicast delegates are commonly used for event handling, logging, or notifications where multiple methods need to be invoked in response to a single event.

  ➤  Multicast delegate example
___________________________________
using System;

// Declare a delegate with a return type of int
public delegate int CalcDelegate(int a, int b);

//  calculator class 
public class Calculator
{
    public int Add(int a, int b)
    {
        int result = a + b;
        Console.WriteLine("Add: " + result);
        return result;
    }

    public int Subtract(int a, int b)
    {
        int result = a - b;
        Console.WriteLine("Subtract: " + result);
        return result;
    }

    public int Multiply(int a, int b)
    {
        int result = a * b;
        Console.WriteLine("Multiply: " + result);
        return result;
    }

    public int Divide(int a, int b)
    {
        int result = a / b;
        Console.WriteLine("Divide: " + result);
        return result;
    }
    
    // Main Method 
    public static void Main()
    {
      // creating instance of Calculator class 
        Calculator obj = new Calculator();

        /* Create a multicast delegate */
        CalcDelegate calc = obj.Add;
        calc += obj.Subtract;
        calc += obj.Multiply;
        calc += obj.Divide;

          /* Invoke the multicast delegate */
// & 
    /* Since the delegate has a return type, only the result of the last method is returned.  
Only the value of Divide method(which is in last) is returned */

       int final_res = calc(20, 5);

      Console.WriteLine("Final Result: " + final_res); // Output: 4
    }
}

Output :-
Add: 25
Subtract: 15
Multiply: 100
Divide: 4
Final Result: 4
___________________________________





 *■ Event* 

Read about event from pdf in classroom (pdf name - delegate and thread)

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 be compilation error. 
• A derived class must provide implementations for all abstract methods in the base class other wisely there will be compile time error. 
• Abstract methods cannot be static. 

                 *Example* :-

public abstract class Animal
{
    public abstract void MakeSound();
    public abstract void Eat();
}

public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Woof!");
    }

    public override void Eat()
    {
        Console.WriteLine("Dog is eating.");
    }
}

public class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Meow!");
    }

    public override void Eat()
    {
        Console.WriteLine("Cat is eating.");
    }
}


■ *Interface* 
• Interface is a contract that defines a set of methods and properties that the implementing class must provide.
• Interfaces specify what a class must do and not how.
• interface include only the signatures of method (i.e. their names, return types, and parameters),not their implementation. Interface cannot have methods with implementation(body). 
• A class that implements an interface must provide implementations for all the inherited methods or members otherwise there will be compilation error .
• Interfaces can’t have private members.
• interface cannot have constructors. 
• By default, all members of an interface are public and abstract(but we can use other modifiers) , so the implementing methods in the derived class must be declared as public.
• Interface cannot contain fields because they represent a particular implementation of data.
• interface cannot contain static methods. 

• Interfaces can inherit from other interfaces, and interface can inherit more than one interface 
• interface helps to achieve multiple inheritance
• Interfaces promote loose coupling

➤ Real life example
Imagine you’re designing a system for different types of devices in a smart home. These devices might include lights, thermostats, and door locks. All these devices have some common actions but might also have device-specific actions

               ➤ Example 
public interface IAnimal
{
    void MakeSound();
    void Eat();
}

public interface IMovable
{
    void Move();
}
public class Dog : IAnimal, IMovable
{
    public void MakeSound()
    {
        Console.WriteLine("Woof!");
    }

    public void Eat()
    {
        Console.WriteLine("Dog is eating.");
    }

    public void Move()
    {
        Console.WriteLine("Dog is running.");
    }
}
public class Program
{
    public static void Main()
    {
        IAnimal myDog = new Dog();
        myDog.MakeSound(); // Output: Woof!
        myDog.Eat(); // Output: Dog is eating.

        IMovable movableDog = (IMovable)myDog;
        movableDog.Move(); // Output: Dog is running.
    }
}


-------------------------------------

 *➤ Difference between Interface and Abstract class* 

Interface 
Vs 
Abstract class 
1) Methods
➔ Methods in an interface are always abstract and do not have implementations.
➔ Can contain both abstract methods (without implementation) and concrete methods (with implementation).

2) Properties 
➔ Properties in interfaces are declared without any implementation or backing fields.
➔Can include properties with or without implementations. 

3) Fields
➔ Interface Cannot contain fields. Only method, property, event, and indexer declarations are allowed.
➔ Can contain fields, which can be used to store state or data relevant to the class.

4) Constructors 
➔ Interface Cannot contain constructors. Interfaces do not define object creation.
➔Abstract class Can have constructors that can be called by derived classes.

5) Inheritance
➔ A class or struct can implement multiple interfaces.
➔ A class can inherit from only one abstract class due to single inheritance.

6) Multiple Inheritance
➔ Interface Supports multiple inheritance.
➔ Abstract class does not supports multiple inheritance. It only supports single inheritance.

7) 
➔ Interface can inherit more than one interface 
➔ Abstract class cannot inherit more than one abstract class

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);
    }

    // Overloading the - operator
    public static Complex operator -(Complex c1, Complex c2)
    {
        return new Complex(c1.Real - c2.Real, c1.Imaginary - c2.Imaginary);
    }

    // Overriding ToString method for better output representation
    public override string ToString()
    {
        return $"{Real} + {Imaginary}i";
    }
}

class Program
{
    static void Main()
    {
        Complex c1 = new Complex(1.0, 2.0);
        Complex c2 = new Complex(3.0, 4.0);

        Complex sum = c1 + c2;
        Complex difference = c1 - c2;

        Console.WriteLine($"Sum: {sum}");
        Console.WriteLine($"Difference: {difference}");
    }
}
_____________________________

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");
    }
}

class Dog : Animal
{
    // Override the Speak method in the Dog class
    public override void Speak()
    {
        Console.WriteLine("Dog barks");
    }
}

class Cat : Animal
{
    // Override the Speak method in the Cat class
    public override void Speak()
    {
        Console.WriteLine("Cat meows");
    }
}

class Bird : Animal
{
    // Override the Speak method in the Bird class
    public override void Speak()
    {
        Console.WriteLine("Bird chirps");
    }
}

class Program
{
    static void Main()
    {
        // Create instances of derived classes and assigning it to base class reference

        Animal dog = new Dog();
        Animal cat = new Cat();
        Animal bird = new Bird();


        // Call the Speak method on each instance

        dog.Speak(); // Outputs: Dog barks
        cat.Speak(); // Outputs: Cat meows
        bird.Speak(); // Outputs: Bird chirps
    }
}


➤ There are two main types of Polymorphism 

1) Compile-Time Polymorphism (Static Polymorphism)
• It resolves method calls at compile time rather than at runtime 
• static polymorphism is achieved through method overloading, operator overloading and method hiding. 

2) Run-Time Polymorphism(Dynamic Polymorphism)
• It resolves method calls at run time rather than at compile time 
• It allows a method to perform differently based on the object that it is acting upon.
• This is typically achieved through method overriding and interfaces.

➤ When or why we made a method a virtual method
• a method is made virtual to allow derived classes to override it and provide their specific implementations. 
• This is a key aspect of achieving polymorphism.
• If you anticipate that new derived classes might need to implement or modify the behavior of certain methods.

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 numeric fields to zero and all string and object fields to null, unless explicitly initialized to different values in the constructor.

2) parameterised constructor
• A constructor having at least one parameter is called as parameterized constructor. 
It can initialize each instance of the class to different values.

3) Copy constructor
• This constructor creates an object by copying variables from another object. 
• Its main use is to initialize a new instance to the values of an existing instance.
• It takes class name as parameter. 

4) Static Constructor
• if we use static keyword before the name of constructor then it is called static constructor. 
• If there are static fields in our class then compiler will provide implicit static constructor until we create explicit constructor. 
• It is called automatically by the runtime before any static members are accessed or any static methods are called, and it runs only once for the class.
• A static constructor is used to initialize static fields of the class and to be executed only once.
• static constructors cannot have parameters and cannot be called directly.
• static constructors cannot be overloaded. 
• We can't use any modifier with static constructor 

    Example  :-
public class MyClass
{
    public static int Count;
    
    // Static constructor
    static MyClass()
    {
        // Initialize static members
        Count = 0;
        Console.WriteLine("Static constructor called.");
    }

    public static void IncrementCount()
    {
        Count++;
    }
}

// Usage
MyClass.IncrementCount();
Console.WriteLine(MyClass.Count); // Output: 1

✅ UNIT 4 — POSET, LATTICES & BOOLEAN ALGEBRA (DISCRETE MATHEMATICS)

  ✅ UNIT 4 — POSET, LATTICES & BOOLEAN ALGEBRA 1. Poset Partially Ordered Set A pair (A, ≤) where relation is: Reflexive Anti-...