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 7649905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:06:49+00:00 2026-05-31T11:06:49+00:00

I seem to have forgotten some of the most basic rules of inheritance because

  • 0

I seem to have forgotten some of the most basic rules of inheritance because I can’t figure out why this won’t work. I have a class SuffixNode which extends Node.

Node:

class Node
{
    public char label;
    public Node parent;
    public Dictionary<char,Node> children;

    public Node(Node NewParent, char NewLabel)
    {
        this.parent = NewParent;
        this.label = NewLabel;
        children=new Dictionary<char,Node>();
    }
}

SuffixNode:

class SuffixNode: Node
{
    public Dictionary<String, int> Location=new Dictionary<String, int>();

    public SuffixNode(Node NewParent):base(NewParent, '$')
    {

    }

    public void AddLocation(String loc,int offset)
    {
        this.Location.Add(loc, offset);
    }
}

I’m trying to call the AddLocation method in the main program from the SuffixNode class but it gives me an error saying that no such method exists (in Node class):

Node n;
char FirstChar = suffix[0]; //first character of the suffix 
if (suffix == "")
{
     return true;
}

//If the first character of a suffix IS NOT a child of the parent
if (!parent.children.ContainsKey(FirstChar))
{
     if (FirstChar == '$')
     {
          n = new SuffixNode(parent);
          n.AddLocation(document, offset);
     }
     else
     {
          n = new Node(parent, FirstChar); //Create a new node with the first char of the suffix as the label
          parent.children.Add(FirstChar, n); //Add new node to the children collection of the parent
     }
}          

I’m sure it’s an extremely simple answer, but I just can’t realise why this isn’t working. Shouldn’t

Node n = new SuffixNode(parent)

allow me to access the SuffixNode methods and variables?

  • 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-05-31T11:06:50+00:00Added an answer on May 31, 2026 at 11:06 am

    Many answers have correctly stated that you cannot call a method defined in a derived class on a variable whose type is the base class. None has noted that this is fundamental to the type safety provided by C#’s static typing.

    When you call a method on a variable, that method call is resolved at compile time, and there’s no way the compiler can resolve n.AddLocation when n is a Node-type variable.

    The alternative would be to resolve the call at run-time, which could result in an exception if the referent of n is an instance of some other subclass of Node that doesn’t have an AddLocation method (or indeed an instance of Node itself). The C# type system is explicitly designed to avoid that situtation.

    Consider:

    object o1 = "Hello, World!";
    Console.WriteLine(o1.Length); //hypothetically fine
    
    object o2 = Math.PI;
    Console.WriteLine(o2.Length); //exception!
    

    The philosophy of C# is fail fast: catch coding errors at compile time whenever possible, because errors caught at compile time are much easier to fix than those caught at run time.

    The trivial solution, without changing your object model, would be to introduce a new variable inside the relevant block:

    if (FirstChar == '$') 
    { 
        SuffixNode sn = new SuffixNode(parent); 
        sn.AddLocation(document, offset); 
        n = sn;
    } 
    else 
    { 
        n = new Node(parent, FirstChar); //Create a new node with the first char of the suffix as the label 
        parent.children.Add(FirstChar, n); //Add new node to the children collection of the parent 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I seem to have a bizarre error I just can't quite figure out. My
This may seem silly but I seem to have forgotten the order of replacement
I know this is a dumb question but I seem to have totally forgotten
I seem to have a problem passing some strings on from one form to
i seem to have some trouble installing autopy.h https://github.com/msanders/autopy/#introduction i already tried the installation
I seem to have the exact opposite problem than this question on stopping dock
I'm an intermittent programmer and seem to have forgotten a lot of basics recently.
Ok, I fear that this is just me having forgotten some small stupid thing
The Eclipselink OSGi bundles seem have trouble working in some OSGi containers, notably Felix,
I seem to have looked everywhere for this. Here is the code i use

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.