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

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 participate in polymorphism

    *Example* :-

1)using new keyword 
class BaseClass
{
    public void Display()
    {
        Console.WriteLine("BaseClass Display");
    }
}

class DerivedClass : BaseClass
{
    public new void Display() // Method hiding
    {
        Console.WriteLine("DerivedClass Display");
    }
}

class Program
{
    static void Main()
    {
        BaseClass baseObj = new BaseClass();
        DerivedClass derivedObj = new DerivedClass();
        BaseClass baseRefToDerived = new DerivedClass();

        baseObj.Display(); // Output: BaseClass Display
        derivedObj.Display(); // Output: DerivedClass Display
        baseRefToDerived.Display(); // Output: BaseClass Display
    }
}

2)without using the new keyword

If you don't use the new keyword in derived class then it will still produce same output as above but will give a warning message as below 
'DerivedClass.Display()' hides inherited member 'BaseClass.Display()' Use the new keyword if hiding was intended

➤ How to call the hidden method
In method hiding, you can also call the hidden method of the base class in the derived class using below different ways:

1) By using the base keyword ->        
        base.parent_method_name()

2) use the base class reference variable ponting to a derived classic object(instance) 

BaseClass obj = new DerivedClass();
    obj.Display(); // this will call the base class Display() method




___________________________________




 *➤ Difference b/w Method Overriding and method Hiding* 

1) Purpose
➔ Allows a derived class to provide a specific implementation of a method that is already defined in its base class.
➔ Allows a derived class to define a method with the same name as a method in the base class, but without affecting the base class method 
2) Usage
➔ Use the 'virtual' keyword in the base class method and the 'override' keyword in the derived class method.
➔ Use the 'new' keyword in the derived class method to hide the base class method.
3) Behaviour 
➔ When you call the method on an instance of the derived class, the derived class's version of the method is executed.
➔ When you call the method on an instance of the derived class, the derived class's version is executed. However, if the method is called on a reference of the base class type, the base class's version is used.

4) Polymorphism
➔ overriding Supports runtime polymorphism
➔Method Hiding does supports compile time polymorphism

5) Access modifiers
➔ Overriding: The access level of the overridden method in the derived class must be the same or less restrictive than the method in the base class.
➔ Method Hiding: The access level of the hiding method can be different from the base class method.

6) Inheritance Chain
➔ If a class overrides a method, any further derived classes can also override the method, creating a chain of overridden methods.
➔ Hiding does not propagate in an inheritance chain. Each derived class that hides a method does so independently.

7) Method signature
➔ For overriding, the method signature in the derived class must exactly match the method signature in the base class.
➔ The method signature in the derived class can match the base class method signature or be different.

8) Base class access
➔ You can call the base class's version of the method from the overridden method using base.MethodName().
➔ You cannot directly call the hidden base class method from the derived class method using base.MethodName(). You would need to explicitly cast to the base class.

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,
                      double b, double c)
    {
        double sum = a + b + c;
        return sum;
    }
     This will throw compile time error as method signature is same but return type is different

• you can't define more than one method with same name and same signature(number, types, and order of parameters)
• The return type of the methods can be the same or different, but overloading is not determined by the return type.
• Method overloading is possible both within the same class and across parent-child classes.


➤ Different ways of doing overloading methods

1) The number of parameters in two methods.
   public int Add(int a, int b) {
        int sum = a + b;
        return sum;
    }
    public int Add(int a, int b, int c) {
        int sum = a + b + c;
        return sum;
    }

2) The data types of the parameters of methods.
public int Add(int a, int b, int c){
        int sum = a + b + c;
        return sum;
   } 
    public double Add(double a,
                      double b, double c) {
        double sum = a + b + c;
        return sum;
    }
3) The Order of the parameters of methods.

    public double Add(int a, double b) {
        return a + b;
    }
    public double Add(double a, int b) {
        return a + b;
    }


_____________________________
_____________________________


■ Overriding

• Creating a method in the derived(child) class with the same signature(same name, return type, and parameters) as a method in the base(parent) class is called as method overriding.
• Overriding is all about changing the behaviour of parent’s method under the 
child class.
• In simple words, Overriding is a feature that allows a child class to provide a specific implementation of a method that is already provided by one of its parent classes.
• Method overriding is a type of run-time polymorphism.
• parent class Method is called overridden method and the method which override the method from parent class are called overriding method.
• The base class method must be marked with the virtual keyword, and the method in the derived class must be marked with the override keyword.
• A non-virtual or a static method can’t be overridden.
• The access modifier of the overriding method in the derived class should be the same or less restrictive than the access modifier of the method in the base class.
• declaring a method as virtual allows it to be overridden in any derived class 

➤ Overriding can achieve by 2 ways
 
1) Using the 'virtual' and 'override' Keywords
2) Using Abstract Methods -> abstract methods are implicitly virtual and must be overridden in any non abstract derived class 

           Ex :-
