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

  • SEARCH
  • Home
  • 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 8631125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:10:28+00:00 2026-06-12T09:10:28+00:00

Say I have an abstract parent class called Parent that implements a method called

  • 0

Say I have an abstract parent class called “Parent” that implements a method called “DisplayTitle”. I want this method to be the same for each subclass that inherits “Parent” – I would like a compile error if a subclass attempts to implement their own “DisplayTitle” method. How can I accomplish this in C#. I believe in Java, I’d just mark the method as “final”, but I can’t seem to find an alternative in C#. I’ve been messing around with “sealed” and “override”, but I can’t get the behavior that I’m looking for.

For example, in this code:

using System;

namespace ConsoleApplication1
{
  class Parent
  {
      public void DisplayTitle() { Console.WriteLine("Parent's Title"); }
  }

  class ChildSubclass : Parent
  {
      public void DisplayTitle() { Console.WriteLine("Child's Own Implementation of Title"); 
  }

    static void Main(string[] args)
    {
        ChildSubclass myChild = new ChildSubclass();
        myChild.DisplayTitle();
        Console.ReadLine();
    }
  }
}

I’d like to receive a compile error saying that the “ChildSubClass” can’t override “DisplayTitle”. I currently get a warning – but it seems like this is something that I should be able to do and I don’t know the proper attributes to label the method.

  • 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-12T09:10:29+00:00Added an answer on June 12, 2026 at 9:10 am

    How can I accomplish this in C#. I believe in Java, I’d just mark the method as “final”, but I can’t seem to find an alternative in C#.

    The rough equivalent is sealed in C#, but you normally only need it for virtual methods – and your DisplayTitle method isn’t virtual.

    It’s important to note that ChildSubclass isn’t overriding DisplayTitle – it’s hiding it. Any code which only uses references to Parent won’t end up calling that implementation.

    Note that with the code as-is, you should get a compile-time warning advising you to add the new modifier to the method in ChildSubclass:

    public new void DisplayTitle() { ... }
    

    You can’t stop derived classes from hiding existing methods, other than by sealing the class itself to prevent the creation of a derived class entirely… but callers which don’t use the derived type directly won’t care.

    What’s your real concern here? Accidental misuse, or deliberate problems?

    EDIT: Note that the warning for your sample code would be something like:

    Test.cs(12,19): warning CS0108:
            'ConsoleApplication1.ChildSubclass.DisplayTitle()' hides inherited
            member 'ConsoleApplication1.Parent.DisplayTitle()'. Use the new keyword
            if hiding was intended.
    

    I suggest you turn warnings into errors, and then it’s harder to ignore them 🙂

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

Sidebar

Related Questions

Let's say I have an abstract parent class called shape, and that there are
Let say I have abstract class called: Tenant and Customer. The tenant in this
Let's say I have something like this: abstract class Parent { protected function foobar($data)
Let's say that I have this: public abstract class myClass<T> : Ob<T> where T
Say I have an abstract base class that has a method to generate Foo
Let's say I have an abstract base class that looks like this: class StellarObject(BaseModel):
Say I have code like this: abstract class Animal[T <: Animal[T]] { def mateWith(that:
Let's say I have these class hierarchy : public abstract class Parent { }
I have one abstract class -let's say myBase. And I want all the classes
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {

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.