Fundamentals of Mixing Classes

Introduction

In previous lessons, when we needed a class, we created it, from scratch. If we needed a different class, we created a new one, even if the new class had only slight differences with an existing class.

Practical LearningPractical Learning: Introducing Object Mixing

  1. Start Microsoft Visual Studio. Create a new ASP.NET Core Web App named Volumetrics1 and uncheck Configure For HTTPS
  2. To create a new class, in the Solution Explorer, right-click Pages -> Add -> Class...
  3. In the middle list, make sure Class is selected.
    Change the name to Rectangle
  4. Click Add
  5. Change the class as follows:
    namespace Volumetrics1.Pages
    {
        public class Rectangle
        {
            public Rectangle()
            {
                width  = 0.00;
                height = 0.00;
            }
    
            public Rectangle(double _wide_, double _high_)
            {
                width = _wide_;
                height = _high_;
            }
    
            public double width;
            public double height;
    
            public double CalculateArea()
            {
                return width * height;
            }
    
            public double CalculatePerimeter()
            {
                return (width + height) * 2;
            }
        }
    }
  6. To create another class, in the Solution Explorer, right-click Pages -> Add -> Class...
  7. In the middle list, make sure Class is selected.
    Change the name to Circle
  8. Click Add
  9. Change the class as follows:
    namespace Volumetrics1.Pages
    {
        public class Circle
        {
            public Circle()
            {
                radius = 0.00;
            }
    
            public Circle(double _radius_)
            {
                radius = _radius_;
            }
    
            public double radius;
    
            public double CalculateArea()
            {
                return radius * radius * 3.141592653589793238462643;
            }
    
            public double CalculateDiameter()
            {
                return radius * 2;
            }
    
            public double CalculateCircumferemce()
            {
                return CalculateDiameter() * 3.141592653589793238462643;
            }
        }
    }
  10. To create one more class, in the Solution Explorer, right-click Pages -> Add -> Class...
  11. In the middle list, click Code File (it should be selected already).
    Change the name to Cylinder
  12. Click Add

An Object as Field

If you already have a useful class but need just some extra functionality, instead of creating a different class from scratch, you can create a new class but you would add only the needed extra functionality. One way you can address this issue is that, in the new class, create a field that uses the existing class as type.

In a class, you create a member whose data type is an existing class. Here is an example of a property whose type is a class:

@functions{
    class Processor
    {
        public string make;
        public string model;
        public int wattage;
        public double speed; 
    }

    class Computer
    {
        public string category;
        public string wirelessType;
        public Processor calculator;
    }
}

After creating the field whose type is a class, you can access the members of that type in a method of the class. To do this, type the name of the field, a period, and the desired member. Here is an example:

@functions{
    class Processor
    {
        public double speed; 
    }

    class Computer
    {
        public string category;
        public string wirelessType;
        public Processor calculator;

        private void Describe()
        {
            Calculator.speed = 5.33; // Ghz
	}
    }
}

To access the members of the type outside the class, after declaring a variable of the class, type the name of the variable, a period, the name of the field, a period, and the desired member. Here is an example:

@page
@model Valuable.Pages.ExerciseModel
@{
    Computer device = new Computer();
    
    device.Describe();
    device.category = "Desktop";

    device.calculator.make  = "AMD";
    device.calculator.model = "Ryzen";
}

@functions
{
    class Processor
    {
        public string make;
        public string model;
        public int    wattage;
        public double speed;
    }

    class Computer
    {
        public string    category;
        public string    wirelessType;
        public Processor calculator;

        public void Describe()
        {
            calculator = new Processor();

            calculator.speed = 5.33; // Ghz
        }
    }
}

<pre>Computer Build-Up
--------------------------------
Category: @device.category
Make:     @device.calculator.make
Model:    @device.calculator.model</pre>

This would produce:

Computer Build-Up
--------------------------------
Category: Desktop
Make:     AMD
Model:    Ryzen

Practical LearningPractical Learning: Introducing Inheritance

  1. Change the Cylinder class as follows:
    namespace Volumetrics1.Pages
    {
        public class Cylinder
        {
            public Cylinder(double radius, double height)
            {
                basis = new Circle(radius);
                lateral = new Rectangle(basis.CalculateCircumferemce(), height);
            }
    
            public Circle basis;
            public Rectangle lateral;
    
            public double CalculateBaseArea()
            {
                return basis.CalculateArea();
            }
    
            public double CalculateLateralArea()
            {
                return lateral.CalculateArea();
            }
    
            public double CalculateVolume()
            {
                return CalculateBaseArea() * lateral.height;
            }
        }
    }
  2. In the Solution Explorer, right-click Pages -> Add -> Razor Page
  3. In the middle list of the Add New Scaffolded Item dialog box, make sure Razor Page - Empty is selected. Click Add
  4. Change the file Name to Geometry
  5. Press Enter
  6. Change the document as follows:
    @page
    @model Volumetrics1.Pages.GeometryModel
    @{
        double radius = 269.77;
        double height = 127.93;
        
        Cylinder cyl = new Cylinder(radius, height);
    }
    
    <pre>========================================
    Geometry - Cylinder Summary
    ========================================
    Base
    ----------------------------------------
    Diameter:      @cyl.basis.CalculateDiameter()
    Radius:        @cyl.basis.radius
    Circumference: @cyl.basis.CalculateCircumferemce()
    Areat:         @cyl.basis.CalculateArea()
    ========================================
    Lateral Side
    ----------------------------------------
    Height:        @cyl.lateral.height
    Lateral Area:  @cyl.lateral.CalculateArea()
    Volume:        @cyl.CalculateVolume()
    ========================================</pre>
  7. To execute the project and test the application, on the main menu, click Debug -> Start Without Debugging
  8. In the broswer, click the right side of the address, type /Geometry and press Enter:
    ========================================
    Geometry - Cylinder Summary
    ========================================
    Base
    ----------------------------------------
    Diameter:      539.54
    Radius:        269.77
    Circumference: 1695.014900317837
    Areat:         228632.0848293714
    ========================================
    Lateral Side
    ----------------------------------------
    Height:        127.93
    Lateral Area:  216843.2561976609
    Volume:        29248902.612221483
    ========================================
  9. Return to your programming environment

