4.1.2.3 - Classes, objects and encapsulation (C#)
Learn how classes define state and behaviour, constructors initialise separate objects, and controlled access protects an object's valid state.
This C# route is incomplete: runnable support pending. Native written source and marking are available; Run is not.
Classes, fields and methods
A class defines the common attributes and behaviours of a kind of object. An object is an instance created from that class. A field stores state; a method implements behaviour. Objects from the same class share the definition of their behaviours while each ordinarily has its own instance-field values.
class Counter
{
private int value;
public Counter(int initial)
{
value = initial;
}
public void Increment()
{
value++;
}
public int GetValue()
{
return value;
}
}
Counter describes the state value and two behaviours. Increment acts on the receiving object's field, without needing a separate value argument. This grouping is useful when a program handles many stateful entities with the same rules: each object owns its own data and exposes a clear set of operations. It reduces accidental mixing of one entity's state with another's and provides reusable abstractions.
Encapsulation
Grouping state with the operations that use it, and hiding internal details behind a controlled public interface.
Encapsulation is more than putting unrelated functions in a class. Give the class a coherent responsibility and make its methods enforce that responsibility. Relationships such as inheritance, composition and aggregation build larger object-oriented designs; the following relationship lesson develops them.
Instantiation and constructors
Instantiation creates an object from a class. A constructor is called during creation to establish its initial state. Its name matches the class and it has no return type, even void.
Counter first = new Counter(2);
Counter second = new Counter(8);
Counter alias = first;
alias.Increment();
Console.WriteLine(first.GetValue()); // 3
Console.WriteLine(second.GetValue()); // 8
The two new expressions create two distinct objects. The variables hold references to them, rather than each holding the complete object directly. alias = first copies the reference; it does not instantiate a third counter. Calling alias.Increment() therefore changes the object that first also refers to. second remains separate.
A class with no declared constructor can receive an implicit parameterless constructor. Once the programmer declares a constructor such as Counter(int initial), C# does not automatically add a parameterless one. new Counter() would need a matching declared constructor in this class. Constructors can enforce conditions or choose valid defaults; state should be usable immediately after construction.
Encapsulation and access
Access specifiers control which code may use a member. public permits access by calling code, private restricts access to its declaring class, and protected permits the declaring class and derived classes. Protected state still is not publicly accessible to an unrelated caller.
| UML prefix | C# access | Example class member |
|---|---|---|
+ | public | + GetValue() : int |
- | private | - value : int |
# | protected | # ResetValue() : void |
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account