Abstraction

Tahiru Kofitey
November 07, 2025 • 1 min read 116 0

Data abstraction is the process of hiding certain details and showing only essential information to the user.

// Abstract class
abstract class Animal
{
  // Abstract method (does not have a body)
  public abstract void animalSound();
  // Regular method
  public void sleep()
  {
    Console.WriteLine("Zzz");
  }
}

// Derived class (inherit from Animal)
class Pig : Animal
{
  public override void animalSound()
  {
    // The body of animalSound() is provided here
    Console.WriteLine("The pig says: wee wee weeee");
  }
}

class Program
{
  static void Main(string[] args)
  {
    Pig myPig = new Pig(); // Create a Pig object
    myPig.animalSound();  // Call the abstract method
    myPig.sleep();  // Call the regular method
  }
}
Tags
About Tahiru Kofitey

Software developer

Comments 0

No comments yet. Be the first to share your thoughts!

Related Articles

More stories you might enjoy

Loading...