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, 2022 in C# by krishna
• 2,820 points
568 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, 2022 by jyoti
• 1,240 points

Related Questions In C#

0 votes
0 answers

When to use an interface instead of an abstract class and vice versa

This could be an OOP question in ...READ MORE

Jun 11, 2022 in C# by pranav
• 2,590 points
377 views
0 votes
1 answer

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

The compiler creates a state machine in ...READ MORE

answered Jul 4, 2022 in C# by krishna
• 2,820 points
546 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, 2022 in C# by rajiv
• 1,620 points
1,801 views
0 votes
1 answer

When would you use delegates in C#

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

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

SQLite.Net not being able to create text file in Win IoT Library!

That exception comes when access limited to ...READ MORE

answered Aug 3, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,002 views
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
1,733 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
944 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
911 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, 2022 in C# by jyoti
• 1,240 points
426 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, 2022 in C# by jyoti
• 1,240 points
1,502 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP