Book Image

Learning Object-Oriented Programming

By : Gaston C. Hillar
Book Image

Learning Object-Oriented Programming

By: Gaston C. Hillar

Overview of this book

Table of Contents (16 chapters)
Learning Object-Oriented Programming
About the Author
About the Reviewers

Encapsulating data in C#

First, we will add fields to a class in C# and then use access modifiers to hide and protect a specific member of a class from unauthorized access. We will use property getters and setters to control how we write and retrieve values to and from related fields.

We will work with auto-implemented properties to reduce the boilerplate code. We will use methods to add behaviors to classes and create the mutable and immutable version of a 3D vector to understand the difference between an object that mutates state and an object that doesn't.

Adding fields to a class

The SmoothFoxTerrier class is a blueprint for dogs that belong to the Smooth Fox Terrier breed. This class should inherit from a Dog superclass, but we will forget about inheritance and other dog breeds for a while and use the SmoothFoxTerrier class to understand the difference between class fields and instance fields.

We will define the following class attributes to store the values that are shared by all the...