Abstraction.md - brainchildservices/curriculum GitHub Wiki

Slide 1

Abstraction

Making coffee with a coffee machine is a good example of abstraction.

You need to know how to use your coffee machine to make coffee. You need to provide water and coffee beans, switch it on and select the kind of coffee you want to get.

The thing you don’t need to know is how the coffee machine is working internally to brew a fresh cup of delicious coffee. You don’t need to know the ideal temperature of the water or the amount of ground coffee you need to use.

Someone else worried about that and created a coffee machine that now acts as an abstraction and hides all these details. You just interact with a simple interface that doesn’t require any knowledge about the internal implementation

Slide 2

image

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.

Slide 3

What is an abstract class?

  • A class that is declared by using the keyword abstract is called an abstract class.
    https://dotnetfiddle.net/Q3u5Wc

  • An abstract class in C# provides only a blueprint and does not contain the implementation of the class members.
    https://dotnetfiddle.net/1yhHuG
    Without abstract keyword we won't be able to declare a Method without body as we would get the error: must declare a body because it is not marked abstract, extern, or partial
    https://dotnetfiddle.net/Quzj6L

Slide 4

  • An abstract class can contain both abstract methods and non-abstract (concrete) methods.
    https://dotnetfiddle.net/e4kxTo

  • If any class contains abstract methods then it must be declared by using the keyword abstract.
    If the class has abstract method and if we don't declare the class as abstract, we would get the error: 'Animal.Walk()' is abstract but it is contained in non-abstract class 'Animal https://dotnetfiddle.net/HtR3mO (Error)

Slide 5

Slide 6

Abstract Method: A method which is declared abstract, has no “body” and declared inside an abstract class. An abstract method must be implemented in all non-abstract classes using the override keyword.

  • Q1. When to use the abstract method in C#?
    Abstract methods are usually declared where two or more subclasses are expected to fulfill a similar role in a different manner.
    https://dotnetfiddle.net/jo23Ib

  • Q2. Why abstract class can not be instantiated?
    Because it is not fully implemented the class as its abstract methods cannot be executed.

Slide 7

  • Q3. Who will provide the implementation (body) for abstract methods?
    Implementation of the abstract method will be provided in the subclass which inherits the abstract class.

*Q4. Can we declare an abstract method as sealed?
No, because it should be allowed to override in subclasses.

*Q5. Can we declare an abstract method as private?
No, because it should be inherited to subclasses

Slide 8

Exercises:

  • Q1: Create a C# program that implements an abstract class Animal that has a Name property of type text and three methods SetName (string name), GetName and Eat. The Eat method will be an abstract method of type void.

You will also need to create a Dog class that implements the above Animal class and the Eat method that says the dog is Eating.

To test the program ask the user for a dog name and create a new Dog type object from the Main of the program, give the Dog object a name, and then execute the GetName and Eat methods.

  • Q2: 1. Create a class named SpaceStation that is abstract
  1. On that abstract class, add a abstract method called FireLaser
  2. Create a derived class called DeathStar that implements the FireLaser method to write "Pew pew" to the Console followed by a new line.

References:
https://www.geeksforgeeks.org/c-sharp-abstract-classes/
https://www.c-sharpcorner.com/UploadFile/0c1bb2/using-abstract-class-in-C-Sharp/
https://dotnettutorials.net/lesson/abstract-class-abstract-methods-csharp/
https://www.w3schools.com/cs/cs_abstract.asp
https://stackoverflow.com/questions/47310158/exercise-implement-an-abstract-class

MCQ Questions from
https://www.interviewsansar.com/csharp-exercises-solutions-abstract-class/