0% found this document useful (0 votes)
5 views

09 Abstraction

abstraction in c#
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

09 Abstraction

abstraction in c#
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

▪ Data abstraction is the process of hiding certain details and showing only essential

information to the user.


▪ Abstraction can be achieved with either abstract classes or interfaces.
▪ Using the Encapsulation principle, we can protect our data, i.e., from outside the
class, nobody can access the data directly. We are exposing the data through
publicly exposed methods and properties.
▪ On the other hand, using the Abstraction Principle, we are exposing only the
services so that the user can consume the services, but how the services/methods
are implemented is hidden from the user. The user will never know how the
method is implemented.
The abstract keyword is used for classes and methods:
1. Abstract class: is a restricted class that cannot be used to create objects (to access
it, it must be inherited from another class).
2. Abstract method: can only be used in an abstract class, and it does not have a body.
The body is provided by the derived class
▪ In C#, an abstract class is a class that serves as a blueprint for other classes.
▪ Abstract classes cannot be instantiated directly, but they can be used as base classes
for other classes that derive from them.
▪ We are not allowed to create objects of the abstract class.
▪ Abstract classes are declared using the abstract keyword.
▪ It can have abstract and non-abstract methods.
▪ They often define a common set of characteristics or behaviors that should be shared
among multiple derived classes.
▪ It is mandatory for the child class to provide the implementation for all the abstract
methods of the parent class.
▪ A method which is declared abstract and has no body is called abstract method. It
can be declared inside the abstract class only.
▪ Its implementation must be provided by derived classes.
▪ An abstract method in C# is internally a virtual method so it can be overridden by
the derived class.
public abstract class Calculator
{
public abstract void Add(int num1, int num2);
}
public abstract class AbsParent
{
public void Add(int x, int y)
{
Console.WriteLine($"Addition of {x} and {y} is : {x + y}");
}
public void Sub(int x, int y)
{
Console.WriteLine($"Subtraction of {x} and {y} is : {x - y}");
}
public abstract void Mul(int x, int y);
public abstract void Div(int x, int y);
}
// Derived class Cat inheriting from Animal
public class Cat : Animal {
using System; // Provide implementation for the abstract method
public override void MakeSound() {
// Abstract class Animal Console.WriteLine("Cat says: Meow");
public abstract class Animal { }
// Abstract method (no implementation) }
public abstract void MakeSound();

public class Program {


// Regular method (with implementation) public static void Main(string[] args) {
public void Sleep() { // Create objects of derived classes
Console.WriteLine("Animal is sleeping"); Animal dog = new Dog();
} Animal cat = new Cat();
}

// Call abstract method (runtime polymorphism)


// Derived class Dog inheriting from Animal dog.MakeSound(); // Output: Dog says: Woof
public class Dog : Animal { cat.MakeSound(); // Output: Cat says: Meow
// Provide implementation for the abstract
method
public override void MakeSound() { // Call regular method from abstract class

Console.WriteLine("Dog says: Woof"); dog.Sleep(); // Output: Animal is sleeping

} cat.Sleep(); // Output: Animal is sleeping

} }
}
▪ Common Blueprint: Abstract classes provide a common blueprint for all derived
classes. They force the derived classes to implement certain methods, ensuring a
consistent interface.
▪ Code Reusability: By using abstract classes, you can reuse common code (like the
Sleep() method in the Animal example) while allowing derived classes to provide
their specific implementation for abstract methods.
▪ Polymorphism: Abstract classes are often used with polymorphism, allowing you to
treat objects of different derived classes as objects of the base class.
▪ An abstract class cannot be instantiated directly. You can only create objects of its
derived classes.
▪ If a class contains even one abstract method, it must be declared as an abstract
class.
▪ Derived classes must implement all abstract methods of the abstract class unless
the derived class is also abstract.
▪ Abstract classes can have both abstract and regular (non-abstract) methods.

You might also like