Abstraction Opps Concepts



Abstraction is "To represent the essential feature without representing the background details."

Abstraction lets you focus on what the object does instead of how it does it.

Abstraction provides you a generalized view of your classes or objects by providing relevant information.

Abstraction is the process of hiding the working style of an object, and showing the information of an object in an understandable manner.

Real-world Example of Abstraction
Suppose you have an object Mobile Phone.

Suppose you have 3 mobile phones as in the following:

Nokia 1400 (Features: Calling, SMS)
Nokia 2700 (Features: Calling, SMS, FM Radio, MP3, Camera)
Black Berry (Features:Calling, SMS, FM Radio, MP3, Camera, Video Recording, Reading E-mails)

Abstract information (necessary and common information) for the object "Mobile Phone" is that it makes a call to any number and can send SMS.

So that, for a mobile phone object you will have the abstract class as in the following:




   abstract class MobilePhone
    {
        public void Calling();
        public void SendSMS();
    }

    public class Nokia1400 : MobilePhone
    {
    }

    public class Nokia2700 : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
    }

    public class BlackBerry : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
        public void Recording();
        public void ReadAndSendEmails();
    }

Abstraction means putting all the variables and methods in a class that are necessary.
For example: Abstract class and abstract method.
Abstraction is a common thing.
Example
If somebody in your collage tells you to fill in an application form, you will provide your details, like name, address, date of birth, which semester, percentage you have etcetera.
If some doctor gives you an application to fill in the details, you will provide the details, like name, address, date of birth, blood group, height and weight.
See in the preceding example what is in common?
Age, name and address, so you can create a class that consists of the common data. That is called an abstract class. 
That class is not complete and it can be inherited by other classes.





Abstraction is an important feature of any object-oriented programming language. Abstraction involves extracting only the relevant information. Abstraction doesn't mean that information is unavailable. In other words all the information exists but only the relevant information is provided to the user.
Abstract class
 
C# enables us to create abstract classes that are used to provide partial class implementation of an interface. We can complete an implementation using the derived classes. Abstract classes contain abstract methods, which can be implemented by using abstract classes and virtual functions.
 
There are certain rules governing the use of abstract class. These are:
 
  1. Cannot create an instance of an abstract class.
  2. Cannot declare an abstract method outside an abstract class.
  3. Cannot be declared sealed. (Sealed classes can never be inherited.)
The syntax to declare a class Abstract is:

               abstract class classname
               
                {
                          ..........
                 }

Abstract methods
 
Abstract methods are the methods without any body. The implementation of an abstract method is done by the derived class. When a derived class inherits the abstract method from the abstract class, it must override the abstract methods.
 
The syntax for using the abstract method is:
 
[access-modifier] abstract  <return-type> <method-name([parameters])>

The following code illustrates the use of an Abstract method:
 
using System;
namespace abstractclass
{
   
       abstract class baseclass
       {
          publicabstract void m1();
       }
          class derived:baseclass
    {
     public  override  void  m1()
        {
            Console.WriteLine("success");
        }
    }


    public class implement
    {
 
        static voidMain(string[] args)
        {
            derived ob =new derived();
            ob.m1();
            Console.Read();
        }
     }
}

In the above code example, the abstract method is declared by adding the abstract modifier to the method. Abstract method m1() is declared in the abstract class named "baseclass". The abstract method is inherited in a "derived" class. Abstract methods cannot contain a method body.