Introduction to object-oriented programming



Introduction to object-oriented programming

• 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 the 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.

[pic]

       Return to beginning of  Object-oriented Design and Programming Concepts

Class

• 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.

[pic]

       Return to beginning of  Object-oriented Design and Programming Concepts

Object

• An object is an instance of a class.  Creating an object is also known as instantiation.  For example, the object "my Porsche" is an instance of the car class.

[pic]

       Return to beginning of  Object-oriented Design and Programming Concepts

Method

• 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.

[pic]

       Return to beginning of  Object-oriented Design and Programming Concepts

Message Passing

• 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.

[pic]

       Return to beginning of  Object-oriented Design and Programming Concepts

Inheritance

• Inheritance (also known as subclasses) occurs when a specialized version of a class is defined.  The subclass 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.

[pic]

• 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".

[pic]

• The object of "my Porsche" may be instantiated from class "Porsche Carrera GT" instead of class "Car".  This allows sending a message to invoke the new method "deploy rear wing spoiler".

[pic]

       Return to beginning of  Object-oriented Design and Programming Concepts

Abstraction

• 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.  It may sometimes be treated as a "Porsche Carrera GT" to access specific properties and methods relevant to "Porsche Carrera GT".  It could also be treated as a vehichle, the parent class of "Car" when considering all traffic in your neighborhood.

[pic]

       Return to beginning of  Object-oriented Design and Programming Concepts

Encapsulation

• 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.  In this example, 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 accelerate message.

[pic]

• 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.

[pic]

       Return to beginning of  Object-oriented Design and Programming Concepts

Polymorphism

• 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.

[pic]

• 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.

[pic]

       Return to beginning of  Object-oriented Design and Programming Concepts

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download