Fundamentals of Inheritance

Class inheritance is the ability to create a new class that gets its primary functionality from an existing class. Before using inheritance, you must have a class. You can create it like any of the classes we have created so far.

Creating a class that is based on another class is also referred to as deriving a class from another. The first class serves as parent or base. The class that is based on another class is also referred to as child or derived.

To create a new class based on another existing class, use the following formula:

options class child-class : parent-class
{
    Body of the new class
}

Among the available options, you can start with an access level (such as public). This is followed by the class keyword and a name for the new class. To indicate that the new class is based on another class, type a colon followed by the name of the base class. For a parent class, you can first create a class. This means that you must use a class that exists aleady. Here is an example of starting a class inheritance:

class Circle
{
}

class Cylinder : Circle
{

}

If you want to be able to access the class from other languages, you can precede its name with the public keyword. Here is an example:

class Circle
{
}

public class Cylinder : Circle
{

}

After deriving a class, it becomes available and you can use it.

Practical LearningPractical Learning: Deriving From a Class

Inheritance and the Access to Class Members

Private Members

When creating a class and when dealing with inheritance, you can control what members can be accessed from outside the class and/or from only inherited members.

As introduced and used in previous lessons, a member of a class is referred to as private if it can be accessed only from within the class. To create a private member, type the private keyword to its left. A member without an access level or marked with the private keyword can be accessed only within the class, that is, only by members of the same class.

Internal and Public Members

A member of a class is internal if it can be accessed by any class of the same project. To create such a member variable, precede it with the internal keyword.

A member variable is referred to as public if it can be accessed by the classes of the same file. To create a public member variable of a class, precede it with the public keyword.

Primary Characteristics of Inheritance

Initializing an Object of a Derived Class

If you declare a variable of a class that is derived, the variable has access to the public and internal members of its direct class. The variable has direct access also to the public and internal members of the parent(s) of its class. You can initialize the variable by accessing each member individually. As an alternative, you use the curly brackets to initialize the object.

Practical LearningPractical Learning: Initializing an Object of a Derived Class

  1. Change the Geometry.cs document as follows:
    @page
    @model Volumetrics1.Pages.GeometryModel
    @{
        Cylinder Initialize()
        {
            double radius = 1429.67;
            double height = 688.38;
            
            Cylinder cylinder = new Cylinder(radius, height);
            
            return cylinder;
        }
        
        Cylinder cyl = Initialize();
    }
    
    <hr />
    <h1>Geometry - Cylinder Summary</h1>
    <hr />
    <h3>Base</h3>
    <hr />
    <p>The diameter is not a member of the cylinder. The diameter is inherited from the Circle class.</p>
    <p><b>Diameter:      @cyl.basis.CalculateDiameter()</b></p>
    <p><b>Radius:        @cyl.basis.radius</b></p>
    <p>The circumferemce is not a member of the cylinder. The circumferemce is inherited from the Circle class.</p>
    <p><b>Circumference: @cyl.basis.CalculateCircumferemce()</b></p>
    <p>The area is not a member of the cylinder. The area of the base is inherited from the Circle class.</p>
    <p><b>Area:          @cyl.basis.CalculateArea()</b></p>
    <hr />
    <h3>Lateral Side</h3>
    <hr />
    <p><b>Height:        @cyl.lateral.height</b></p>
    <p><b>Lateral Area:  @cyl.lateral.CalculateArea()</b></p>
    <p><b>Volume:        @cyl.CalculateVolume()</b></p>
    <hr />
  2. To make sure there is no error, press Ctrl + F5
  3. For the Radius, type 1429.67 and press Enter
  4. For the Height, type 688.38 and press Enter:
    <hr />
    Geometry - Cylinder Summary
    <hr />
    Base
    <hr />
    The diameter is not a member of the cylinder. The diameter is inherited from the Circle class.
    
    Diameter: 2859.34
    
    Radius: 1429.67
    
    The circumferemce is not a member of the cylinder. The circumferemce is inherited from the Circle class.
    
    Circumference: 8982.88153811544
    
    The area is not a member of the cylinder. The area of the base is inherited from the Circle class.
    
    Area: 6421278.12429875
    <hr />
    Lateral Side
    <hr />
    Height: 688.38
    
    Lateral Area: 6183635.993207906
    
    Volume: 4420279435.204774
    <hr />
  5. Return to Microsoft Visual Studio

