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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:38:44+00:00 2026-05-25T17:38:44+00:00

No problems here, just need explanation how does that work. I was doing homework

  • 0

No problems here, just need explanation how does that work.

I was doing homework for my C# class and I managed to do it by myself by following code examples provided by our professor. The problem is I don’t get how it works. Here are things that boggle me:

First of all, how come I need to use xmlBook.Title = "XML Primer Plus"; instead of Book clrBook = new Book("CLR via C#", ...") and vice-versa as constructors.

Second, why I don’t have to have any parameters when using : base()?

Third, how does overwrite by using new public void display() only adds output, instead of completely modifying original protected void display()? I guess because the original diplay() is protected?

Please clarify

Regards.

Main.cs

using System;

namespace Lab_4
{
    class Program
    {
        static void Main(string[] args)
        {
            Book xmlBook = new Book();
            xmlBook.Title = "XML Primer Plus";
            xmlBook.AuthorFirstName = "Nicolas";
            xmlBook.AuthorLastName = "Chase";
            xmlBook.Price = 44.99F;
            xmlBook.PublisherName = "Sams Publishing";

            Book clrBook = new Book("CLR via C#", 
                                    "Jeffrey", 
                                    "Richter", 
                                    59.99f, 
                                    "Microsoft Press");

            Console.WriteLine("=== xmlBook ===");
            xmlBook.display();
            Console.WriteLine();
            Console.WriteLine("=== clrBook ===");
            clrBook.display();
        }
    }
}

Publication.cs

using System;

namespace Lab_4
{
    public class Publication
    {
        string publisherName, title;
        float price;

        public Publication()
        {
        }

        public Publication(string title, 
                           string publisherName, 
                           float price)
        {
            Title = title;
            PublisherName = publisherName;
            Price = price;
        }

        public float Price
        {
            set
            {
                price = value;
            }
        }

        public string PublisherName
        {
            set
            {
                publisherName = value;
            }
        }

        public string Title
        {
            set
            {
                title = value;
            }
        }

        protected void display()
        {
            Console.Write("{0}\n{1}\n{2}\n", title, publisherName, price);
        }
    }
}

Book.cs

using System;

namespace Lab_4
{
   public class Book : Publication
    {
        string authorFirstName, authorLastName;

        public Book()
        {
        }

        public Book(string bookTitle, 
                    string firstName, 
                    string lastName, 
                    float bookPrice, 
                    string publisherName)
           : base()
        {
            Title = bookTitle;
            AuthorFirstName = firstName;
            AuthorLastName = lastName;
            Price = bookPrice;
            PublisherName = publisherName;
        }

        public string AuthorFirstName
        {
            get
            {
                return authorFirstName;
            }
            set
            {
                authorFirstName = value;
            }
        }

        public string AuthorLastName
        {
            get
            {
                return authorLastName;
            }
            set
            {
                authorLastName = value;
            }
        }

        new public void display()
        {
            base.display();
        Console.WriteLine("{0}", getAuthorName());
        }

        string getAuthorName()
        {
            return AuthorFirstName + " " + AuthorLastName;
        }
    }
}
  • 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-25T17:38:45+00:00Added an answer on May 25, 2026 at 5:38 pm

    First of all, how come I need to use xmlBook.Title = “XML Primer Plus”; instead of Book clrBook = new Book(“CLR via C#”, …”) and vice-versa as constructors.

    You are free to swap them if you like. Either option works, which is what that code is trying to demonstrate.

    There are multiple constructors for the Book class. From code outside the class (like in your Main method), you are allowed to call any constructor of the class that is marked public.

    The reason you need to set the properties when you call new Book(); is because that constructor doesn’t set the properties of the class. When you call new Book("CLR via C#", "Jeffrey", "Richter", etc);, you are calling the constructor that does set the properties.

    Second, why I don’t have to have any parameters when using : base()?

    The base class (Publication) also has two constructors. When calling base(), you are calling the constructor of Publication that takes no parameters.

    When you call the constructor for a derived class, some constructor for the base class will always get called. If you don’t explicitly specify which of the base class’s constructors you want to call (by calling base() or base("some title", "some publisher name", 0.0f /* some price */)), then the parameterless constructor (i.e. public Publication()) will be called by default.

    Third, how does overwrite by using new public void display() only adds output, instead of completely modifying original protected void display()? I guess because the original diplay() is protected?

    It doesn’t have anything to do with it being protected. Try changing it to public, and you’ll see the same behavior.

    The way it “adds” behavior is by actually calling that function in the base class. The line base.display(); calls that function. If that line weren’t there, the base class’s function would effectively be “replaced”.

    There is only one thing that protected means. It means you can’t call it from outside code – you can only call it from within the same class (from within Publication) and from inside a derived class (Book). protected will not “protect” it from being overridden in derived classes (either using override or new).

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

Sidebar

Related Questions

I'm following the kernel tutorial from here im having problems compiling my files. i
I'm having some problems with getting the .getruntime.exec() to work properly. Here is the
I need to work around an NHibernate bug I reported here . Essentially, NHibernate
Can anyone figure out a nice way to get the following code to work?
Just a small SVN problem here. I setup my own SVN server Setting up
I have just been helped on a problem I have here . var win
I am having problems here if I want to check if eerste points to
I'm trying to compile a Flex application in Ant (no problems here, I can
I'm new around here and i have a little problems with a C# application.
Looking for some direction here as I'm running into some migration problems. We have

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.