Wednesday, 30 October 2024

Create object Using Abstract and Interface and its difference

Interface Class

interface InterfaceA

{

    string MethodA(string s);

}

Abstract Class

public class AbstractB:InterfaceA

{

    public string MethodA(string s)

    {

        return s;

    }

}


Object Creation Using Abstract 

AbstractB abstractB = new AbstractB();

abstractB.MethodA("Abstract object");

Output : Abstract object

Object Creation Using Interface 

InterfaceA interfaceA = new AbstractB();

interfaceA.MethodA("Interface object");

Output : Interface object

Note (Object Creation Using Interface) :



 Inheriting the interface directly in the Class and creating the object


 public class DerivedClass :InterfaceA
 {
     public string MethodA(string s)
     {
         return s;
     }
 }

InterfaceA interfaceA1 = new DerivedClass();
interfaceA1.MethodA("Derived object");

Output : Derived object

No comments:

Post a Comment