What are the uses of abstraction in the OPPs concept, how to implement it in our program?

Asked 26-Aug-2021
Viewed 474 times

0

What are the uses of abstraction in the OPPs concept, how to implement it in our program?


1 Answer


0

Data Abstraction in C#

In C#, Data Abstraction is a technique by which only necessary data is shown to the user and unnecessary data is hidden. In other words,

“Data Abstraction is a process by which only necessary data is provided and the internal data which is there is hidden.”

This is a very important feature of object-oriented programming (OOP). By which we show the necessary information of the program to the user and hide the unnecessary information which is there.

So let's see a real-life example of this now

Whenever we drive a bike. So we only know that if we press the accelerator, then the speed of the bike will increase. And if you press the brake, the bike will stop. But we do not know how the speed increases by pressing the accelerator and how the bike stops by pressing the brake. This is called abstraction. C# provides us with a high level of abstraction such as the pow() function is used to calculate the power (power) of a number. But we do not know which algorithm this function follows.

Advantage of Data Abstraction in C++ in Hindi

1. By this the programmer does not need to write low-level code.

2. This does not cause duplication of code.

3. This increases the reusability of the code. That is, we can use the code in other places.

4. It increases the readability of the code. Through this, the user can easily read the code.

5. Only you can make any changes in data and functions, no one else can.

6. Data abstraction keeps the application secure as it does not allow anyone to see the background details.

7. We can change the internal implementation of the class without affecting the user.

Syntax

 abstract public class class-name

 {
  // normal function
  public datatype function-name()
  // abstract method
  abstract public datatype function-name();
 }

Example

using System;
abstract class Animal{ // abstract class
 public string Sound;
 abstract public void AnimalSound(); // abstract method 
 public int AnimalLegs(){
  return 4;
 }
 abstract public string Message(); // abstract method
}
class Pig : Animal
{
 public override void AnimalSound() // override function
 {
  Sound = 'wee wee';
 }
 public override string Message() // override function
 {
  this.AnimalSound();
  return this.GetType().FullName + ' has '+ this.AnimalLegs() + ' legs and sound is ' + this.Sound;
 }
}
class Dog : Animal
{
 public override void AnimalSound()
 {
  Sound = 'Bark';
 }
 public override string Message()
 {
  this.AnimalSound();
  return this.GetType().FullName + ' has '+ this.AnimalLegs() + ' legs and sound is ' + this.Sound;
 }
}
class Cat : Animal
{
 public override void AnimalSound()
 {
  Sound = 'Meow';
 }
 public override string Message()
 {
  this.AnimalSound();
  return this.GetType().FullName + ' has '+ this.AnimalLegs() + ' legs and sound is ' + this.Sound;
 }
}
public class Program 
{
  public static void Main()
  {
    Pig pig=new Pig();
  Console.WriteLine(pig.Message());
  Dog dog=new Dog();
  Console.WriteLine(dog.Message());
  Cat cat=new Cat();
  Console.WriteLine(cat.Message());
  }
}

Output

Pig has 4 legs and sound is wee wee
Dog has 4 legs and sound is Bark
Cat has 4 legs and sound is Meow