-
Key Features and
Create a new project in Visual Studio 2022 - Naming Standards & Comments
- Data Types
- Variables and Constant Variable
- Conditional Statement
- Switch Conditional Statement
- Arithmetic, Comparison, Assignment and Logical Operators
- For, While and Do While Loops
- Importance of Break and Continue Keywords
- Strings and Important String Methods
- Single Dimensional Array
- Multi Dimensional Array
- Methods and Method Overloading
- Math Class and Functions
- Casting and Type Conversions
- Class and Objects
- Constructor, Parameter less and Parameterized Constructor
- Single, Multi level and Hierarchical Inheritance
- Compile Time Polymorphism, Method and Operator Overloading
- Run-time Polymorphism and Importance of base keyword
- Abstract Class and Method
- Interface and Multiple Inheritance through Interfaces
Agenda
- Abstraction in C#
- Abstract Class
- Abstract Method
- Non Abstract Method
Abstraction in C#
- Abstraction is a process of hiding implementation details and showing only functionality to the user.
- In another way it shows important things to the user and hides internal details, for example, sending SMS where user type the text and send the message. One don't know the internal processing about the message delivery.
- Abstraction focuses on what the Object does instead of how it does. Abstraction can be achieved with either abstract classes or interfaces.
- A class which is declared with the abstract keyword is known as an abstract class in C#. It can have abstract and non-abstract methods (method with the body).
- It is not possible to create an object of the abstract class.
- The abstract keyword enables user to create classes and class members that are incomplete and must be implemented in a derived class.
- The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
- Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods.
- User can not declare the abstract methods outside the abstract class.
Abstraction Example in C#
Example:
abstract class Animal
{
public abstract void eat();
public void drink()
{
Console.WriteLine(“Animal used to drink Water”);
}
}
class Tiger : Animal
{
public override void eat()
{
Console.WriteLine(“Tiger used to eat meat”);
}
}
static void Main()
{
Tiger obj = new Tiger();
obj.eat();
obj.drink();
//Animal animal = new Animal(); // Error CS0144 Cannot create an instance of the abstract type or interface ‘Animal
}
abstract class Animal
{
public abstract void eat();
public abstract void run(); //Create later after one run
public void drink()
{
Console.WriteLine("Animal used to drink Water");
}
}
class Tiger : Animal
{
public override void eat()
{
Console.WriteLine("Tiger used to eat meat");
}public override void run()
{
Console.WriteLine("Tiger used to run fast");
}
}static void Main()
{
Tiger obj = new Tiger();
obj.eat();
obj.drink();
obj.run();
//Animal animal = new Animal(); // Error CS0144 Cannot create an instance of the abstract type or interface 'Animal'
Console.ReadKey();
}