Object-Oriented Programming Concepts Real world objects include things like your car, TV etc. These objects share two characteristics: they all have state and they all have behavior. Software objects are modeled after real-world objects in that they, too, have state and behavior. A software object maintains its state in variables and implements its behavior with methods. In an OO language like Java or C++, the basic building block is the object, an entity that has its own data (variables) and its own methods for manipulating that data and interacting with other objects. OOP refers to the art of decomposing an application into some number of objects that work together. An Object Methods Variables For e.g., a Circle object would need to describe its center (x and y coordinates) and its radius. This object must also be able to set its center coordinates or its radius, and report its radius to other object.
Description of a Circle object Data float x_coord // the x-coord of the center float y_coord // the y-coord of the center float radius // the radius Methods void setcenter(float x, float y) //accept two numbers and //set the coordinates to them void setcenter(float rad) //accept one number and set the //radius float getradius() // returns the radius value These variables and methods are known as instance variables and instance methods to distinguish them from class variables and class methods. In Java, everything is an object, with the exception of the primitive types of information like integers. Every Java program is a collection of cooperating objects, passing requests to other objects to perform their methods, and responding to similar requests from other objects. Objects communicate with each other by sending messages to each other. Message components include: 1. The object to whom the message is addressed. 2. The name of the method to perform. 3. Any parameters needed by the method. Objects don't need to be in the same process or even the same machine to send and receive messages to each other.
Benefits of Encapsulation Encapsulating related variables and methods into an object provides two major benefits to software developers: 1. Modularity: The source code for an object can be written and maintained independently of the source code for other objects. An object can also be passed around in the system. 2. Information Hiding: An object can maintain private information and methods that can be changed at any time without affecting other objects that depend on it. E.g. you don't need to know how a particular method is implemented; just how to use it. Classes A class is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind. Example of the Circle class: Class Circle { private float x_coord; private float y_coord; private float radius; public void setcenter(float x, float y) { public void setcenter(float rad) { public float getradius() {
A class is used to instantiate (create an instance of) objects. Circle c1; //declaration of var c1 that creates a var (a //reference) that refers to an object of type Circle. c1 = new Circle(); //dynamically creates the object in //memory using the new keyword. To use this circle object, c1: c1.setcenter(1.0,2.0); float radius = c1.getradius(); Each object has its own set of instance variables. Values of these variables in one object can differ from the values in another object. Each object is associated with an instance method(s) but does not have its own copy of the method(s). Objects are created (instantiated) with the new keyword E.g. Circle c1 = new Circle(); The Circle() is called the constructor and specifies the type of object and any required parameters to create it. Another e.g. int hours = new Date().gethours(); Visibility modifiers (e.g. private) restrict access to variables and methods. So the following statement is illegal: float radius1 = c1.radius; //needs the getradius() method