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

No comments:

Post a Comment

How we can get higher marks in semester exam

 Here we talk about how to get higher marks in exams or test paper. Now we have to remember that the test and exams are follow the pattern b...