Wednesday, January 13, 2010

Methods,Classes

Classes

All object-oriented programming languages support the concept of a class. As with a table definition, a class provides a template for objects that share common characteristics. Each class can contain the following:

* Attributes

Attributes are variables that are present in each object, or instance, of a particular class. Attributes within an instance are known as fields. Instance fields are analogous to the fields of a relational table row. The class defines the variables and the type of each variable. You can declare variables in Java as static and public, private, protected, or default access.

Variables of a class that are declared as static are global and common to all instances of that class. Variables that are not declared as static are created within each instance of the class.

The public, private, protected, and default access modifiers define the scope of the variable in the application. The Java Language Specification (JLS) defines the rules of visibility of data for all variables. These rules define under what circumstances you can access the data in these variables.

In the example illustrated in Figure below, the employee identifier is defined as private, indicating that other objects cannot access this attribute directly. In the example, objects can access the id attribute by calling the getId() method.
*

Methods

Methods are blocks of code that perform specific tasks or actions. You can call methods on an instance of a class. You can also call methods that are inherited by a class. Methods provide a way for instances to interact with other instances and the application. Similar to attributes, methods can be declared as public, private, protected, or default access.
Classes and Instances

No comments: