Skip to main content

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

Comments

Popular posts from this blog

Raster scan Vs Vector Scan

1. Raster Scan Display   How It Works : A raster scan display works by painting an image on the screen pixel by pixel, row by row. It follows a systematic pattern where the electron beam (in CRT monitors) or the display elements (in modern LCD/LED screens) sweep across the screen from left to right, top to bottom, in a series of horizontal lines (scan lines). This process is akin to how a traditional TV screen works.   Process : The display draws the image starting from the top-left corner, moving to the right, then moves to the next row below, and repeats this process until the entire screen is filled. This pattern creates a grid of pixels, where each pixel can have a color and brightness level.   Characteristics : Pixel-based : The screen consists of a grid of pixels, and each pixel can have a distinct color and intensity. Continuous Image : Raster scan displays are capable of displaying detailed and complex images, including photographs and videos, because they break t...

Inheritance

*■ Inheritance*  • Inheritance is a concept in OOP that allows a class to inherit properties and behaviors (methods) from another class. • A class that inherits from another class is called a derived class (or subclass) • The class which gets inherited by another class is called the base class (or superclass). • Inheritance is possible only if there is is-a relationship between parent and child class. • constructors are not inherited in derived class, however the derived class can call default constructor implicitly and if there's a parameterised constructors in bass class then derived class can call it using 'base' keyword.  ____________________________________________  *➤ Rules of Inheritance*  1) C# supports single inheritance, meaning a class can inherit from only one base class. 2) A parent class constructor must be accessible in child class otherwise  inheritance will be not possible. 3) every class, whether user-defined or predefined implicitly derives fr...

unit -1 Introduction of Image processing

  What is Image Processing? Image processing is a method to perform operations on an image to enhance it or extract useful information. It is a type of signal processing where the input is an image, and the output may be either an image or characteristics/features associated with that image. Goals of Image Processing Image Enhancement : Improving visual appearance (e.g., contrast, sharpness) Image Restoration : Removing noise or distortion Image Compression : Reducing the amount of data required to represent an image Feature Extraction : Identifying objects, edges, or patterns Image Analysis : Understanding and interpreting image content Object Recognition : Detecting and identifying objects in an image What is an Image? An image is a two-dimensional function f(x, y) , where x and y are spatial coordinates, and f is the intensity (brightness or color) at that point. For digital images, both x, y, and f are finite and discrete. Types of Image Representation...