Wednesday, September 18, 2024

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 .

Monday, March 11, 2024

Spiral model in Software Engineering

 áš˘piral model-:

Process is represented as a spiral rather than as a sequence of activities with backtracking.

Each loop in the spiral represents a phase in the process.

No fixed phases such as specification or design -loop in the spiral are chosen depending on what is required.
 Risks are explicitly assessed and resolved throughout the process.



  •  risk driven process model
  •  used for generating software projects
  •  alternate solution is provided if any risk found in the risk analysis then alternate solutions are suggested and implemented
  • combination of proto type and sequential model or waterfall model
  •  one iteration all activities are done for large projects the output is small.
>>>>>>> 
  1. Planning
  1. Modelling
  1. Construction
  1. Deployment
  1. Communication
 
Advantages
  • Reduce high amount of risk
  • Good for large and critical projects
  • Strong  approval and documentation control
  • The software is produced early in the life cycle
 
 
 

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