Showing posts with label interface. Show all posts
Showing posts with label interface. Show all posts

Saturday, April 17, 2021

What is the difference between Abstract Class and Interface in C#?


This is a very important question for interviews at all levels.

Common differences to remember,

  • An abstract class is a half defined class, Interface is fully abstract
  • An abstract class does not provide full abstract implementation but the Interface does
  • Multiple inheritances of an abstract class are not allowed but multiple interfaces are allowed
  • An abstract class can contain a method with implementation and fields but Interfaces are only allowed to declare methods and properties.
  • Access modifiers can be defined in an abstract class but Interface is by default public
  • An abstract class and methods require Abstract keyword to declare whereas in Interface it is Interface
  • In other words, an abstract class does not provide full abstraction but an Interface does

Common points to remember,

  • We can not create an object of both Abstract class and an Interface
  • An abstract class with all abstract member are similar to an Interface declaration

I hope this basic differences and common characteristics help you to understand the basics of both an Abstract class and an Interface.

For more details, you are welcome to visit the dedicated posts for each topic as given below,

Any doubts, confusion? feel free to drop your thoughts. 

Saturday, April 3, 2021

What is the difference between Interface and Abstract Class in C#?

Difference between Interface and Abstract Class

Below are the common points to be remembered,

  • Abstract classes can have abstract plus nonabstract methods while Interfaces all methods needs to be abstract only.
  • Abstract classes can have variables and method implementation whereas Interfaces are not allowed to declare variables and the methods are abstract only. (No implementation)
  • Abstract class members are private by default even they can be changed, In Interface it is public by default and you cannot change the same.
  • To declare abstract classes Abstract keyword is used, for Interface it is Interface
  • Abstract classes and Interfaces are not allowed to create an instance.
  • Abstract classes can have a constructor but Interface do not. 

Friday, April 2, 2021

What is Interface?

The interface is used to achieve abstraction in C#. It can only contain method declarations and properties. It is a contract between two entities. 

I am using the same example from the previous post on Abstraction, to know more about what is abstraction you can visit the same post.

interface IBird        //Interface keyword to declare it
{
    void Sound();    //Only method signature
}

Points to remember,
  • Like abstract classes, no instance creation so no constructor is allowed.is allowed for Interfaces as well 
  • Declaring interface with I comes out of practice to identify the same. 
  • Interfaces can contain methods and properties but not fields
  • All members of interface are abstract and public, you do not need to use public access modifiers
  • Derived classes must have an implementation of Interface members, you do not require to use override keyword to implement the method like abstract classes
  • The interface provides security and standard contract 

class Dove: IBird                    //Inherited Interface
{
        public void Sound()        //interface method implementation
        {               
                Console.WriteLine("Coo Coo");
        }
 }

above is an implementation example of Interface IBird to Dove class. 


What is Abstraction in C#?

Abstraction is about showing what is necessary.

In C# we have abstract classes and abstract methods to achieve abstraction. 

  • Abstract Class is a restricted class or can say a half-defined class, so we can not create an instance of the class. To use this class it needs to be inherited.
  • Abstract Method can only be declared in abstract classes and it does not contain implementation like an Interface method declaration. Method implementation is a must to their derived classes.
  • An abstract class can also contain regular methods with abstract ones.
  • You cannot create an instance of an abstract class
  • An abstract class with all abstract methods will work as an Interface
  • An abstract class can not be declared as Sealed 


abstract class Bird
{
    public abstract void Sound();
    public virtual void Sleep()    //Virtual methods can have their primary implementation
    {
        //Implementation
    }
}

Bird bird = new Bird();    //This will throw an error saying cannot create an instance of an abstract class 

Abstract class needs to be Inherited to their derived class

class Dove: Bird
{
        public override void Sound()        //abstract method implementation
        {               
                Console.WriteLine("Coo Coo");
        }
        public override void Sleep()        //Virtual method can be overridden and have its own implementation
        {
                //
Implementation
        }
}


Abstract classes provide security and hide the complexity of the object so the object shows only necessary details to the client. Abstraction can also be achieved using Interfaces. Kindly refer dedicated topic on Interface to learn more.  

Thursday, April 1, 2021

Polymorphism (Static vs Dynamic Polymorphism)

Polymorphism means "many forms", also can be said "one interface and many functions"

