Menu

Search

Monday, 24 December 2012

Classes and Objects


          Java is an object-oriented language, which means that it has constructs to represent objects from the real world. Each Java program has at least one class that knows how to do certain things or how to represent some type of object. For example, the simplest class, HelloWorld, knows how to greet the world.

        Classes in Java may have methods (or say functions) and fields (or say attributes or properties).

         Let’s create and discuss a class named Car. This class will have methods, describing what this
type of vehicle can do, such as start the engine, shut it down, accelerate, brake, lock the doors,
and so on.
        This class will also have some fields: body color, number of doors, sticker price, and so on.


class Car
{
           String color;
           int numberOfDoors;
           void startEngine
           {
              // Some code goes here
            }
           void stopEngine
           {
              int tempCounter=0;

              // Some code goes here
            }
}


No comments:

Post a Comment