public class BaseClass {
    public virtual void Display() {
        Console.WriteLine("Base Class Display Method");
    }
}
public class DerivedClass : BaseClass {
    public override void Display() {
        Console.WriteLine("Derived Class Display Method");
    }
}
class Program
{
    static void Main(string[] args)
    {
        BaseClass obj1 = new BaseClass();
        obj1.Display();  // Output: Base Class Display Method

        DerivedClass obj2 = new DerivedClass();
        obj2.Display();  // Output: Derived Class Display Method


//The reference to the DerivedClass object is assigned to the obj3 variable, which is of type BaseClass.

        BaseClass obj3 = new DerivedClass();
        obj3.Display();     // Output: Derived Class Display Method
    }
}


____________________________________________

➤ Calling the Base Class Method
The derived class method can call the base class method using the 'base' keyword. 

➤ Sealing an Overridden Method
If you want to prevent further overriding of a method, you can use the 'sealed' keyword.

public class DerivedClass : BaseClass
{
    public sealed override void Display()
    {
        Console.WriteLine("Derived Class Display Method");
    }
}

public class FurtherDerivedClass : DerivedClass
{
    // This will cause a compile-time error
    // public override void Display()
    // {
    //     Console.WriteLine("Further Derived Class Display Method");
    // }
}
 
____________________________________________


➤ Assigning a Derived Class Object to a Base Class Reference

Concept  :-When you create an object of a derived class (Apple) and assign it to a reference of the base class (Fruit), you can use that base class reference to call methods defined in the base class.
➔ How it works
When you create an object of a derived class (Apple) and assign it to a reference of the base class (Fruit), you can use that base class reference to call methods defined in the base class.
If any method is overridden in the Apple(derived) class and the method in the Fruit(base) class is marked as virtual, the overridden method in Apple(derived) will be called.

➔ We can assign derived class object to a base class reference in 2 ways and both will produce exactly same result 

1) Directly Assigning a Derived Class Object to a Base Class Reference

BaseClass baseobj=new DerivedClass();


2) Assigning a Derived Class Object to a Base Class Reference Through an Intermediate Derived Class Reference

DerivedClass deriveobj=new DerivedClass();
BaseClass baseobj= derivedobj

Let's understand with Example 

public class Animal
{
    public virtual void Speak()
    {
        Console.WriteLine("Animal speaks");
    }
}

public class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Dog barks");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal obj = new Dog();    // Using the base class type to refer to a derived class object
       
         obj.Speak();       // This will call the Speak method in the Dog class
    }
}


Animal obj = new Dog();

1) 'Animal obj' creates a variable named 'obj' that can hold a reference to an object of type Animal or any of its derived types.
2) obj = new Dog() The reference to the Dog object is assigned to the obj variable

➤ let's understand what's the difference between 

"DerivedClass derivedobj = new DerivedClass();  " 

&

 "BaseClass baseobj = new DerivedClass();   "



1)
➔ derivedObj is of type DerivedClass
➔ The reference variable baseobj is of type BaseClass, but it points to an instance of DerivedClass

2)
➔ derivedObj can access all public and protected members (methods, properties, fields) defined in DerivedClass, including those inherited from BaseClass.
➔ baseobj can can only access the members defined in BaseClass, even though it is actually an instance of DerivedClass

➤ So now question is if baseobj can only access base class members how it is calling overriding(child class) method?

       Answer :- 
•it is because of virtual and override keyword. • When a method in a base class is marked as virtual, it means that the method is allowed to be overridden in any derived class.
• The override keyword in the derived class indicates that the derived class is providing a new implementation for that method.
• Even though baseobj is a reference of type BaseClass, it points to an instance of DerivedClass. 
• So it is able to calling overriding method because of virtual keyword, override keyword & because it points to the instance of derived class, it decides on run time that method overriding have to be called 


➤ Why Use This?
• Using a base class type to refer to a derived class object allows you to write code that works with the base class type but can also operate on any derived class objects.
• This is useful for polymorphism, where the exact type of the object may not be known until runtime.



___________________________________
------------------------------




➤  Difference between method overloading and overriding

1) Definition 
➔ Method overloading allows you to define multiple methods with the same name but different signatures. 
➔ Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its base class.

2) Use case
➔ It is used when methods perform similar tasks but with different inputs. 
➔ It is used to change or extend the behavior of an inherited method.

3) Runtime
➔ Method overloading is resolved at compile time. 
➔ Method overriding is resolved at run time. 

4) Inheritance 
➔ Overloading does not require inheritance. ➔ overriding require inheritance. 

5) Signature 
➔ Methods must have different parameter lists. 
➔ The overridden method in the derived class must have the same signature as the method in the base class.

6) Access Modifiers
➔ Methods can have different access modifiers(like public, private, protected, internal). 
➔The access level of the overriding method must be the same or less restrictive than the overridden method.

7) static methods
➔ Static methods can be overloaded.
➔ Static methods cannot be overridden .

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