|
CIS162AD -- C Sharp (C#) -- Section 5832
Professor: Dave Hammer dwhammer@cox.net http://members.cox.net/dwhammer/ Student: Patrick Moss patmoss@patmoss.com http://www.patmoss.com/cis162ad/ Worksheet #7 Due Thursday, April 27, 2006
I. Definitions -- Please explain the following terms:
1. abstraction (p. 795)
The act of generalizing or thinking about an object in general
terms. Through abstraction, data members and behaviors of a
class are identified.
2. inheritance (p. 800)
The concept of defining subclasses of data objects that share
some or all of the parent's class characteristics.
3. polymorphism (p. 803)
The ability of classes to provide different implementations
of methods based on what type of argument is used for the call.
Polymorphism is one of the four major concepts that form the
basis of an object-oriented programming language.
The four major concepts of OOP are: (p. 556)
a. Abstraction
b. Encapsulation
c. Inheritance
d. Polymorphism
An easy-to-remember acronym that we sometimes use is "A PIE".
4. base class (p. 796)
A class from which other classes can inherit characteristics.
The base class is sometimes called the super class or the
parent class. The ultimate base class of all classes in C#
is Object.
5. DLL = Dynamic Link Library (p. 573-574)
C# and Visual Studio .NET offer several options for creating
components. One option is to compile the source code files
into a DLL file instead of into the EXE file type that we
are accustomed to creating. After we have a DLL, for any
application that will use that component, simply add a
reference to the DLL and that referenced file with the .dll
extension becomes part of the application's private assembly.
6. override a method (p. 803, 567-573)
The keyword "override" can be added to the method heading
to allow a method to provide a new implementation (method
body) of a method inherited from a base class.
7. virtual method (p. 564, 571)
The "virtual" modifier implies that any class that derives
from another class can override that method. To override a
method, the new method must have exactly the same parameters
that are used in the method to be overridden.
8. abstract class (p. 585)
C# allows us to add an "abstract" modifier to classes that
prohibits other classes from instantiating objects of a base
class. We can still inherit characteristics from this base
class in subclasses, which enables us to ensure a certain
amount of identical functionality from subclasses. The base
class can have data and method members.
9. Interface (p. 801)
The interface is the front end of a program. It is the visual
image which you see when you run a program, and it allows the
user to interact with a running program.
II. Questions or short essay:
1. How is an "is-a" relationship different from a "has-a"
relationship? (p. 558, 564)
Inheritance is associated with an "is a" relationship. A
specialized class "is a" form of the general class.
Classes can also have a "has a" relationship in which a
single class is defined to have instances of other class
types. This is a concept called "containment" or "aggregation".
For example, we are using containment when we define classes
that instantiate objects of the string or int classes.
"Has a" relationships are usually associated with user-defined
classes. For example, a Person "has a" medicalRecord and also
"has a" dentalRecord.
2. What is the significance of the Object class"? (p. 558-559)
Every object created in C# automatically inherits from a "base
class" named "object" that is in the System namespace. Object
has four methods that every class written in C# inherits, as
long as it provides a reference to the System namespace:
a. Equals
b. GetHashCode
c. GetType
d. ToString
3. Why must constructors have "public" access? (p. 561)
When a class is instantiated, the instantiating program code
must call a constructor in the class. In order for the
constructor name to be "visible" to the program code, it must
have "public" access.
4. What is the purpose of the "private" access modifier?
(p. 132, 561)
The private members are not accessible to other classes that
derive from the class or that instantiate objects of this
class. Using a private access modifier enables the class to
protect its data and only allow access to the data through
its methods or properties. This ensures the data-hiding
characteristic of encapsulation.
5. What is the purpose of the "protected" access modifier?
(p. 564-565)
Although derived classes inherit all of the characteristics
of the base class, they do not have access to change their
private members. In addition to "private" and "public" access,
C# offers "internal" and "protected" access. Internal members
are accessible only within files in the same assembly. And
protected members are accessible to any class that is derived
from them, but not to any other classes. So, if we want
methods in derived classes to have access to change data in
the base class, define the data members by using a "protected"
access, instead of a "private" access. This way, the data is
still hidden from other classes, but is available for use in
derived classes.
6. What syntax is used to call a parent constructor from the
constructor of the derived class? (p. 565-566)
To call the constructor for the base class, an extra entry
is added between the constructor heading for the subclass
and the opening curly brace. For example:
public Student( )
:base( ) // no arguments sent to base constructor
{
...
}
7. What is the purpose of properties? (p. 561)
Properties are new to the C/C++/C# family line. They look like
data fields, but are implemented as methods that can be used
to get or set a "private" data field instance. (In java, the
terms "accessor" and "mutator" are used for get and set.)
III. Programming Exercise:
Write source code for the following.
Create a base banking account. Decide what characteristics are
common for savings and checking accounts, and include these
characteristics in the base class. Define subclasses for checking
and savings. Include a presentation class to test your design.
Turn in your source code for this program.
|