About this question
Hi I'm just confused on how to to use Abstract class and Virtual. Can't seem to find any difference between them and what are some scenarios where in it's applicable to use abstract or virtual.
Hi I'm just confused on how to to use Abstract class and Virtual. Can't seem to find any difference between them and what are some scenarios where in it's applicable to use abstract or virtual.
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Full Stack Developer Expert
Answered on Aug 1, 2021
tl;dr: have c# virtual vs abstractPractically speaking, there isn't much difference. Here we The difference has historically been quite subtle.
c# virtual vs abstract
Both virtual and abtract classes allow you to extend the class (i.e. create child classes that inherit non-private methods and variables) A virtual class can be instantiated directly, whereas an abstract class cannot Both virtual and abstract classes can contain virtual methods (virtual methods can have a default implementation that is inherited by child classes, whereas abstract methods can only be signatures, and must be implemented in child classes) Only abstract classes may contain abstract methods Virtual methods have an implementation and provide the derived classes with the option of overriding it. Abstract methods do not provide an implementation and force the derived classes to override the method. So, abstract methods have no actual code in them, and subclasses HAVE TO override the method. In practice, I haven't seen much practical difference between the two. The abstract and virtual modifiers, in my mind, are more important for methods (as opposed to classes). If you're going to use a class that's either abstract or virtual, I've found that I'm more likely to be using that class as more of a contract (i.e. when you use this class, we guarantee that variables a, b, and c will be available for use, along with methods x, y, and z), rather than a specialization that is used apart, distinctly, from its parent class. This mostly comes down to the philosophy of object-oriented design that I have learned as part of my professional career. To put it into a single sentence: If you're relying on something having a specific implementation (i.e. you need to use a square or a rectangle instead of a more general parallelogram), then you can probably improve your solution. The general idea is that by relying on contracts rather than implementations, your code is more resistant to change. Put another way, by relying on contracts, a change to a specific implementation of that contract is contained to one or two classes (as opposed to making a change in one class ending up requiring a change to many other classes). So for that goal (relying on a contract more than an implementation), I find myself using abstract classes more often because I tend to want the consumers of my code to use specific, specialized implementations (to suit the task at hand) rather than a general "catch-all" implementation. To that end, abstract, with it's inability to be directly constructed, is more useful to me.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Most-viewed questions across JanBask
Guides, tips and career advice from JanBask experts.
QA Testing Software Tester Job Demand: Job Market, Outlook & Most In-Demand Skills
Software tester job demand in 2027: BLS projects 6% growth and a $104,300 median wage. See which roles are growing, how AI fits…
QA Testing Automation Tester Career Path: Stages, Pay, and Where It Leads
Automation tester career path explained: stages from junior to architect, US pay, the manager vs. specialist fork, and how AI…
QA Testing Manual Tester vs Automation Tester: Differences, Skills, Pay, and Which Role Fits You
Manual tester vs automation tester: compare daily work, skills, tools, pay and career paths, then see which role fits you best.
QA Testing Automation Tester Job Demand: What the Job Data Really Says
Is automation testing in demand? See job growth, pay by title, the skills employers want, and how to move into an automation…