Object Orientated Programming is a very meaty subject, and can be a difficult concept to grasp. This short tutorial will hopefully explain the concept, so that when we look at the code, its not so alien and you can understand a little about what is going on.
This article is designed to give you an overview of the concept of Object Orientated Programming. It is not intended to teach the entire subject or to cover in-depth the ins and outs of classes and objects. With that in mind, I hope this guide will help you understand what a class is and how they are used in an object-orientated methodology.
What is a Class
A class can be thought of as a noun, e.g. a person, place or thing. Noun's have attributes that describe them such as Size, Mass, Temperature, Speed and so on. These are referred to as the fields of a class.
A noun can also have actions or processes associated with it. For example a car has a field called Speed, it can also have an action Accelerate which will change the field Speed. These actions are called methods.
Question
Can you sort these words into Fields and Methods?
- indicate
- change gear
- brake
- speed
- amount of fuel
- wash windscreen
- is engine running
You should have identified the fields and methods as follows:
- indicate - Method
- change gear - Method
- brake - Method
- speed - Field
- amount of fuel - Field
- wash windscreen - Method
- is engine running - Field
Improved Definition of a Class
My initial definition of a class was a little inaccurate. The true definition of a class, based on my initial definition would be: "A Class is a blueprint for a noun."
A class cannot be used by itself, but it is used to instantiate (create) an instance of an object, which shares all the same fields and methods. Because it is a blueprint, you can create many, many instances, all with the same fields and methods. An instance of a class is called an object. One thing you must keep in mind is that each object that is created from a class is unique, each instance of a class will have its own values for the fields, totally separate from the values of the other objects.
This concept of creating one class that can be reused many times is the foundation of object orientation and code reuse.
Why use classes?
Now, lets have a look at where object orientation really comes into its own. There is a concept known as "inheritance", where one class can inherit the methods and fields of another (base) class and also add new functionality, or replace (override) the methods of the base class. This allows the new class to be more focused, targeted towards the object being modelled. This process is called inheritance.
To put that into English with the car example, every car manufacturer uses the same base class. This class defines a basic car with four wheels, a steering wheel, brakes, headlights, seats etc… However if every manufacturer only used this blueprint, every car produced would be exactly the same - what would be the point?
What we can do is take the functionality from the base class and create another car class and add functionality to it. We can add a method for central locking; we can add a field for water temperature and paint colour. A different manufacturer may use trim colour and oil temperature and a method ABS brakes. We can override the methods from the base class, so that the basic seats are now heated leather seats, the headlights are HID.
Classes can also use other classes. Our base car class can use the base engine class. Our new car class can override this base engine class to use a super turbo charged jet powered engine if we wish.
Summary and Conclusion
- Object Orientated Programming allows us to programmatically model the real world using classes.
- Classes are blueprints for objects, which can be thought of as nouns.
- Classes have methods and fields, which are similar to the actions, processes or attributes of a real life object
- Classes use inheritance to create a more defined, targeted class.
- Classes can override the methods inherited from its base class.
This is a very generalised overview of the concept, there are a great many features of Object Orientated Programming that have not been covered here, such as polymorphism, abstraction, interfaces, partial classes etc..
No comments:
Post a Comment