Showing posts with label classes. Show all posts
Showing posts with label classes. Show all posts

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.  

Monday, March 29, 2021

Working with Classes and Objects

I would request you to review my previous blog post for a basic understanding of Object Oriented Programming (To Read Click Me)

From the previous post, we will extend our Car example for better realistic understanding,

  • Class Car contains attributes such as color, engine power, size, airbag
  • Class Car contains methods such as drive, brake

Classes are just a blueprint. Objects are inherited property of the blueprint and it's reusable in nature.

Class example,

Class Car
{
        String Color;
        String Power;     

}

        From the above example, we have declared a class named Car and attributes are Color and Power. 

Creating an Object from the above class

Car carObject = new Car();

        Now, you can create as many as the object you want depending on the need of the program. This is called reusability. The below example explains the same.

        Car audi = new Car();
        
Car ferrari = new Car();


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