Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Abstraction | OOP Principles
C# Beyond Basics

AbstractionAbstraction

Abstraction is a fundamental concept in object-oriented programming (OOP) that allows developers to hide complex implementation details and focus on essential functionalities. In C#, Abstraction is achieved through abstract classes.

OOP was initially based on three principles (sometimes referred to as "The Three Pillars of OOP") including Encapsulation, Inheritance, and Polymorphism. Hence Abstraction is a new addition and sometimes may not be considered as a fundamental concept according to some sources, nevertheless, it is an important concept.

An abstract class is a class that cannot be instantiated which means that we cannot create an object of that class. An abstract class can contain attributes and methods like any other class however it may also contain abstract methods which are blueprint methods meant to be implemented by derived classes.

We can create an abstract class by adding the abstract keyword before the class definition. For-example, lets create an abstract class called Shape:

cs

index.cs

Similarly, we can create an abstract method by adding the keyword abstract before its return type. An abstract method does not have any body - it is simply a blueprint:

cs

index.cs

The purpose of this is to create a blueprint for other classes. This helps in simplifying the code. To understand this better, let's look at the Polymorphism task from Chapter 5:

cs

index.cs

In the above example, we never intend to use the Shape class however we still had to write some bogus implementations of the getArea and calculatePerimeter methods inside the Shape class. We can somewhat simplify this code by making the Shape class abstract, apart from that we can also make the getArea and calculatePerimeter methods abstract.

A method can either be abstract or virtual but not both at the same time. However it is important to note that an abstract method is basically a virtual (overridable) method but it doesn't have any body in the base class.
cs

index.cs

1. Which keyword is used for making a class abstract?
2. Can an abstract class be instantiated?

Which keyword is used for making a class abstract?

Виберіть правильну відповідь

Can an abstract class be instantiated?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 5. Розділ 8
course content

Зміст курсу

C# Beyond Basics

AbstractionAbstraction

Abstraction is a fundamental concept in object-oriented programming (OOP) that allows developers to hide complex implementation details and focus on essential functionalities. In C#, Abstraction is achieved through abstract classes.

OOP was initially based on three principles (sometimes referred to as "The Three Pillars of OOP") including Encapsulation, Inheritance, and Polymorphism. Hence Abstraction is a new addition and sometimes may not be considered as a fundamental concept according to some sources, nevertheless, it is an important concept.

An abstract class is a class that cannot be instantiated which means that we cannot create an object of that class. An abstract class can contain attributes and methods like any other class however it may also contain abstract methods which are blueprint methods meant to be implemented by derived classes.

We can create an abstract class by adding the abstract keyword before the class definition. For-example, lets create an abstract class called Shape:

cs

index.cs

Similarly, we can create an abstract method by adding the keyword abstract before its return type. An abstract method does not have any body - it is simply a blueprint:

cs

index.cs

The purpose of this is to create a blueprint for other classes. This helps in simplifying the code. To understand this better, let's look at the Polymorphism task from Chapter 5:

cs

index.cs

In the above example, we never intend to use the Shape class however we still had to write some bogus implementations of the getArea and calculatePerimeter methods inside the Shape class. We can somewhat simplify this code by making the Shape class abstract, apart from that we can also make the getArea and calculatePerimeter methods abstract.

A method can either be abstract or virtual but not both at the same time. However it is important to note that an abstract method is basically a virtual (overridable) method but it doesn't have any body in the base class.
cs

index.cs

1. Which keyword is used for making a class abstract?
2. Can an abstract class be instantiated?

Which keyword is used for making a class abstract?

Виберіть правильну відповідь

Can an abstract class be instantiated?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 5. Розділ 8
some-alt