Showing posts with label abstraction. Show all posts
Showing posts with label abstraction. Show all posts

Saturday, April 10, 2021

What is difference between Abstraction and Encapsulation?

Abstraction is about showing what is necessary to the client

Encapsulation is about hiding the complexity

Key Differences

  • Abstraction is about to show the only necessary, Encapsulation is about hiding complexity 
  • Abstraction is focusing on what to be done, Encapsulation is focusing on how it can be done.
  • Abstraction solves problems at the design level while Encapsulation solves problems at the implementation level
  • Abstraction can be achieved using Interface or Abstract class, Encapsulation is achieved through Access Modifiers (Private, Public, Protected, etc.)
  • Abstraction is to be achieved during the design level, while Encapsulation is achieved at the implementation level.

Importance of Abstraction

  • Abstraction imposes simplicity in the code
  • It hides the irrelevant details
  • Programmed can be partitioned into a different level and helps decouple the architecture
  • Provides flexibility across different types of objects

Importance of Encapsulation

  • Makes code more manageable and easy to change with new requirements
  • Makes coding easier by hiding the complexity to the client
  • This makes unit testing easy
  • It helps to change part of code rather than changing everything 
  • Makes code more readable and understandable to the client what it offers 


Friday, April 2, 2021

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

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