Tip:
Highlight text to annotate it
X
An object-oriented program may be considered a collection of interacting objects.
Each object is capable of sending and receiving messages, and processing data.
Consider objects of a driver, a car, and a traffic light.
When the traffic light changes, it sends a virtual message to the driver.
The driver receives the message, and then chooses to accelerate or decelerate.
This sends a virtual message to the car.
When the car's speed changes, it sends a virtual message back to the driver, via the speedometer.
The fundamental concepts of object-oriented programming include:
Class, Object, Method, Message passing, Inheritance, Abstraction, Encapsulation, Polymorphism
A class defines the characteristics of an object.
Characteristics include: Attributes (fields or properties), and behaviors (methods or operations)
For example, a "Car" class could have properties such as: year, make, model, color, number of doors, and engine.
Behaviors of the "Car" class include: On, off, change gears, accelerate, decelerate, turn, and brake.
An object is an instance of a class.
For example, the object "my Porsche" is an instance of the car class.
A method is a behavior of an object.
Within a program, a method usually affects only one particular object.
In our example, all cars can accelerate, but the program only needs to make "my Porsche" accelerate.
Message passing (or method calling) is the process where an object sends data to another object to inkove a method.
For example, when the object called "joe" (an instance of the driver class), presses the gas pedal,
he literally passes an accelerate message to object "my Porsche", which in turn, invokes the "my Porsche" accelerate method.
Inheritance (also known as subclasses) is a specialized version of a class.
It inherits attributes and behaviors from the parent class.
For example, the "Car" class could have subclasses called "Porsche car", "Chevy car", and "Ford car".
Subclasses inherit properties and methods from the parent class.
The software engineer only only has to write the code for them once.
A subclass can alter its inherited attributes or methods.
In our example, the "Porsche car" subclass would specify that the default make is Porsche.
A subclass can also include its own attributes and behaviors.
We could create a subclass of "Porsche car" called "Porsche Carrera GT".
The "Porsche Carrerra GT" class could further specify a default model of "Carrera GT" and "number of doors" as two.
It could also include a new method called "Deploy rear wing spoiler".
The object of "my Porsche" may be instantiated from class "Porsche Carrera GT" instead of class "Car".
Abstraction is the practice of reducing details so that someone can focus on a few consepts at a time.
For example, "my Porsche" may be treated as a "Car" most of the time;
and treated as a "Porsche Carrera GT" to access specific properties and methods relevant to "Porsche Carrera GT";
and treated as a vehichle, the parent class of "Car" when considering all traffic in your neighborhood.
Encapsulation conceals the functional details of a class from objects that send messages to it.
For example, the "Porsche Carrera GT" class has a method called "accelerate".
The code for the "accelerate" method defines exactly how acceleration occurs.
Fuel is pumped from gas tank and mixed with air in the cylinders.
Pistons move causing compression, resulting in combustion, etc.
Object "Joe" is an instance of the "Driver" class.
It does not need to know how "my Porsche" accelerates when sending it an acclerate message.
Encapsulation protects the integrity of an object by preventing users from changing internal data into something invalid.
Encapsulation reduces system complexity and thus increases robustness, by limiting inter-dependencies between components.
Polymorphism is the ability of one type to appear as (and be used like) another type.
Classes "Porsche Carrera GT" and "Ford Mustang" both inherited a method called "brake" from a similar parent class.
The results of executing method "brake" for the two types produces different results.
"my Porsche" may brake at a rate of 33 feet per second, whereas "my Mustang" may brake at a rate of 29 feet per second.
This concludes the tutorial on object oriented programming concepts.