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;
}
}
}
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
Bookclass. From code outside the class (like in yourMainmethod), you are allowed to call any constructor of the class that is markedpublic.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 callnew Book("CLR via C#", "Jeffrey", "Richter", etc);, you are calling the constructor that does set the properties.The base class (
Publication) also has two constructors. When callingbase(), you are calling the constructor ofPublicationthat 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()orbase("some title", "some publisher name", 0.0f /* some price */)), then the parameterless constructor (i.e.public Publication()) will be called by default.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
protectedmeans. It means you can’t call it from outside code – you can only call it from within the same class (from withinPublication) and from inside a derived class (Book).protectedwill not “protect” it from being overridden in derived classes (either usingoverrideornew).