Hi there readers,
We’ll begin our journey down the road of oriented object programming with the first principle: encapsulation. When we create a software, we have multiple pieces of data which communicate with each other to do exactly what we wish to achieve. While we would love to live in a world were the users of our software will do exactly what we hope they should do, they won’t. THEY WILL NOT. When we create a software, we must the users not as good users who will do what we want but as evil users. This way, while we’re creating a software, we’ll make sure it is bulletproof.
The first way we get to do that is with encapsulation. While some part of the data must be available to the users, or they won’t be able to use the software, encapsulation creates a wall around the important and sensible data. We protect what is important so that no wrong or unpredictable behavior This wall enables us to choose exactly what we wish to protect and make private and what we wish to make public for the user.
We are not going ahead of ourselves, we’re going to stay with how to make data public and private. What we really want to do is how we can access and/or modify data. Before we get into that, we must first get comfortable with the following: fields and properties. At first, they might seem almost alike for the beginner, I myself took a little while before I got used to them, but they’re not. Fields are simply public that a class exposes. Even though properties do exposes those variables, they give to users the ability to control how the values can be set or how they can be accessed. But in the end, they are both a way to store and retrieve information when we’re manipulating objects in our software.
Like I said earlier, it can be hard to get used to them and to know which to use in a given situation. Usually, we use properties when one has to enforce the way a value can be retrieve any moment during the run time. We use fields for operations like a validation, for example, does this particular file exists at this moment when I need to use it (bool value).
We got to know about all of this, it would be a great time to see this all in action in an example. We’ll create our first class with fields, properties and methods. Those are really what define an object. The properties and fields provide different states to the object and the methods give the object the behavior we expect it to have at run time.
We are going to create the class Person, class starts with an upper-cased letter, which will have a few fields such like a height or a weight. The objects from the class Person will have a few different behaviors, such as saying hello to a fellow comrades.
Before we get into the code sample, I thought it would be a good idea to talk about how we do create objects. We have mentioned them but we barely scratch the surface. We have what the techies in this world call constructors. They have the same name as the class we’re creating, and just like any other methods, they can, or not, receive parameters to initialize given attributes from the class at run time.
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Person aPerson = new Person("Bob", 70.45, 23); aPerson.SayingHello("Sarah"); Console.ReadLine(); } } public class Person { private double _weight; //we define the fields by starting with the underscore (_) public double Weight { get { return _weight; } set { _weight = value; } //We'll let the weight like this because over time, it can change } private String _name; public String Name { get { return _name; } set { _name = value; } //Usually, the name of a person does not change, so we'll make it private. //This means that no one who can access an object of Person will be able to modify the name } private int _age; public int Age { get { return _age; } set { _age = value; } } public Person(String aName,double aWeight, int anAge) { Name = aName; Weight = aWeight; Age = anAge; } public void SayingHello(String personName) { Console.WriteLine("Hi, my name his : {0}. It his a pleasure to make your acquantaince {1} !", Name, personName); } } }
Kevin