06 Classes and Objects
06 Classes and Objects
Encapsulation
Encapsulation is the process of hiding or encapsulating data from the outside world. A class or structure can specify how
accessible each of its members is to code outside of the class or structure. Methods and variables that are not intended to be
used outside of the class or structure can be hidden to limit the potential for coding errors. Then the members of class or
structure can only be accessed or updated by calling the respective methods through the object.
Encapsulation can be implemented by using the following:
• Defining the modifier of class data members, like instance variables, as private – This will make sure that data members
of the class are not directly accessible from an object instance.
• Accessing the private data members through the class methods or by using properties – The properties use a pair of public
mutator (set) and accessor (get) methods to manipulate private data members of a class. The following shows the general
syntax of a property:
access_modifier datatype propertyName {
get {
//get accessor code
}
set {
//set mutator code using value keyword
}
}
The following class Point shows an example where its instance variables are encapsulated using the private keyword and on
how to create property.
06 Handout 1 *Property of STI
[email protected] Page 1 of 5
IT1907
Example 1:
public class Point {
//instance variables
private int xPos, yPos;
//constructor
public Point() {
this.xPos = 0;
this.yPos = 0;
}
//Using properties (set and get methods) in get, use return, while in set, use the value keyword
public int xPoint {
get {
return this.xPos;
}
set {
this.xPos = value;
}
}
}
In the given example, the return keyword is used in the get accessor to return the value of the property. The value keyword
used in set mutator represents the value being assigned to the property. In the example, the property names xPoint and
yPoint can be used in expressions and assignments. After using a property name, the respective get and set methods are
automatically invoked. The following statements show how to use properties:
Point p1 = new Point();
//this following initialization automatically calls the set method
p1.xPoint = 20;
p1.yPoint = 10;
//accessing the property automatically calls the get method and return its value
int x = p1.xPoint;
Console.WriteLine("value of p1.xPoint is " + x);
Console.WriteLine("value of p1.yPoint is " + p1.yPoint);
Output:
value of p1.xPoint is 20
value of p1.yPoint is 10
In the given example, xPoint and yPoint are properties and have get and set methods. While using the statement
p1.xPoint = 20;, the set method is called, and the value 20 is assigned to the parameter value keyword – from which
it is assigned to the data member of Point class which is xPos. While using the p1.xPos, it calls the get method, returning
the value in the data member of the class which is xPos.
public Person() {
this.firstName = "no name";
this.lastName = "no name";
this.age = 0;
}
}
In the given example, the constructor Person() is declared and initializes the instance variables with values. The following
statement shows how to create an object using the constructor Person():
Person p1 = new Person();
The object p1 is defined using the Person() constructor. Then, the constructor executes automatically and perform its
statements to initializes the instance variables of object p1.
Overloading Constructors
Constructors can be overloaded like all methods. A class can have one (1) or more constructors with same name, as long asthey
have a different set of parameters. Constructors with parameters or overloaded constructors are useful in directly initializing
instance variables and states with defined values to the objects at the time of creation. The following is an example of
overloaded constructors and how to invoke them depending on the required arguments:
public Person() {
this.firstName = "no name";
this.lastName = "no name";
this.age = 0;
}
public Person()
{
this.firstName = "no name";
this.lastName = "no name";
this.age = 0;
06 Handout 1 *Property of STI
[email protected] Page 4 of 5
IT1907
}
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
this.age = 0;
}
The members of a namespace can be accessed using the using keyword and must appear at the top of the program. The
following example shows how to import the namespace CustomNamespace:
using CustomNamespace;
When a namespace is imported, all of its public members can be accessed by the class where it is declared. The following
example shows how to implement the using keyword on a class:
using System;
using CustomNamespace;
namespace ConsoleApp
{
public class Program
{
static void Main(string[] args)
{
//The class Person is a member of the namespace CustomNamespace
Person p1 = new Person();
Person p2 = new Person("Jack", "Paul");
Person p3 = new Person("Elizabeth", "Cruz", 25);
p3.displayInfo();
//The class Console is a member of the namespace System
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
Output:
First name: Elizabeth
Last name: Cruz
Age: 25
Press any key to exit...
REFERENCES:
Deitel, P. and Deitel, H. (2015). Visual C# 2012 how to program (5th Ed.). USA: Pearson Education, Inc.
Gaddis, T. (2016). Starting out with visual C# (4th Ed.). USA: Pearson Education, Inc.
Harwani, B. (2015). Learning object-oriented programming in C# 5.0. USA: Cengage Learning PTR.