Abstraction

This is the fourth principle of  object oriented programming. Abstraction is a concept related to polymorphism. Abstraction in programming means declaring the methods that will be defined in derived class. The idea of abstraction works well with large and complex projects,  where class contains multiple methods that performs different tasks.

Another approach to this concept is saying that Abstraction works by establishing a level of complexity on which a person interacts with the system, suppressing the more complex details below the current level. The process of abstraction is the process to build a software model that presents solution for some task\s in real world. Abstraction is  a process of building concept model of more complex system where we dont need to know how low level tasks are executed

Abstract Base Class

The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events.  Abstract classes are created to be a base class of other classes. User can not instantiate object and call abstract class method. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.

Lets take a look at example:

 abstract class car
    {
        public abstract void move();
    }

Here we create abstract class with one abstract method. This method must be public so it’s available for other classes.

 abstract class car
    {
        public abstract void move();
    }
    class audi : car
    {
        public override void move()
        { 
             Console.WriteLine("moving"); 
        }
    }

Method in child class must overwrite the abstract method and contain the body of the function. Noticed that abstract class contains only declaration of the method.

class audi : car
        {
            public override void move() { Console.WriteLine("moving"); }
        }
        static void Main(string[] args)
        {
            audi r8 = new audi();
            car myNewCar = r8;
            myNewCar.move();
            r8.move();
        }
    }

We can now call our method from the instance of the child class. We can also create Object of type car, by coping the instance of subclass. Notice that we don’t instantiate object of abstract class.

This was the last topic of main principles of object oriented programming in C#. In the next article I’ll cover more complex topics. Use this knowledge to write better and more cleaner code. Posts about other principles of oop you can find in C# .NET category. Stay Awesome!