Search This Blog

Wednesday 28 December 2011

Java Objects with Example


Objects
Object-oriented programming focuses on constructs called “objects.” An object consists of data and functions known as methods which use or change the data. (Methods are similar to procedures or functions in other languages.)

 Objects of the same kind are said to have the same type or be in the same class. A class defines what data can be in an object, and what operations are performed by the methods. One or more objects can be created or “instantiated” from a class.

 The structure of a Java program consists of various objects exchanging messages. The keyword class defines a blueprint for objects with similar properties.

 In our first example we define the class Particle

public class Particle
{
   double x, y, vx, vy, mass;
}

This class does not do anything (because it has no methods), but it can be used to describe a particle by its position, velocity, and mass. For simplicity, we will consider only two dimensions.

No comments:

Post a Comment