Intro to Objects & Classes 


 


Object-Oriented Languages

  • Object Oriented Programming is a programming language paradigm that builds upon structured programming in procedural programming languages with the concept that procedures and the data on which they operate can be viewed as two parts of the same thing: an object. 

  • An object-oriented programming language is generally characterized by four distinguishing features--abstraction, polymorphism, inheritance, and encapsulation (sometimes referred to collectively as A-PIE). 

    • abstraction: ignoring irrelevant features, properties, or functions and emphasizing those that are relevant to the given project.

    • encapsulation: the act of grouping into a single object both data and the operations that affect that data.   

    • inheritance: the process of creating new classes from existing classes . 

    • polymorphism: the ability to perform the same operation on different types of objects, each one implementing that operation in a way appropriate to them. 


  • Object-oriented designs are better able to more accurately model the real world than procedural languages.

    • Instead of viewing the world as a set of processes, it instead views the world as a collection of entities that are characterized by certain behaviors.

    • Furthermore, objected-oriented languages make it easier to create new data types to better represent real-world objects.

  • The fundamental idea behind object-oriented languages is to combine both data and the functions that operate on that data into a single unit called an object.

    • An object's functions, referred to as methods, provide the only way to access its data.

    • In OO terminology, methods are also referred to as behaviors.

    • In order to read a data item in an object, one of the object's methods must be called to read the data item and return its value.

    • The data cannot be accessed directly.

    • It is hidden, and is safe from accidental alteration.

    • Data and functions are said to be encapsulated into a single entity.

  • Both data encapsulation and data hiding are key concepts in the object-oriented paradigm.

  • The restriction of allowing only an object's methods to change that object's data insures that nothing else can alter that data, and therefore simplifies writing, debugging, and maintaining the program.


Concepts of the Object-Oriented Paradigm

