Chapter 6: Object-Oriented Programming

Quiz Solutions

Solution to Question 6-1. New (user-defined) types are most often created in C# with the keyword class.

Solution to Question 6-2. A class defines a new type; an object is an instance of that type.

Solution to Question 6-3. Making your member fields private allows you to change how you store that data (as a member variable, or in a database) without breaking your client’s code.

Solution to Question 6-4. Encapsulation is the principle of keeping each class discrete and self-contained, so you can change the implementation of one class without affecting any other class.

Solution to Question 6-5. Specialization allows a new class to “inherit” many of the characteristics of an existing class, and to be used polymorphically with that class. Specialization is implemented in C# through inheritance.

Solution to Question 6-6. Polymorphism is the ability to treat derived classes as though they were all instances of their base class, yet have each derived class specialize its own implementation of the base class’s methods.

Solution to Question 6-7. The is-a relationship is established through inheritance. The has-a relationship is implemented through aggregation (making one type a member variable of another type).

Solution to Question 6-8. Access modifiers indicate which class’s methods have access to a given field, property, or method of a class. Public members are available to methods of any class; private members are available only to methods of instances of the same class.

Solution to Question 6-9. State is the current conditions and values of an object, and is implemented with properties and member variables. Capabilities are what the object can do, exposed through public methods. Responsibilities are the promises a well-designed class makes to the clients of that class.

Solution to Question 6-10. A use-case scenario is a tool for the analysis of a problem. In a use-case scenario, you walk through the details of how your product will be used by one user to accomplish one task, noting which classes interact and what their responsibilities are.

Exercise Solutions

Solution to Exercise 6-1. A visual representation of a class, its member fields and methods, and its place in the hierarchy is called a class diagram. There are several accepted methods for drawing a class diagram, but we won’t hold you to any of those right now. For this example, simply draw a class diagram for a class named vehicle, listing some properties and methods that you think that class should have. Then add to your diagram the derived classes car, boat, and plane, and list their properties and methods (remember that all derived classes inherit the properties and methods of their parent class).

The rule of thumb when you’re designing a parent class is to keep it as simple as it needs to be—include those properties and methods you need, but nothing extraneous. Leave the specialization to the derived classes; that’s what they’re there for. There are lots of ways to define these classes, but Figure A-4 shows one example.

One solution to Exercise 6-1.

Figure A-4. One solution to Exercise 6-1.

We’re using a common drawing convention that shows each class as a box with three sections. The top section shows the class name, the middle section shows its member fields, and the bottom section shows its member methods. The arrow leading from the lower three boxes to the upper one indicates that the lower three classes are all inherited from the upper one.

We’ve decided that all vehicles have a number of passengers, and a type of fuel (even if it’s human-powered pedaling). Those are the member fields, and you’ll notice we’ve assigned a type to each of them. We’ve also decided that all vehicles can move and stop; those are the methods.

We’ve gone on to derive three child classes, Car, Plane, and Boat. Each class inherits the properties and methods of the parent, so Car has a number of passengers, for example. However, cars have to specify the number of doors, and whether they’re stick-shift or automatic. Cars can also honk, and haul a trailer, which neither of the other two classes can do. Both Plane and Boat have their own specialized properties and methods, which are probably different from the ones you thought of.

Solution to Exercise 6-2. You’ve defined a class as a diagram; now try defining one in code. Define a class Book, in which a book has a title, author, and ISBN, and the book can be read or shelved. You don’t need to fill in the code for any methods you include; simply include a comment in the body, like we did for the Dog class earlier in the chapter.

This definition is relatively simple. We’ve given you the requirements for the class; you just need to decide which ones are methods and which are properties. Example A-13 shows what we had in mind.

Example A-13. Our solution to Exercise 6-2

class Book
{
   private String title;
   private String author;
   private String ISBN;

   public void Read(  )    // member method
   {
      // code here to read book
   }
   public void Shelve(  )  // member method
   {
      // code here to shelve book
   }
}

This isn’t real code, and it won’t compile without Main( ). In Chapter 7, you’ll see how to create a class definition.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset