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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:50:01+00:00 2026-05-16T05:50:01+00:00

I have some rudimentary question about inheritance in C#. I have two classes. I

  • 0

I have some rudimentary question about inheritance in C#.

I have two classes. I want the child class to inherit all members of the base class (e.g. x and y), but it seems to use inheritance I should initialize the base constructor. If I define another x and y in child class so that i later initialize the base constructor so what is the use of inheritance? And what is the solution?

The other question is that I have two methods with the same signature in base and child class and i need to use both. What can i do?

class Base
{
    int x;
    int  y;
    string name;

    public Base(int i,int j)
    {
        x = i;
        y = j;
        name="s";
    }
}

class Child
{
    public Base()
    {

    }
}
  • 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-16T05:50:02+00:00Added an answer on May 16, 2026 at 5:50 am

    Your questions are not very clear, and the code is invalid [now fixed by the editor] but I am going to answer what I think you are asking.

    To initialize fields of a base class in a derived class, you must call the base constructor from the constructor of your derived class:

    class Base
    {
        private int x;
        private int y;
    
        // ...
    
        public Base(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
    
    // option 1, pass in values as parameters to constructor:
    class Child1
    {
        public Child(int x, int y)
            : base(x, y) // this passes the values up to the base constructor
        {
        }
    }
    
    // option 2, pass literals to the base constructor:
    class Child2
    {
        public Child()
            : base(3, 4) // this passes the literal values 3 and 4 to the base constructor
        {
        }
    }
    
    // elsewhere
    
    var child1 = new Child1(1, 2); // child1.x = 1, child1.y = 2
    var child2 = new Child2(); // child2.x = 3, child2.y = 4
    

    Now, regarding your second question regarding having a method in the derived class with the same name. If you wish to redefine the behaviour of a method, you should make it virtual in the base class and override it in the derived class, which you can see from the method called SomeAction in this example:

    class Base
    {
        public virtual void SomeAction()
        {
            Console.Out.WriteLine("Base.SomeAction");
        }
    
        public void DifferentAction()
        {
            Console.Out.WriteLine("Base.DifferentAction");
        }
    }
    
    class Derived
    {
        // this is a normal override
        public override void SomeAction()
        {
            Console.Out.WriteLine("Derivde.SomeAction");
        }
    
        // this is an advanced technique which you should try to avoid in most cases
        public new void DifferentAcion()
        {
            Console.Out.WriteLine("Derived.DifferentAction");
        }
    }
    
    // elsewhere
    var base = new Base();
    base.SomeAction(); // prints Base.SomeAction
    base.DifferentAction(); // prints Base.DifferentAction
    
    var derived = new Derived();
    derived.SomeAction(); // prints Derived.SomeAction
    derived.DifferentAction(); // prints Derived.DifferentAction
    
    ((Base) derived).SomeAction(); // prints Derived.SomeAction
    ((Base) derived).DifferentAction(); // prints Base.DifferentAction
    

    If a method is overriden, then the new behaviour will be exhibited regardless of whether you reference it as the base type or the derived type.

    (If you use the new keyword, or don’t put anything, then a new inheritance chain is created and the behaviour will depend upon whether you reference the object as the base type or the derived type. You can see this in the final two calls in the code above: for the overriden method the derived behaviour is used but for the method where ‘new’ was used the orginal behaviour is exhibited when the object is treated as a Base.)

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

Sidebar

Related Questions

I only have some very rudimentary theoretical knowledge about RSA. While reading different sources
I have some files stored at amazon. all in private mode, and since I
I have some interesting problem for an hour.. In my flex project, all width
I have some code which populates a hashtable with a question as the key
I am new to Git and have done some rudimentary tutorials on how to
I have a WCF service with a security class for getting some of the
I'm implementing some rudimentary SQL Server monitoring to watch for excessive blocking. I have
I want to do some rudimentary things with RMonad . Are there ways of
I have some files that are uuencoded, and I need to decode them, using
I have some script in my default page that redirects users to language specific

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.