Inheritance allows the use of parent class members, Polymorphism makes the same methods work in a different way. Means method signature remains the same but the implementation may vary according to the need.

There are two forms we can achieve polymorphism

  • Static Polymorphism
    • compile time
  • Dynamic Polymorphism
    • run time

In, Static Polymorphism we have the same method with multiple definitions. Method parameters or return types must differ to achieve polymorphism. 

class Print
{
    public void PrintNumber(int 
val)
    {
            Console.WriteLine(
val);
    }

    public void PrintNumber(string val)
    {
            Console.WriteLine(val);
    }
}

The above example contains the same method PrintNumber() with different parameters, so here we client is able to pass either int to string to print the same. This is called static polymorphism.


In Dynamic Polymorphism, With the help of Abstract classes and Virtual methods, you can achieve dynamic polymorphism. As we know Abstract classes have an abstract method that would require to declare an implementation in derived classes.

Point to remember,

  • Instance creation is not allowed of an abstract class
  • Abstract methods can be defined in abstract classes
  • Sealed classes can not be inherited hence you can not declare an abstract class as Sealed.

abstract class Bird
{
        public virtual void Sound()
        {
                Console.WriteLine("Bird Sound");
        }
}

class Dove : Bird
{
       
 public override void Sound()
        {               
                Console.WriteLine("Coo Coo");
        } 
}

class Rooster : Bird
{
       
 public override void Sound()
        {               
                Console.WriteLine("cock-a-doodle-doo");
        } 
}

//Calling class method

Bird myBird = new Bird();        //This will create a bird object
Bird myDove = new Dove();    //This will create a dove object
Bird myRooster = new Rooster(); //This will create a rooster object

//Calling their methods

myBird.Sound();          //This returns Bird Sound from base class
myDove.Sount();        // This returns Coo Coo from the Dove class
myRooster.Sound();    //This returns cock-a-doodle-doo from Rooster class

The above example contains an abstract class Bird and Primary method definition with a virtual keyword which allows its child class to override the same method and make its own implementation. This is called a dynamic polymorphism as at runtime object comes to know which one to call. 

When to use Polymorphism?

  • This allows reusability in code 
  • The class extension is allowed using Inheritence so no change in the existing implementation
  • Respective clients have their own method definition so it won't affect existing clients

Wednesday, March 31, 2021

What is Inheritance in C#?

Inheritance is a relationship between a base and its derived class. 

  • Base class
    • Also called the Parent class
    • This can be reusable by being inherited to the child classes
  • Derived class
    • The child class inheriting its parent
Advantages of using Inheritance
  • Provides resubility of existing implementation
  • Polymorphism, will see more on dedicated post that helps the same
Example,

class Vehicle
{
    public string make;
    
    public void StartEngine
    {
        //Different ways, its implementation
    }

}

class Car : Vehicle         //class Car Inherits from Parent class Vehicle
{
    public string model;
}

Now class Car has its own property model and it has also inherited the properties from Vehicle class as used Inheritance.

So creating intance of the class will allow you to use both as given below,

Car myCar = new Car();
myCar.make = "Mercedes";          //Inherited property
myCar.model = "C Class";            //Own property
myCar.StartEngine();                     //Inherited method


Sealed keyword in C# restrict the Inheritance of the class. 
Example,

sealed class Vehicle
{
    //implementation
}

class Car : Vehicle    //This is not allowed due to sealed keyword
{
    //
implementation

Will see more on scenario base queries on Sealed keyword dedicated topic. 

Abstract Classes
  • It is a half defined class, becuase it can have an abstract implementation and hence no instance can be created of abstract classes
  • This is helpful to create a base class to provide primary implementation to be followed for their child classes
  • All the abstract methods declaration requires its child classes do have an implementation of the same
  • Virtual Methods helps child classes to Override the same and change their own logic 
Interface
  • It's a contract 
  • classes that inherite the interface must have to implement all the methods from that Interface
  • Instance creation is not allowed
  • Helps to standardize coding practice across multiple classes
Hide base class member
  • if parent and child classes share the same name for their methods, the new modifier at method definition helps to hide the base class implementation

C# Interview Prep: 100 Common Questions for 0-3 Years Experience

Common C# Interview Questions  Basics of C# Language: What is C#? C# (pronounced C sharp) is a modern, object-oriented programming language...