Wednesday, November 20, 2024

Common .NET OOPS Questions In BCA

1. What is OOPS?

2. Explain encapsulation, inheritance, polymorphism, and abstraction.

3. Difference between interface and abstract class.

4. How do you implement polymorphism in .NET?

5. What is the purpose of constructors and destructors?


.NET OOPS Benefits


1. Code reusability

2. Easier maintenance

3. Improved scalability

4. Enhanced security

5. Better organization


.Net OOPS Concepts

 .NET OOPS Concepts


1. Classes

2. Objects

3. Interfaces

4. Inheritance

5. Polymorphism

6. Encapsulation

7. Abstraction

8. Constructors

9. Destructors

10. Events

11. Delegates


The .Net Framework and oops in .Net

Introduction

 Developed by Microsoft

- Supports building web, mobile, desktop, and enterprise applications

- Includes:

    - Common Language Runtime (CLR)

    - Framework Class Library (FCL)

    - (.Net Framework) (web development)

    - (.Net Framework) (data access)

    - Windows Forms (desktop applications)

OOPS in .Net

object oriented programmming system 

based on four pillars

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction


DLL HELL -

DLL Hell is a term used to describe the problems and frustrations associated with managing and maintaining Dynamic Link Libraries (DLLs) in Windows-based systems.

Causes of DLL Hell:

1. Version Conflicts: Multiple applications requiring different versions of the same DLL.
2. DLL Overwriting: Newer or older versions of DLLs overwrite existing ones, breaking compatibility.
3. Missing DLLs: Required DLLs are not installed or missing.
4. Registry Issues: Incorrect or corrupted registry entries.
5. Dependency Issues: DLLs dependent on other DLLs or libraries.

Symptoms of DLL Hell:

1. Application crashes or failures
2. Error messages (e.g., "DLL not found" or "DLL version mismatch")
3. System instability or slowdowns
4. Difficulty installing or uninstalling applications

Types of DLL Hell:

1. Classic DLL Hell: Version conflicts and overwriting issues.
2. Shared DLL Hell: Multiple applications sharing the same DLL.
3. Private DLL Hell: Applications using private DLLs with conflicting versions.
    
Solutions to DLL Hell:

1. Side-by-Side (SxS) assemblies: Allows multiple versions of DLLs to coexist.
2. Windows Installer: Manages DLL dependencies and versions.
3. Assembly Manifests: Declares dependencies and versions.
4. Strong Naming: Unique identification for DLLs.
5. .NET Framework: Resolves DLL conflicts through assembly management.
6. DLL isolation: Using private DLLs or DLL redirection.


Prevention Strategies:

1. Use latest versions of DLLs.
2. Test applications for DLL compatibility.
3. Use dependency management tools.
4. Avoid shared DLLs.
5. Implement robust error handling.

Tools for Resolving DLL Hell:

1. Dependency Walker
2. Process Monitor
3. DLLViewer
4. Fusion Log Viewer
5. Windows Installer CleanUp Utility

DLL Hell has been significantly mitigated with the introduction of:

1. Windows Vista's User Account Control (UAC)
2. Windows 7's DLL management improvements
3. .NET Framework's assembly management
4. Windows 10's Universal Windows Platform (UWP)

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)

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