Chapter 7
This chapter introduces concepts such as properties, methods, constructors, collections, and overloading by making use of examples related to real-world objects.
Exercise 1
In our examples, we've modelled some simple characteristics of real world objects, such as animals. Think about other real world objects that when turned into classes would be useful in programming.
Solution
You've probably used one of these objects without thinking about it. What happens when you go to the supermarket? You put items in a shopping basket. Ever bought anything online? How many of those online sites have a shopping basket? Yep, pretty much all of them. In fact, the shopping basket is one thing where the concept maps really well from the real world into the virtual one. Creating a shopping basket in .NET is actually a little more complex than you'd think because it has to contain multiple items. You don't know in advance the number of items people will put into it, therefore you can't define properties for each item. What you need is a collection of some sort which expands as items are added. There are several collections supplied in the System.Collections namespace. It's worth experimenting with them.
Exercise 2
In the Animal class where the Walk() method accepts an argument of type Integer, expand this to use it as the speed of walking. Think about how you'd store that speed and what you could do with it.
Solution
Storing the speed internally in the class can be achieved simply by declaring a private variable, and modifying the Walk() method:
private int _Speed;
public string Walk(int Direction) {
_Speed = Direction;
if (Direction > 0)
Return _Name +
": you are now walking backwards at a speed of " + _Speed;
else
Return _Name +
": you are now walking forwards at a speed of " + _Speed;
}
What you could also consider is providing a property to access or set the speed. Other options include adding methods to speed up and slow down the speed. For example:
public void SpeedUp(int SpeedUpBy) {
_Speed += SpeedUpBy;
}
public void SlowDow(int SlowDownBy) {
_Speed -= SlowDownBy;
}
Exercise 3
Taking the results of Exercise 2, think about how you'd add validation to the speed to ensure that it doesn't exceed some set limits.
Solution
The interaction between methods and properties can be seen very clearly when we need to validate values. Consider the addition of speed to our Person class, which can be set via three methods: Walk() , SpeedUp() , and SlowDown() . If you want to ensure that the speed never exceeds the range -100 to 100 , where would you do this? Should you do this in each method that accesses the _Speed variable? No, because you don't want to have lots of repeated code. Remember how we said encapsulation is one of the key points of classes – we should encapsulate the speed as a property and do the checking there:
public int Speed {
get {
return _Speed;
}
set {
if (Value < -100)
_Speed = -100;
else
_Speed = Value;
if (Value > 100)
_Speed = 100;
else
_Speed = Value;
}
}
All accesses to the _Speed variable should be replaced with the property name:
public string Walk(int Direction) {
Speed = Direction;
If you don't want to expose the speed as a public property you can make it private :
private int Speed {
You still get the advantages of encapsulation but without exposing the property.
Exercise 4
Describe the main differences between inheritance and interfaces. When would you use one over the other?
Solution
The main difference between inheritance and interfaces is that interfaces don't implement any functionality. They only define what the class must implement and not how it is done. Inheritance on the other hand provides a way to supply implementation to classes that inherit from a base class. It's important to understand this difference and understand where you'd use one method over another.Interfaces are good if you need to define the structure of a class for others to implement – perhaps in a team environment where you are writing common routines and require certain features to be present on the objects that will use those routines. Inheritable classes are good when you have to supply functionality to multiple child classes. The ASP.NET Server controls are a perfect example, where much of the implementation is provided in the base WebControl class. An interface wouldn't be any use because each control would then have to implement the same functionality.
Exercise 5
Create a class called PieceOfString with a single read / write property (of type int ) called Length . Create an instance of the class and set Length to 16 . Now you can answer that age old question, "How long is a piece of string?"
Solution
OK, OK, so it's not a real exercise. But I thought it was funny!!!