How To Work With Inheritance: Murach's C# 2015

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

Chapter 14

How to work
with inheritance

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 1
Objectives
Applied
1. Use any of the features of inheritance that are presented in this
chapter as you develop the classes for your applications.
Knowledge
1. Explain how inheritance is used within the .NET Framework classes
and how you can use it in your own classes.
2. Explain why you might want to override the ToString, Equals, and
GetHashCode methods of the Object class as you develop the code
for a new class.
3. Describe the use of the protected and virtual access modifiers for
the members of a class.
4. Describe the use of polymorphism.
5. Describe the use of the Type class.

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 2
Objectives (cont.)
6. Describe the use of explicit casting when working with the objects
of a base class and its derived classes.
7. Distinguish between an abstract class and a sealed class.

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 3
How inheritance works

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 4
The inheritance hierarchy
for form control classes

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 5
Methods of the System.Object class
ToString()
Equals(object)
Equals(object1, object2)
ReferenceEquals(object1, object2)
GetType()
GetHashCode()
Finalize()
MemberwiseClone()

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 6
Business classes
for a Product Maintenance application

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 7
The code for a simplified version
of the Product base class
public class Product
{
public string Code { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }

public virtual string GetDisplayText(string sep) =>


Code + sep + Description + sep +
Price.ToString("c");
}

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 8
Access modifiers
Keyword Description
public Available to all classes.
protected Available only to the current class or to
derived classes.
internal Available only to classes in the current
assembly.
protected internal Available to the current class, derived
classes in the current assembly, or
derived classes in other assemblies.
private Available only to the containing class.

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 9
The syntax for creating subclasses
To declare a subclass
public class SubclassName : BaseClassName

To create a constructor that calls a base class


constructor
public ClassName(parameterlist) : base(parameterlist)

To call a base class method or property


base.MethodName(parameterlist)
base.PropertyName

To hide a non-virtual method or property


public new type name

To override a virtual method or property


public override type name

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 10
The code for a Book class
public class Book : Product
{
public string Author { get; set; } // A new property

public Book(string code, string description, string author,


decimal price) : base(code, description, price)
{
this.Author = author;
// Initializes the Author field after
} // the base class constructor is called.

public override string GetDisplayText(string sep) =>


this.Code + sep + this.Description +
"( " + this.Author + ")" +
sep + this.Price.ToString("c");
}

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 11
Another way to override a method
public override string GetDisplayText(string sep) =>
base.GetDisplayText(sep) + "( " + this.Author + ")";

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 12
Three versions of the GetDisplayText method
A virtual GetDisplayText method
in the Product base class
public virtual string GetDisplayText(string sep) =>
code + sep + price.ToString("c") + sep + description;

An overridden GetDisplayText method in the Book class


public override string GetDisplayText(string sep) =>
base.GetDisplayText(sep) + sep + author;

An overridden GetDisplayText method


in the Software class
public override string GetDisplayText(string sep) =>
base.GetDisplayText(sep) + sep + version;

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 13
Code that uses the overridden methods
Book b = new Book("CS15", "Murach's C# 2015",
"Anne Boehm", 56.50m);
Software s = new Software("NPTK",
".NET Programmer's Toolkit", "4.5", 179.99m);
Product p;
p = b;
MessageBox.Show(p.GetDisplayText("\n"));
// Calls Book.GetDisplayText
p = s;
MessageBox.Show(p.GetDisplayText("\n"));
// Calls Software.GetDisplayText

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 14
The Product Maintenance form

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 15
Two versions of the New Product form

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 16
The code for the Product class
public class Product
{
private string code;
private string description;
private decimal price;

public Product()
{
}

public Product(string code, string description, decimal price)


{
this.Code = code;
this.Description = description;
this.Price = price;
}

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 17
The code for the Product class (cont.)
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}

public string Description


{
get
{
return description;
}
set
{
description = value;
}
}

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 18
The code for the Product class (cont.)
public decimal Price
{
get
{
return price;
}
set
{
price = value;
}
}

public virtual string GetDisplayText(string sep) =>


Code + sep + Price.ToString("c") + sep + Description;
}

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 19
The code for the Book class
public class Book : Product
{
private string author;

public Book()
{
}

public Book(string code, string description, string author,


decimal price) : base(code, description, price)
{
this.Author = author;
}

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 20
The code for the Book class (cont.)
public string Author
{
get
{
return author;
}
set
{
author = value;
}
}

public override string GetDisplayText(string sep) =>


base.GetDisplayText(sep) + " (" + Author + ")";

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 21
The code for the Software class
public class Software : Product
{
private string version;

public Software()
{
}

public Software(string code, string description, string version,


decimal price) : base(code, description, price)
{
this.Version = version;
}

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 22
The code for the Software class (cont.)
public string Version
{
get
{
return version;
}
set
{
version = value;
}
}

public override string GetDisplayText(string sep) =>


base.GetDisplayText(sep) + ", Version " + Version;

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 23
The code for the Product Maintenance form
public partial class frmProductMain : Form
{
public frmProductMain()
{
InitializeComponent();
}

private ProductList products = new ProductList();

private void frmProductMain_Load(object sender, EventArgs e)


{
products.Fill();
FillProductListBox();
}

private void FillProductListBox()


{
lstProducts.Items.Clear();
foreach (Product p in products)
lstProducts.Items.Add(p.GetDisplayText("\t"));
}

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 24
The Type class
Property Description
Name Returns a string that contains the name of a type.
FullName Returns a string that contains the fully qualified
name of a type, which includes the namespace
name and the type name.
BaseType Returns a Type object that represents the class
that a type inherits.
Namespace Returns a string that contains the name of the
namespace that contains a type.
Method Description
GetType() Returns a Type object that represents the type of
an object.

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 25
Code that uses the Type class
to get information about an object
Product p;
p = new Book("CS15", "Murach's C# 2015",
"Anne Boehm", 56.50m);
Type t = p.GetType();
Console.WriteLine("Name: " + t.Name);
Console.WriteLine("Namespace: " + t.Namespace);
Console.WriteLine("FullName: " + t.FullName);
Console.WriteLine("BaseType: " + t.BaseType.Name);

The result that’s displayed on the console


Name: Book
Namespace: ProductMaintenance
FullName: ProductMaintenance.Book
BaseType: Product

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 26
How to test an object’s type
if (p.GetType() == typeof(Book))

Another way to test an object’s type


if (p.GetType().Name == nameof(Book))

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 27
An abstract Product class
public abstract class Product
{
public string Code { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }

public abstract string GetDisplayText(string sep);


// No method body is coded.
}

An abstract read-only property


public abstract bool IsValid
{
get; // No body is coded for the get accessor.
}

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 28
A class that inherits the abstract Product class
public class Book : Product
{
public string Author { get; set; }

public override string GetDisplayText(string sep) =>


this.Code + sep + this.Description +
"( " + this.Author + ")" + sep +
this.Price.ToString("c");
}

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 29
The class declaration for a sealed Book class
public sealed class Book : Product

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 30
How sealed methods work
A base class named A that declares a virtual method
public class A
{
public virtual void ShowMessage() =>
MessageBox.Show("Hello from class A");
}

A class named B that inherits class A


and overrides and seals its method
public class B : A
{
public sealed override void ShowMessage() =>
MessageBox.Show("Hello from class B");
}

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 31
How sealed methods work (cont.)
A class named C that inherits class B
and tries to override its sealed method
public class C : B
{
public override void ShowMessage() => // Not allowed
MessageBox.Show("Hello from class C");
}

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 32

You might also like