Abstraction
Abstraction is a fundamental concept in object-oriented programming (OOP) that allows you 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 you 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.
You can create an abstract class by adding the abstract
keyword before the class definition. For example, let's create an abstract class called Shape
:
index.cs
12345678910111213141516171819using System; abstract class Shape { protected float circumference; public float getCircumference() { return circumference; } } class ConsoleApp { static void Main() { Shape s = new Shape(); // Error: Cannot create an instance of an abstract class } }
Similarly, you 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:
index.cs
1234567891011abstract class Shape { protected float circumference; public float getCircumference() { return circumference; } public abstract float getArea(); }
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:
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102using System; class Shape { // Perimeter is the length of the 'outline' of a shape protected float perimeter; public float getPerimeter() { return perimeter; } public virtual float getArea() { return 0.0f; } // A virtual method can only be either 'public' or 'protected' protected virtual float calculatorPerimeter() { return 0.0f; } } class Rectangle : Shape { float width; float height; public Rectangle(float width, float height) { this.width = width; this.height = height; this.perimeter = getPerimeter(); } public override float getArea() { return width * height; } protected override float calculatorPerimeter() { return width * 2 + height * 2; } } class Square : Shape { float length; public Square(float length) { this.length = length * length; } public override float getArea() { return length * length; } protected override float calculatorPerimeter() { return 4 * length; } } class Circle : Shape { float radius; public Circle(float radius) { this.radius = radius; } // Area of a Circle = pi . r . r public override float getArea() { return 3.14f * radius * radius; } // Perimeter (or) Circumference: 2 . pi . r protected override float calculatorPerimeter() { return 2.00f * 3.14f * radius; } } class ConsoleApp { static void Main() { Rectangle r1 = new Rectangle(10f, 20f); Square s1 = new Square(10f); Circle c1 = new Circle(10f); Console.WriteLine(r1.getArea()); Console.WriteLine(s1.getArea()); Console.WriteLine(c1.getArea()); } }
In the above example, you never intend to use the Shape
class however you still had to write some bogus implementations of the getArea
and calculatePerimeter
methods inside the Shape
class. You can somewhat simplify this code by making the Shape
class abstract, apart from that you 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.
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100using System; using System.Collections.Generic; abstract class Shape { // Perimeter is the length of the 'outline' of a shape protected float perimeter; public float getPerimeter() { return perimeter; } public abstract float getArea(); protected abstract float calculatorPerimeter(); } class Rectangle : Shape { float width; float height; public Rectangle(float width, float height) { this.width = width; this.height = height; this.perimeter = getPerimeter(); } public override float getArea() { return width * height; } protected override float calculatorPerimeter() { return width * 2 + height * 2; } } class Square : Shape { float length; public Square(float length) { this.length = length * length; } public override float getArea() { return length * length; } protected override float calculatorPerimeter() { return 4 * length; } } class Circle : Shape { float radius; public Circle(float radius) { this.radius = radius; } public override float getArea() { return 3.14f * radius * radius; } protected override float calculatorPerimeter() { return 2.00f * 3.14f * radius; } } class ConsoleApp { static void Main() { Rectangle r1 = new Rectangle(10f, 20f); Square s1 = new Square(10f); Circle c1 = new Circle(10f); // We cannot create an instance of 'Shape' but we can use type datatype "Shape" for creating variables, arrays or lists List<Shape> shapes = new List<Shape>(); shapes.Add(r1); shapes.Add(s1); shapes.Add(c1); foreach(Shape shape in shapes) { Console.WriteLine(shape.getArea()); } } }
1. Which keyword is used for making a class abstract?
2. Can an abstract class be instantiated?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.04
Abstraction
Swipe to show menu
Abstraction is a fundamental concept in object-oriented programming (OOP) that allows you 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 you 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.
You can create an abstract class by adding the abstract
keyword before the class definition. For example, let's create an abstract class called Shape
:
index.cs
12345678910111213141516171819using System; abstract class Shape { protected float circumference; public float getCircumference() { return circumference; } } class ConsoleApp { static void Main() { Shape s = new Shape(); // Error: Cannot create an instance of an abstract class } }
Similarly, you 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:
index.cs
1234567891011abstract class Shape { protected float circumference; public float getCircumference() { return circumference; } public abstract float getArea(); }
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:
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102using System; class Shape { // Perimeter is the length of the 'outline' of a shape protected float perimeter; public float getPerimeter() { return perimeter; } public virtual float getArea() { return 0.0f; } // A virtual method can only be either 'public' or 'protected' protected virtual float calculatorPerimeter() { return 0.0f; } } class Rectangle : Shape { float width; float height; public Rectangle(float width, float height) { this.width = width; this.height = height; this.perimeter = getPerimeter(); } public override float getArea() { return width * height; } protected override float calculatorPerimeter() { return width * 2 + height * 2; } } class Square : Shape { float length; public Square(float length) { this.length = length * length; } public override float getArea() { return length * length; } protected override float calculatorPerimeter() { return 4 * length; } } class Circle : Shape { float radius; public Circle(float radius) { this.radius = radius; } // Area of a Circle = pi . r . r public override float getArea() { return 3.14f * radius * radius; } // Perimeter (or) Circumference: 2 . pi . r protected override float calculatorPerimeter() { return 2.00f * 3.14f * radius; } } class ConsoleApp { static void Main() { Rectangle r1 = new Rectangle(10f, 20f); Square s1 = new Square(10f); Circle c1 = new Circle(10f); Console.WriteLine(r1.getArea()); Console.WriteLine(s1.getArea()); Console.WriteLine(c1.getArea()); } }
In the above example, you never intend to use the Shape
class however you still had to write some bogus implementations of the getArea
and calculatePerimeter
methods inside the Shape
class. You can somewhat simplify this code by making the Shape
class abstract, apart from that you 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.
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100using System; using System.Collections.Generic; abstract class Shape { // Perimeter is the length of the 'outline' of a shape protected float perimeter; public float getPerimeter() { return perimeter; } public abstract float getArea(); protected abstract float calculatorPerimeter(); } class Rectangle : Shape { float width; float height; public Rectangle(float width, float height) { this.width = width; this.height = height; this.perimeter = getPerimeter(); } public override float getArea() { return width * height; } protected override float calculatorPerimeter() { return width * 2 + height * 2; } } class Square : Shape { float length; public Square(float length) { this.length = length * length; } public override float getArea() { return length * length; } protected override float calculatorPerimeter() { return 4 * length; } } class Circle : Shape { float radius; public Circle(float radius) { this.radius = radius; } public override float getArea() { return 3.14f * radius * radius; } protected override float calculatorPerimeter() { return 2.00f * 3.14f * radius; } } class ConsoleApp { static void Main() { Rectangle r1 = new Rectangle(10f, 20f); Square s1 = new Square(10f); Circle c1 = new Circle(10f); // We cannot create an instance of 'Shape' but we can use type datatype "Shape" for creating variables, arrays or lists List<Shape> shapes = new List<Shape>(); shapes.Add(r1); shapes.Add(s1); shapes.Add(c1); foreach(Shape shape in shapes) { Console.WriteLine(shape.getArea()); } } }
1. Which keyword is used for making a class abstract?
2. Can an abstract class be instantiated?
Thanks for your feedback!