Difference Between Classes and Objects

J.E.D.I 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.

9.3 Classes and Objects

9.3.1 Difference Between Classes and Objects

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 propertiesattributes 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. To differentiate between classes and objects, let us discuss an example. What we have here is a Car Class which can be used to define several Car Objects. In the table shown below, Car A and Car B are objects of the Car class. The class has fields plate number, color, manufacturer, and current speed which are filled-up with corresponding values in objects Car A and Car B. The Car has also some methods Accelerate, Turn and Brake. Car Class Object Car A Object Car B In s ta n c e V a ri a bl e s Plate Number ABC 111 XYZ 123 Color Blue Red Manufacturer Mitsubishi Toyota Current Speed 50 kmh 100 kmh In s ta n c e M e th o ds Accelerate Method Turn Method Brake Method Table 18: Example of Car class and its objects 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. Introduction to Programming I 129 J.E.D.I

9.3.2 Encapsulation