Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8157673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:21:48+00:00 2026-06-06T17:21:48+00:00

So apparently you cannot use the virtual modifier with the override modifier. virtual –

  • 0

So apparently you cannot use the virtual modifier with the override modifier.

virtual – a method that can be overridden

override – a method that is overriding a method of the same name in its parent’s class

This leads me to believe that if I override a method in a child class, if that child has a child you can’t override that method again.

And it is safe to say that if you put override and virtual in a method declaration you will get a compile error in C#.

However I can’t understand why the code I made below works the way in which it does

using System;
public class DrawingObject
{
public virtual void Draw()
{
    Console.WriteLine("Drawing Object");
}
}
public class DrawDemo
{
     public static int Main()
     {
          DrawingObject[] dObj = new DrawingObject[3];


          dObj[0] = new DrawingObject();
           dObj[1] = new Line();
           dObj[2] = new LittleLine();

           foreach (DrawingObject drawObj in dObj)
           {
                drawObj.Draw();
           }
            Console.Read();
           return 0;
       }
  }
 public class Line : DrawingObject
 {
       public override void Draw()
       {// the method above me is in fact virtual because LittleLine overid it?
               Console.WriteLine("I'm a Line.");
       }
  }
  public class LittleLine : Line
  {
       public override void Draw()
       {
              Console.WriteLine("I'm a Little Line.");
         }
    }

Here’s the output:

Drawing Object

I’m a Line.

I’m a Little Line.

So the draw method in Line looks as though it was overridden by LittleLine. Is this code not actually overriding it, or is the compiler doing some other trick? Or am I not understanding the context of virtual and override?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:21:50+00:00Added an answer on June 6, 2026 at 5:21 pm

    You can declare a certain method as virtual only once, but you can override it as many times as you want – an override is not final, and it does not limit classes that inherit from the first overriding class. The method that will eventually execute is the last one the overrides the virtual method. So your code does behave as expected.

    C# is very verbose with regard to overriding – you have more specifiers than C++ or Java. It is so to let the programmer specify the exact intent:

    • You use virtual to specify a method can be overridden by subclasses.
    • You use override to specify you are overriding a method that you know is virtual (if it’s not, the compiler will report an error).
    • You use sealed to prevent further overriding.
    • And you use new to hide instead of override.

    This can be confusing and sometimes annoying, but it ensures you actually know what you’re doing, and it makes your intention self-documented.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since I'm using Java 1.4.2, this means that I cannot use Java's implementation of
Apparently, you cannot use the normal + operator to append strings in jsp...at least
Apparently, you cannot use a null for a key, even if your key is
I'm trying to use getResources().getXml so that I can access an XML file in
This is very weird. Apparently, I can use both .val() and .text() to manipulate
I am writing a method that should accept as its parameter an object of
PowerBuilder 10.5 Apparently, in a function I cannot pass an argument of the base
The code shows compilation with the -cp trigger but not running. Apparently, it cannot
Apparently MapReduce queries are one of the slowest things one can do in MongoDB
I saw this similar question here but can't figure out how to use Contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.