Object Oriented Programing is common practice in modern software development. So any beginner developer must know what it is and should be able to explain certain terms which is used in .NET a object oriented programming platform .
- Classes
- Properties
- Methods
- Fields
- Members
- Enums
- Casting
- Structures
- Abstraction
- Encapsulation
- Interfaces
- Static classes
- Constructors
- Method overloading
- Inheritance
- Overriding methods
- Virtual methods
- Abstract classes
- Polymorphism
- Delegates
- Events
- Assemblies
- Namespaces
A class is an abstract concept. It is a blueprint. Try to think of a class as e.g the blueprints of a car in the real world.
The designers of auto mobiles sit in front of their computer (or use paper and pencil) and describe exactly the parts of the auto mobile. They describe how these parts interact, the colour of the car, the height of the car, the size of the engine, the acceleration of the car, if the car has air-conditioning system installed.
Then the mechanics that observe the production line, make sure that the cars built (the actual cars) follow the blueprints outlined in the design stage of building a car.
So a class is a way of describing real world entities. It is the code definition for objects.
The class is the fundamental building block of code when creating object-oriented software. A class describes in abstract (in theory) all of the characteristics and behaviour of an object.
The object on the other hand is the instance of a class. The real thing, if you excuse my slang…
So we must start thinking about modelling our applications in terms of objects.
When someone, who has hired us to implement a web site-commerce site for his business, he could outline his view of the web site in plain words…
” I would like to have a site where I can keep track of the sales-orders that were placed through the site. I also would like to be able to see the customer details and manage my employees details”,
Then you must think in terms of Orders,Customer,Employee classes-objects for this particular scenario.
This is a first attempt of Abstraction for the scenario above.
Abstraction is the process of representing simplified versions of real-world objects in your classes and objects.
Programming with the OOP paradigm is to decide what a class should represent and breaking down your code into a group of interrelated classes.
Members of a class
The first thing after finalising the class names is to identify the members of a class.
I will talk about Properties, methods and events. As we go on I will talk in greater detail about class members.
A Property allows you to access an object’s data. Properties can be read-only, so they cannot be modified, while others can be changed. A Property defines the state of an object.It describes its individual data or unique configuration.
A method allows you to perform an action with an object. Unlike properties, methods are used for actions that perform a distinct task and may change the object’s state-property.
An event provides notification that something has happened. Objects can fire events to trigger the code we have placed in the event-handling routines-methods. For example, if a user clicks on a button,the button object fires a Click event, which our code can react to.
Methods, properties and events can be considered as the public interface of a class.
Now we are ready to move on and practice what we have been saying so far.
I assume that people who will read this post, have some experience with C# and Visual studio as a development platform.
I will use Visual Studio 2008 Professional edition. People who have downloaded and installed Visual web developer 2008 can also follow these examples. You can download Visual Web Developer by clicking
here .
I will create an ASP.NET application. I will create a base class and then take it from there and try to highlight all the concepts mentioned above. The point of this example is not create super sophisticated classes and methods but to create a simple class with plain properties and methods.
1) Launch VS 2008
2) Go to File->New->Project
3) From the templates, choose ASP.NET web application. Make sure you select C# as the language of development
4) Give a name for your project. I name it “LearnCLass”. Click OK on the Templates window.
5) You will have 2 main files, Default.aspx and Default.aspx.cs