E-Books: MSDN Training
Essentials of Object-Oriented Programming
Microsoft
Essentials of Object-Oriented Programming
Microsoft
Overview
C# is an object-oriented programming language. In this section, you will learn the terminology and concepts required to create and use classes in C#.
After completing this module, you will be able to:
- Define the terms object and class in the context of object-oriented programming.
- Define the three core aspects of an object: identity, state, and behavior.
- Describe abstraction and how it helps you to create reusable classes that are easy to maintain.
- Use encapsulation to combine methods and data in a single class.
- Explain the concepts of inheritance and polymorphism.
- Create and use classes in C#.
The whole structure of C# is based on the object-oriented programming model.
To make the most effective use of C# as a language, you need to understand the nature of object-oriented programming.
In this section, you will learn about the basics of object-oriented programming.
You will examine classes and objects in the context of object-oriented programming. You will then learn the how to apply the concept of abstraction.
What Is a Class?
The root word of classification is class. Forming classes is an act of classification, and it is something that all human beings (not just programmers) do. For example, all cars share common behavior (they can be steered, stopped, and so on) and common attributes (they have four wheels, an engine, and so on).
You use the word car to refer to all of these common behaviors and properties.
Imagine what it would be like if you were not able to classify common behaviors and properties into named concepts! Instead of saying car, you would have to say all the things that car means. Sentences would be long and cumbersome. In fact, communication would probably not be possible at all. As long as everyone agrees what a word means, that is, as long as we all speak the
same language, communication works well—we can express complex but precise ideas in a compact form. We then use these named concepts to form higher-level concepts and to increase the expressive power of communication.
All programming languages can describe common data and common functions.
This ability to describe common features helps to avoid duplication. A key motto in programming is “Don’t repeat yourself.” Duplicate code is troublesome because it is more difficult to maintain. Code that does not repeat itself is easier to maintain, partly because there is just less of it! Object-oriented languages take this concept to the next level by allowing descriptions of classes
(sets of objects) that share structure and behavior. If done properly, this paradigm works extremely well and fits naturally into the way people think and communicate.
Classes are not restricted to classifying concrete objects (such as cars); they can also be used to classify abstract concepts (such as time). However, when you are classifying abstract concepts, the boundaries are less clear, and good design becomes more important.
The only real requirement for a class is that it helps people communicate.
What Is an Object?
The word car means different things in different contexts. Sometimes we use the word car to refer to the general concept of a car: we speak of car as a class, meaning the set of all cars, and do not have a specific car in mind. At other times we use the word car to mean a specific car. Programmers use the term object or instance to refer to a specific car. It is important to understand this difference.
The three characteristics of identity, behavior, and state form a useful way to think about and understand objects.
Identity
Identity is the characteristic that distinguishes one object from all other objects of the same class. For example, imagine that two neighbors own a car of exactlythe same make, model, and color. Despite the obvious similarities, the registration numbers are guaranteed to be unique and are an outward reflection that cars exhibit identity. The law determines that it is necessary to distinguish one car object from another. (How would car insurance work without car identity?)
For Download: