top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Introduction to classes and objects

0 votes
267 views

Object

An object is the software representation of a real-world entity. For example, objects in a railway reservation system may include a train, a journey destination, an icon on a screen, or a full screen with which a railway booking agent interacts. Any tangible entity in the real-world can be described as an object.  Example of real-world entities around everyone, such as a car, bike, flower or person.

Every entity has some characteristics and is capable of performing certain actions. Characteristics are attributes or features describing the entity, while actions are activities or operations involving the object.

For instance, consider a real-world entity such as a Toyota or a Ford car. Some of the characteristics associated with a Toyota or Ford car are:

  • Color

  • Make

  • Model

Some of the actions associate with a Toyota or Ford car are:

  • Drive

  • Accelerate

  • Decelerate

  • Apply brake

 

Software Object

The concept of objects in the real-world can be extended to the programming world where software “objects” can be defined.

Like its real-world counterpart, a software objects has state and behavior. The “state” of the software object refers to its characteristics or attributes whereas the “behavior” of the software object comprises its actions.

For example, a real-world object such as a car will have the state as color, make a model and the behavior as driving, changing gear and increasing speed, applying brakes.

The software objects stores its state in fields (also called variables in programming languages) and exposes its behavior through methods (called functions in programming languages).

 

Defining a class

In the real-world, several objects having common state and behavior can be grouped under a single class. For example, a car (any car in general) is a class and Toyota car (a specific car) is an object or instance of the class.

A class is a template or blueprint which defines the state and behavior for all objects belonging to that class.

Typically, in an object-oriented language, a class comprises fields and methods, collectively called as members, which depict the state and behavior respectively.

 

comparison

Class

Object

Class is a conceptual model

Object is a real thing

Class describes an entity

Object is the actual entity

Class consists of fields (data members) and functions

Object is an instance of a class

References

Java Black Book by Steven Holzner
posted Jun 15, 2017 by Sonali Gupta

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

What is Storage Class in C/C++
A storage class defines the scope (visibility) and life time of variables and/or functions within a C Program.

There are following storage classes which can be used in a C/C++ Program

auto storage class
auto is the default storage class for all local variables.

{
  int a;
  auto int b;
}

The example above defines two variables with the same storage class. auto can only be used within functions, i.e. local variables.

register storage class
Register behaves just like auto, except that instead of being allocated onto the stack, they are stored in a register.

Registers offer faster access than RAM, but because of the complexities of memory management, putting variables in registers does not guarantee a faster program—in fact, it may very well end up slowing down execution by taking up space on the register unnecessarily. As it were, using register is actually just a suggestion to the compiler to store the variable in the register; implementations may choose whether or not to honor this.

Example 
{
   register int  m;
}

static storage class
static is the default storage class for global variables. The two variables below (count and road) both have a static storage class.

static int C;
    int R;
    {
        printf("%d\n", R);
    }

static variables can be 'seen' within all functions in this source file. At link time, the static variables defined here will not be seen by the object modules that are brought in.

static can also be defined within a function. If this is done the variable is initalised at run time but is not reinitalized when the function is called. This inside a function static variable retains its value during vairous calls.

   static count=5;   
   main()
   {
     while (count--) 
     {
         func();
     }   
   }

   void func( void )
   {
     static i = 5;
     i++;
     printf("i is %d and count is %d\n", i, count);
   }

This will produce following result

   i is 6 and count is 4
   i is 7 and count is 3
   i is 8 and count is 2
   i is 9 and count is 1
   i is 10 and count is 0

extern storage class
extern is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern' the variable cannot be initalized as all it does is point the variable name at a storage location that has been previously defined.

When you have multiple files and you define a global variable or function which will be used in other files also, then extern will be used in another file to give reference of defined variable or function. Just for understanding extern is used to decalre a global variable or function in another files.

File 1: main.c

   int count=5;

   main()
   {
     write_extern();
   }

File 2: write.c

   void write_extern(void);
   extern int count;

   void write_extern(void)
   {
     printf("count is %i\n", count);
   }

Here extern keyword is being used to declare count in another file.

Now compile these two files as follows

   gcc main.c write.c -o write

This fill produce write program which can be executed to produce result. Count in 'main.c' will have a value of 5. If main.c changes the value of count - write.c will see the new value

READ MORE
...