UrbanPro

Learn .Net Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

When can we use the abstract class in C#.NET?

Asked by Last Modified  

1 Answer

Learn .Net

Follow 1
Answer

Please enter your answer

In C#.NET, an abstract class is a class that cannot be instantiated and may contain abstract methods, which are methods without a body. Abstract classes are used when you want to provide a common base class for other classes but want to leave certain methods as abstract, letting the derived classes provide...
read more

In C#.NET, an abstract class is a class that cannot be instantiated and may contain abstract methods, which are methods without a body. Abstract classes are used when you want to provide a common base class for other classes but want to leave certain methods as abstract, letting the derived classes provide their own implementation. Here are some scenarios where you might use abstract classes:

1. **Common Base Class:**
- You can use an abstract class to define a common base class for a group of related classes. This allows you to provide a shared set of fields, properties, and methods to the derived classes.

```csharp
public abstract class Shape
{
public abstract double CalculateArea();
public abstract double CalculatePerimeter();
}

public class Circle : Shape
{
public double Radius { get; set; }

public override double CalculateArea()
{
return Math.PI * Math.Pow(Radius, 2);
}

public override double CalculatePerimeter()
{
return 2 * Math.PI * Radius;
}
}

public class Square : Shape
{
public double SideLength { get; set; }

public override double CalculateArea()
{
return Math.Pow(SideLength, 2);
}

public override double CalculatePerimeter()
{
return 4 * SideLength;
}
}
```

2. **Enforcing a Contract:**
- Abstract classes can define abstract methods that must be implemented by derived classes. This enforces a contract, ensuring that all derived classes provide their own implementation for specific methods.

```csharp
public abstract class DatabaseConnection
{
public string ConnectionString { get; set; }

public abstract void Open();
public abstract void Close();
public abstract void ExecuteQuery(string query);
}
```

3. **Partial Implementation:**
- Abstract classes can provide a partial implementation of methods, leaving some parts to be implemented by derived classes. This is useful when there's common functionality shared among derived classes.

```csharp
public abstract class Animal
{
public void Eat()
{
Console.WriteLine("Animal is eating.");
}

public abstract void MakeSound();
}

public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Dog barks.");
}
}

public class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("Cat meows.");
}
}
```

4. **Preventing Direct Instantiation:**
- Since abstract classes cannot be instantiated directly, they prevent the creation of objects of the abstract class itself. Instances are created through derived classes.

```csharp
// This is not allowed:
// Animal animal = new Animal();

// This is allowed:
Animal dog = new Dog();
```

In summary, abstract classes are used in C#.NET when you want to provide a common base with some default implementation and leave certain methods to be implemented by derived classes. They help in creating a consistent and extensible class hierarchy.

read less
Comments

Related Questions

How to create different types of cells references in Excel
A cell reference refers to a cell or a range of cells on a worksheet and can be used in a formula so that Microsoft Office Excel can find the values or data that you want that formula to calculate. In...
Vijayalakshmi G M
I am new to ASP.Net MVC.I need to learn ASP.Net MVC .I need an Online training course for that.Kindly suggest a good online training website for ASP.Net MVC
Hi Shubhatra.. Many materials are available online for ASP.NET MVC course. You can find many tutorials videos. Try to make small projects using those tutorials,not just read or watch them. Remember best...
Shubhabrata
Are Frontend developers that know C# and ASP.Net in demand?
C# and ASP.Net are not much in demand now a days. Few projects which are in maintenance, you may get vacancies. Upgrade your skills to Angular would be a better idea.
Taj
0 0
8
What is the major difference between Linq and SQL?
Jayant, Both are entirely different concepts so they can't be compared for difference. SQL is query writing mechanism which database developer used to write inside a RDBMS to insert, fetch records. While...
Jayant

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Multi-core CPU utilization using Task Parallel Library in c#
Nowadays, we are using multiple core systems. We must write our .NET applications in such a way that we must utilise the complete computing power of the machine. The parallel task library(TPL) allows...

Working with Delegates & Events in .NET
Events & Delegates A delegate is a reference type derived from the System.Delegate class which can be used to call methods of same matching signature.As you all know signature of a method is defined...

Pro Developer Program Syllabus
What you will learn in Pro-Developer Program: i. Introduction to Web Technologies. ii. Introduction to .Net - Features of .Net, CTS, CLS, CLR and MSIL. iii. C# & .Net Basics - Data Types,...

Format Date to “MM/dd/yyyy” in Webgrid ASP.Net MVC Razor View
Replace above line with below to apply ("DD/mm/yyyy") formatting to date field in WebGrid AllTicket.Column(columnName: "DueDate", header: "Due Date") grid.Column("Date",format:@<text> @item.Date.ToString("MM/dd/yyyy")</text>)
M

Mohammad Shafi

0 0
0

Differences Between Execution Query, Execute Scalar and Execute Reader.
ExecuteNonQuery ExecuteNonQuery method will return number of rows effected with INSERT, DELETE or UPDATE operations. This ExecuteNonQuery method will be used only for insert, update and delete, Create,...

Recommended Articles

Hadoop is a framework which has been developed for organizing and analysing big chunks of data for a business. Suppose you have a file larger than your system’s storage capacity and you can’t store it. Hadoop helps in storing bigger files than what could be stored on one particular server. You can therefore store very,...

Read full article >

Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a “significant" increase in demand for IT professionals in 2014 as compared to 2013, a “steady” demand for IT professionals is rest assured...

Read full article >

Business Process outsourcing (BPO) services can be considered as a kind of outsourcing which involves subletting of specific functions associated with any business to a third party service provider. BPO is usually administered as a cost-saving procedure for functions which an organization needs but does not rely upon to...

Read full article >

Microsoft Excel is an electronic spreadsheet tool which is commonly used for financial and statistical data processing. It has been developed by Microsoft and forms a major component of the widely used Microsoft Office. From individual users to the top IT companies, Excel is used worldwide. Excel is one of the most important...

Read full article >

Looking for .Net Training ?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for .Net Training Classes?

The best tutors for .Net Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn .Net Training with the Best Tutors

The best Tutors for .Net Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more