Objects

  • An object is a computer representation of some real-world thing or event.

  • Example: if you own a Jeep, you might know several of its characteristics, such as the name of the model (Jeep Wrangler), the vehicle ID number (#51Y62BG826341Y), and the motor type (6­Cyl).

  • Objects have both attributes (such as the model, VIN, and motor type) and behaviors (such as "lights go on" and "lights go off").

Classes

  • A class is a category of similar objects.

  • A class defines the set of shared attributes and behaviors found in each object in the class.

  • Objects are grouped into classes.

  • Example: a group of objects representing automobiles might be formed into a class called "Automobile." Every automobile will have values for the attributes Make/Model, VIN, and Engine.

  • The data components of a class are called instance variables.

  • Classes must be defined by the programmer. When the program runs, objects can be created from the established class. The term instantiation is used when an object is created from a class. For example, a program could instantiate the Jeep Wrangler as an object from the class Automobile.

  • Just as an instance of a primitive type is called a variable, an instance of a class is called an object.

  • There may be many objects of a class, just as there may be many variables of a primitive data type.

  • Placing data and functions together into a single entity is the central idea of object-oriented programming.


Classes and Objects

  • An object has the same relationship to a class that a variable has to a data type.  (Recall user-defined types.)

  • An object is said to be an instance of a class.

Methods

  • Methods are member functions that are included within a class in order to implement a specific behavior that objects of that class are capable of performing.

  • In order for an object to perform an action, one of its methods must be invoked.

  • If a class does not include a method for a particular action, then objects of that class cannot perform that action.

Message Passing

  • Message passing is a way of communicating between objects. 

  • Example: an object of the class "Driver" sends a message to an object (Jeep Wrangler) of the class "Automobile" instructing it to "Start Engine."

  • Each class must be programmed to send and/or receive the appropriate messages.

  • The use of the term message emphasizes that objects are discrete entities and that we communicate with them by calling their member functions.

  • The classes Driver and Automobile have been programmed to send and receive a Start Engine message. 

    • The Driver class has been programmed to transmit a Start Engine message under certain conditions.

    • The Automobile class has been programmed to react to a Start Engine message in some way.

Encapsulation

  • Encapsulation is the act of grouping into a single object both data and the operations that affect that data.  

  • Access to data within an object is restricted to only those methods defined by the object's class.

  • If data is encapsulated, an object must be directed to change its own data with a message, because outside processes are prohibited from altering the nature of the object.

  • Example: a mechanic (class Mechanic) sends a "Pull Engine" message to the Jeep object (class Automobile). The Automobile class reacts to this message with a behavior (also called a "method" or "procedure") that changes the Engine attribute to None. This method is named "Pull Engine." The Jeep object reacts to the message by changing one of its attributes to None.

  • It is important to note that encapsulation exists only if data is protected in such a way that only the object itself can make such changes through its own behavior. 

  • Encapsulation is significant because 

    1. it makes it easier to build objects that are very reliable and consistent because they have complete control over their own attributes, and 

    2. it makes program maintenance and change much easier.

  • This isolation of data makes it much easier to change one part of a program without causing problems to cascade into other parts of the program.

  • For example, the Mechanic class is isolated completely from the internal details of the Automobile class. The Automobile class can be totally reprogrammed without changing anything in the Mechanic class, as long as the Automobile class continues to receive a "Pull Engine" message properly.

Inheritance

  • Inheritance is a form of software reusability in which new classes are created from existing classes by absorbing their attributes and behaviors and embellishing these with capabilities the new classes require.

  • The original, or parent class is known as a "base class."

  • The child class is called a "derived class."

  • A derived class can be created in such a way that it will inherit all of the attributes and behaviors of the base class.

  • Example: a derived class (Truck) is created in such a way that it inherits all of the attributes of the base class Automobile.

  • A derived class shares all the characteristics of a base class, but can add new characteristics as well. A derived class is not limited to the inherited attributes and behaviors.

  • For example, the class Truck not only has attributes for Make/Model, VIN, and Engine, but also has attributes for Cargo, Trailers, and Refrigerated. Automobile objects do not have these added attributes.

  • Inheritance reduces programming labor by reusing old objects. The programmer only needs to declare that the Truck class inherits from the Automobile class and then provide any additional details about new attributes or behaviors.

  • (All of the old attributes and behaviors of the Automobile class are automatically and implicitly part of the Truck class and require no additional programming.)

Extensibility

  • Extensibility refers to the creation of new data types from existing data types.

  • Inheritance is a form of extensibility, but note that not only can user-defined types be extended, but base types can be extended as well.

  • example: Coordinates (position1 = position2 + origin)

    • This example requires creation of a new class that represents a pair of independent numerical quantities, and declaration of position1, position2, and origin as objects of this class.

Polymorphism

  • Polymorphism refers to the ability to manipulate objects of distinct classes using only knowledge of their common capabilities without knowing their exact class.

  • Polymorphism enables a message to have different effects depending on exactly what class of object receives the message.

  • When multiple classes inherit both attributes and behaviors, there can be cases where the behavior of a derived class might be different from that of its base class or its sibling derived classes.

  • Example: position arithmetic above differs from normal arithmetic

  • When an existing operator is given the capability to operate on a new data type, it is said to be overloaded.

Reusability

  • Once a class has been written, created, and debugged, it can be used in multiple programs.

  • Reusability can be utilized in conjunction with inheritance to create derived classes from a base class.

Abstraction

  • In its simplest sense, abstraction refers to deferring an explanation of the details of what is being modeled in order to simplify a complex phenomenon to a level where analysis, experimentation, or understanding can take place.

  • Abstraction is a design technique that focuses on the essential aspects of an entity and conceals the less important or non-essential aspects.

  • In software, abstraction is concerned with the attributes and behaviors of entities.

Information Hiding

  • Information hiding is the technique of concealing implementation details within the objects themselves, while the interface remains visible.

  • Objects make information hiding possible , which means that although objects may know how to communicate with each other, they normally are not allowed to know how other objects are implemented.

  • Data is concealed within a class, so that it cannot be accessed mistakenly by functions outside the class.



Continue...

VB now OO