Creating a Grand-Child

You can create a class that adds some functionality to a class that itself is derived from another. One approach is to proceed as we saw in our introduction to mixing classes: In the new class, create a member that is of the child class type. Here is an example:

@functions
{
    class Person
    {
        public string firstName;
        public string fastName;
    }

    class Student : Person
    {
        public int studentNumber;
        public string grade;
    }

    class ReportCard
    {
        public string schoolYear;
        public Student Pupil;
    }
}

Otherwise, you can create a class that is derived from a class that itself is based on another class. Here is an example:

@functions
{
    class Person
    {
        public string firstName;
        public string lastName;
        public string gender;
        public string dateOfBirth;
    }

    // Child class
    class Adult : Person
    {
        public string highestEducationLevel;
        public string maritalStatus;
    }

    // Grand-child class
    class Employee : Adult
    {
        public int employeeNumber;
        public double hourlySalary;
        public string dateHired;
    }
}

In the same way, you can create a class that is based on class that is itself is based on a class that is derived from another class that in turn descends from another class, and so on.

Intermediate Characteristics of Inheritance

A Public Class

Sometimes, you will work on a solution that includes many projects. Sometimes too, you will create classes that you want to use in different projects. To make sure that a class is made for that type of exchange, create it as a public class. To do that, mark the class with the public keyword, which is written on the left side of the class keyword. Here is an example:

@functions
{
    public class Book
    {
    
    }
}

If you are creating a class that will be used in one project, the public keyword in not required. Still, in that case, you can apply the public keyword if you want (it doesn't have any negative impact).

Remember that you can create many classes in the same document. In that case, you can mark some classes with the public keyword and omit it for some other classes. Here is an example:

@functions
{
    class School
    {

    }

    public class Country
    {

    }

    class PoliticalParty
    {

    }
}

You can mark a partial class with an access level. In this case, you can write the public keyword to the left of the partial keyword. Here is an example:

@functions
{
    public partial class Person
    {
    
    }
}

Inheritance with a Public Class

If you create a class that is based on another class, if you decide to make the child class public, the compiler will check the access level of the parent class. If the parent is not made public, you would receive an error. The suggestion is to either make the parent class public or not apply the access level to the classes. Here is an example:

@functions
{
    public class Schaool
    {

    }

    public class Country
    {

    }
}

Partial Classes and Inheritance

If you create a class as a partial one, you can create another class based on it. Here is an example:

partial class Road
{
    
}

class Highway : Road
{

}

The child class doesn't have to be partial, although it can be. You can create a partial class that is derived from a non-partial class. Here is an example:

class EletronicMemory
{
    public int capacity;
}

partial class HardDrive : EletronicMemory
{
    public string make;
    public string model;
}

You can also create a partial class that is derived from a partial class. Here is an example:

partial class Road
{

}

partial class Interstate : Road
{

}

Remember that you can mark a partial class with an access level. This is also valid in inheritance. Here are examples:

public partial class Road
{

}

public partial class Interstate : Road
{

}

Namespaces and Inheritance

Imagine you had created a class named Element in a namespace named Chemistry as follows:

namespace Chemistry
{
    class Element
    {
    }
}

To derive a class from a class that belongs to a namespace, type the name of the namespace, followed by the period operator ".", and followed by the name of the base namespace. Here is an example:

namespace Chemistry
{
    class Element
    {
    }
}

class Isotope : Chemistry.Element
{
    public int neutronNumber;
}

If you need to call the class that was defined in a different namespace, you can qualify its name with the period operator. Here is an example:

namespace Chemistry
{
    class Element
    {
    }
}

class Isotope : Chemistry.Element
{
    public int neutronNumber;
}

class Exercise
{
    public void Create()
    {
	    Chemistry.Element h = new Chemistry.Element();
    }
}

Alternatively, to use the contents of a namespace, prior to calling a member of that namespace, you can type the using keyword followed by the name of the namespace. Here is an example:

using Chemistry;

class Isotope : Element
{
    public int neutronNumber;
}

class Exercise
{
    public void Create()
    {
	    Element h = new Element();
    }
}

Consider the following class named Matter and that is created in a nested namespace:

namespace Chemistry
{
    class Element
    {
    }

    namespace Atoms
    {
        namespace Nucleons
        {
            class Proton
            {
                public string symbol;
            }

            class Neutron
            {
            }
        }

        public class Matter
        {
            public double mass;
        }
    }
}

If you want to create a class that is based on a class in a nested namespace, you can qualify its name. Here is an example:

class Atom : Chemistry.Atoms.Matter
{
    public Chemistry.Element element;
    public Nucleus nucleus;
}

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2001-2022, C# Key Monday 27 December 2021 Next