Object-Oriented Programming Concepts
Some of the terms used in object-oriented programming include:
An object is a software bundle of related variables and methods. Software objects are often used to model real-world objects you find in everyday life. Examples of real-world objects include a dog, a desk, a television set and a bicycle.
These real-world objects share two characteristics: They all have state and behavior. For example cars have state (current speed, current gear, four wheels and number of gears) and behavior (braking, accelerating, slowing down, and changing gears).
Software objects are modeled after real-world objects in that they too have state and behavior. A software object maintains its state in one or more variables. A variable is an item of data named by an identifier. A software object implements its behavior with methods. A method is a function (subroutine) associated with an object.
This refers to packaging of an object’s variables within the protective custody of its methods. It is a simple yet powerful idea that provides two primary benefits to software developers:
- Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Also, an object can be easily passed around in the system. You can give your bicycle to someone else, and it will still work.
- Information hiding: An object has a public interface that other objects can use to communicate with it. The object can maintain private information and methods that can be changed at any time without affecting the other objects that depend on it. You don’t need to understand the engine of a vehicle to drive it.
Software objects interact and communicate with each other by sending messages .When object A wants object B to perform one of B’s methods, object A sends a message to object B
The three components that comprise a message are described explained using the example below:
- The object to which the message is addressed
- The name of the method to perform
- Any parameters needed by the method
Messages provide two important benefits.
- Message passing supports all possible interactions between objects.
- Objects don’t need to be in the same process or even on the same machine to send and receive messages back and forth to each other.
A class is a blueprint or prototype that defines the variables and the methods common to all objects of a certain kind.
In object-oriented software, it is possible to have many objects of the same kind that share characteristics: for example; rectangles, employee records, video clips, and so on. Programmers can take advantage of the fact that objects of the same kind are similar and can create a blueprint for those objects. A software blueprint for objects is called a class.
Inheritance is the ability of a class to acquire properties of another class. If a class say A acquires properties of another class say B then A is said to be the subclass and B a superclass.
Inheritance offers the following benefits:
- Code Reuse
Through the use of inheritance, programmers can reuse the code in the superclass many times.
- Defining generic behavior using abstract classes
Programmers can implement superclasses called abstractclasses that define the general behavior of the object and leave the rest to other programmers who fill in the details with specialized subclasses.
An interface is a collection of unimplemented methods and data items. This term is different from the one used in GUI. The term interface as defined above refers to a list of public methods provided by a class. In fact, other object-oriented languages have the functionality of interfaces, but they call their interfaces protocols. A class that implements an interface must implement all of the methods declared in that interface.
Interfaces are useful for the following:
- Capturing similarities among unrelated classes without artificially forcing a class relationship.
- Declaring methods that one or more classes are expected to implement.
- Revealing an object’s programming interface without revealing its class.