Monday, February 20, 2012

JAVA CLASSES AND OBJECTS

CLASSES AND OBJECTS

Class:

  • A class is a blue print to create instance of itself.
  • A class defines constituent members which enable these class instances to have state and behavior
  • Data field members (member variables or instance variables) enable a class object to maintain state. Other kinds of members, especially methods, enable a class object's behavior. Class instances are of the type of the associated class.
  • A class usually represents a noun, such as a person, place or (possibly quite abstract) thing, or something nominalized.
  • Classes consist of and are composed from structural and also behavioral constituents, though there may not be a requirement to include any structural or behavioral constituents at all. 
  • A class contains both data (referred to as attributes), and executable code (referred to as methods).
  • Programming languages that include classes as a programming construct offer support for various class-related features. These features, and the syntaxes with which they are provided, vary greatly. Various control features such as member access specifiers may be provided by a language.

Example:

class Sample {
// field, constructor, and
// method declarations
}
  • This is a class declaration
  • The class body (the area between the braces) contains all the code that provides for the life cycle of the objects created from the class: constructors for initializing new objects, declarations for the fields that provide the state of the class and its objects, and methods to implement the behavior of the class and its objects.

Example:
class Sample extends SuperClass
implements Interface1 {
// field, constructor, and
// method declarations
}

means that Sample is a subclass of SuperClass and that it implements the Interface1 interface.

  • Here the extend keyword derives properties of the base class.Thus inheritance property is used.
  • And implements keyword creates the interface to access all methods and data members in that class


In general, class declarations can include these components, in order:
  1. Modifiers such as publicprivate, and a number of others that you will encounter later.
  2. The class name, with the initial letter capitalized by convention.
  3. The name of the class's parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
  4. A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
  5. The class body, surrounded by braces, {}.

Object:
  • An object is any entity that can be manipulated by the commands of a programming language, such as a valuevariablefunction, or data structure
  • The modern concept of "object" and the object-oriented approach to programming were introduced by the Simula programming language originally released in 1967, popularized by Smalltalk released two years later in 1969, and became standard tools of the trade with the spread of C++ originally released in 1983.
  • In the "pure" object-oriented approach, the data fields of an object should only be accessed through its methods (subroutines).

Properties of an object:
Three properties characterize objects:
  1. Identity: the property of an object that distinguishes it from other objects
  2. State: describes the data stored in the object
  3. Behavior: describes the methods in the object's interface by which the object can be used

Object creation:
  •  Object for above given program can be created by using new keyword.new keyword dynamically allocates memory to object

Example:

Sample sam=new Sample( );    //Object of class Sample
  • This sam object is used to access the all the data members and member functions in Sample class.




No comments:

Post a Comment