Section A.6. Chapter 6: Object-Oriented Programming


A.6. Chapter 6: Object-Oriented Programming

A.6.1. Quiz



Solution to Question 61 .

New ( user -defined) types are most often created in C# with the keyword class .



Solution to Question 62 .

A class defines a new type; an object is an instance of that type.



Solution to Question 63 .

Making your member fields private allows you to change how you store that data (as a member variable, in a database) without breaking your client's code.



Solution to Question 64 .

Encapsulation is the principle of keeping each class discreet and self-contained, so you can change the implementation of one class without affecting any other class.



Solution to Question 65 .

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 66 .

Polymorphism is the ability to treat derived classes as if 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 67 .

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 68 .

Access modifiers indicate which class' 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 69 .

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 610 .

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.

A.6.2. Exercises



Solution to Exercise 6-1 .

Draw a class diagram for a class named "vehicle" and show how the classes "car," "truck," and "motorcycle" derive from it. (See Figure A-4.)

Figure A-4. Exercise 6-1


Solution to Exercise 6-2 .

Define a class Book , in which a book has a title, author, and ISBN, and the book can be read or shelved:

 class Book {  private String title;  private String author;  private String ISBN;  public Book (string title, string author, string ISBN)  {     this.title = title;     this.author = author;     this.ISBN = ISBN;  }  public void Read(  )    // member method  {    // code here to read book  }  public void Shelve(  )  // member method  {    // code here to shelve book }; 



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net