Programming References and tutorials for Developers.

Object-Oriented programming concept

By The Saint on Thursday, January 08, 2009

Filed Under:

What is Object-Oriented programming?

Object-Oriented programming or OOP revolves around the concept of objects as the basic elements of your programs. When we compare this to the physical world, we can find many objects around us, such as cars, lion, people and so on. These objects are characterized by their properties (or attributes) and behaviors. For example, a car object has the properties, type of transmission, manufacturer and color. Its behaviors are turning, braking and accelerating. Similarly, we can define different properties and behavior of a lion.

With these descriptions, the objects in the physical world can easily be modeled as software objects using the properties as data and the behaviors as methods. These data and methods could even be used in programming games or interactive software to simulate the real-world objects! An example would be a car software object in a racing game or a lion software object in an educational interactive software zoo for kids.

Difference between a Class and an Object

In the software world, an object is a software component whose structure is similar to objects in the real world. Each object is composed of a set of data (properties/attributes) which are variables describing the essential characteristics of the object, and it also consists of a set of methods (behavior) that describes how an object behaves. Thus, an object is a software bundle of variables and related methods. The variables and methods in a Java object are formally known as instance variables and instance methods to distinguish them from class variables and class methods, which will be discussed later. The class is the fundamental structure in object-oriented programming. It can be thought of as a template, a prototype or a blueprint of an object. It consists of two types of members which are called fields (properties or attributes) and methods. Fields specifiy the data types defined by the class, while methods specify the operations. An
object is an instance of the class.

When instantiated, each object gets a fresh set of state variables. However, the method implementations are shared among objects of the same class. Classes provide the benefit of reusability. Software programmers can use a class over and over again to create many objects.

Class Variables and Methods

In addition to the instance variables, it is also possible to define class variables, which are variables that belong to the whole class. This means that it has the same value for all the objects in the same class. They are also called static member variables.

Encapsulation

Encapsulation is the method of hiding certain elements of the implementation of a certain class. By placing a boundary around the properties and methods of our objects, we can prevent our programs from having side effects wherein programs have their variables changed in unexpected ways.

We can prevent access to our object's data by declaring them declaring them in a certain way such that we can control access to them. We will learn more about how Java implements encapsulation as we discuss more about classes.

Reference: Jedi Courseware

0 comments for this post