Learning C# and OOP: Getting Started, Objects and Encapsulation
Summary
This is the first in a miniseries of lessons that describe and discuss the necessary and most significant aspects of OOP using C#. This lesson was broken into two major and generally unrelated sections. The first section dealt with the mechanics of getting started programming in C#. The second section dealt with the concepts involved in getting started in OOP.The mechanics
It is not necessary to spend a lot of money for a high-level development environment to begin writing object-oriented programs using C#. I provided you with the information needed to download everything you need, for free. In addition, I discussed the installation and testing of those free development tools.
The concepts
In order to understand OOP, you need to understand the following three concepts:
- Encapsulation
- Inheritance
- Polymorphism
A description of an object-oriented program was provided, along with a description of an object, and how it relates to encapsulation.
A real-world analogy
In order to relate object-oriented programming to the real world, a car radio was used to illustrate and discuss several aspects of software objects. You learned that car radios, as well as software objects, have the ability to store data, along with the ability to modify or manipulate that data.
You learned that car radios, as well as software objects, have the ability to accept messages, which results in performing an action, modifying state, returning a value, or some combination of the above.
Jargon, jargon, jargon
You learned some of the jargon used in OOP, including persistence, state, messages, methods, and behaviors.
Where do objects come from?
You learned where objects come from, and you learned that a class is a set of plans that can be used to construct objects. (You also learned that a struct is a set of plans that can be used to construct objects, but I didn't discuss that in detail. This is one of the differences between C# and Java.) You learned that a C# object is an instance of a class or an instance of a struct.
A little bit of code
You saw a little bit of C# code, used to create an object, and then to send a message to that object (invoke a method on the object).
You learned about C# references and reference variables. You learned a little about memory allocation for objects and variables in C#.
What's Next?
Continuing with the real-world example introduced in this lesson, the next lesson will provide a complete C# program that simulates the manufacture and use of a car radio.
Review Questions
2. True or false? You can run the .NET Framework SDK on Windows 98.
3. True or false? You must download the documentation for the .NET Framework SDK as a separate download package.
4. True or false? You can compile and execute C# programs either from within an IDE, or directly from the Windows command line.
5. List the three pillars of OOP.
6. Describe an object-oriented program.
7. Describe a software object.
9. Explain why a car radio is an imperfect, but good analogy to a software object.
10. What is the physical manifestation of sending a message to an object in C#?
Richard Baldwin is a college professor (at Austin Community College in Austin, Texas) and private consultant whose primary focus is a combination of Java, C#, and XML. In addition to the many platform and/or language independent benefits of Java and C# applications, he believes that a combination of Java, C#, and XML will become the primary driving force in the delivery of structured information on the Web.
Richard has participated in numerous consulting projects, and he frequently provides onsite training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Programming Tutorials, which has gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
Answers
Answer 10
As a practical matter, the physical manifestation of sending a message to an object in C# is to cause that object to execute one of its methods.Answer 9
A car radio has the ability to store data, and to allow you to use and modify that data at will. However, you can only use and modify that data through use of the operator interface that is provided by the manufacturer of the radio. These are also the characteristics of a software object.Answer 8
According to Rocky Lhotka in an article regarding VB.NET:"Encapsulation is the concept that an object should totally separate its interface from its implementation. All the data and implementation code for an object should be entirely hidden behind its interface."Back to Question 8
Answer 7
A software object is a software construct that encapsulates data (into a software entity), along with the ability to use or modify that data.Answer 6
Many authors would describe an object-oriented program something like the following:An Object-Oriented Program consists of a group of cooperating objects, exchanging messages, for the purpose of achieving a common objective.Back to Question 6
Answer 5
Most books on OOP will tell you that in order to understand OOP, you need to understand the following three concepts:- Encapsulation
- Inheritance
- Polymorphism
Answer 4
TrueAnswer 3
False. According to Microsoft,"The Microsoft® .NET Framework Software Development Kit (SDK) includes the .NET Framework, as well as everything you need to write, build, test, and deploy .NET Framework applicationsdocumentation, samples, and command-line tools and compilers."Back to Question 3
Answer 2
False. According to Microsoft, the .NET Framework SDK runs on:- Microsoft Windows NT® 4.0 (SP 6a required)
- Microsoft Windows® 2000 (SP 2 recommended)
- Microsoft Windows XP Professional
Answer 1
False. All you need to get started programming using C# is the free Microsoft® .NET Framework Software Development Kit (SDK) and a simple text editor.
Copyright 2002, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.
-end-
