When to use abstract classes

0 votes
I read the abstract classes on MSDN. Didn't understand it.

When is it appropriate to utilise abstract classes? What are some of the benefits of abstract classes?
Jun 11 in C# by krishna
• 1,340 points
12 views

1 answer to this question.

0 votes
When you need a class for inheritance and polymorphism, but you don't want to instantiate the class itself, only its subclasses, abstract classes come in handy. They're frequently used when you want to provide a template for a group of subclasses that have some common implementation code, but you also want to ensure that the superclass's objects aren't created.

Let's imagine you need to make stuff for a dog, a cat, a hamster, and a fish. They share characteristics like as colour, size, and leg count, as well as behaviour, therefore you establish an Animal superclass. What colour, on the other hand, is an Animal? What is the number of legs on an Animal object? In this situation, instantiating an object of type Animal rather than its subclasses makes more sense.

Polymorphism is another advantage of abstract classes: you can use the type of the (abstract) superclass as a method argument or a return type. Instead of writing a method for each subtype of Animal, if you had a PetOwner class with a train() method, you could define it as taking in an object of type Animal, e.g. train(Animal a).
answered Jun 17 by jyoti
• 1,240 points

Related Questions In C#

0 votes
0 answers

How and when to use ‘async’ and ‘await’

One of the key benefits of async ...READ MORE

Jun 11 in C# by jyoti
• 1,240 points
2 views
0 votes
1 answer

Which design pattern to use for pre-process process and post-process task

You have pre-defined and precise steps to ...READ MORE

answered Jun 9 in C# by rajiv
• 1,620 points
12 views
0 votes
1 answer

When would you use delegates in C#

Delegates come in handy for a variety ...READ MORE

answered Jun 11 in C# by pranav
• 2,320 points
11 views
0 votes
1 answer

When should you use C# indexers

First, let's talk about indexer. Class (or struct) ...READ MORE

answered 5 days ago in C# by krishna
• 1,340 points
8 views
0 votes
1 answer
0 votes
2 answers

Is there a .NET equivalent to Apache Hadoop?

Hadoop is a Java-based platform. So, to ...READ MORE

answered Jul 16, 2020 in Big Data Hadoop by Suhana
• 340 points
632 views
0 votes
1 answer

Validate String against USPS State Abbreviations

Try something like this: private static String states ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
216 views
0 votes
1 answer

Authenticate on an ASP.Net Forms Authorization website from a console app

Essentially, we need to record a regular ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
247 views
0 votes
1 answer

How to enumerate an enum

foreach (Suit suit in (Suit[]) Enum.GetValues(typeof(Suit))) { } Note that ...READ MORE

answered Jun 14 in C# by jyoti
• 1,240 points
9 views
0 votes
1 answer

How to get substring from string containing newlines

If you want to maintain the newline ...READ MORE

answered Jun 14 in C# by jyoti
• 1,240 points
8 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP