Aug 26, 2011

Class

In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.
Syntax of a class:
      modifier  class ClassName extends ClassName1 implements Interface1,Interface2..
          {
          }
Modifiers
       Modifiers are keywords that you add to those definitions to change their meanings. The Java language has a wide variety of modifiers, including the following:
                
                    1. Access Modifiers
                    2.Non Access Modifiers
Access Modifiers
        Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are:
  • Visible to the package. the default. No modifiers are needed.
  • Visible to the class only (private).
  • Visible to the world (public).
  • Visible to the package and all subclasses (protected).
Non Access Modifiers
            Java provides a number of non-access modifiers to achieve many other functionality.
  • The static modifier for creating class methods and variables
  • The final modifier for finalizing the implementations of classes, methods, and variables.
  • The abstract modifier for creating abstract classes and methods.
  • The synchronized and volatile modifiers, which are used for threads.

Difference between access specifiers and modifiers
        Java Access Specifier is works as super class of the non-Access Modifier and Access Modifier to specifies accessibility to java code.
Java Access Modifier comprises four categories:

                    Default
                    Protected
                    Public
                    Private

Java Access Specifier comprises three categories:

                   Private
                   Public
                   Protected

Defining classes
To define a class, we use the class keyword and the name of the class:


                          class MyClassName
                          {

                          }

By default, classes inherit from the Object class.
 If this class is a subclass of another specific class (that is, inherits from another class), use extends to indicate the superclass of this class:

                         class myClassName extends mySuperClassName
                         {

                         }

Following is the code for a class called SimplePoint that represents a point in 2D space:

                         public class SimplePoint
                         {
                            public int x = 0;
                            public int y = 0;
                         }

This segment of code declares a class-- a new data type really-- called SimplePoint. The SimplePoint class contains two integer member variables, x and y. The public keyword preceding the declaration for x and y means that any other class can freely access these two members. 
We create an object from a class such as SimplePoint by instantiating the class. When we create a new SimplePoint object space is allocated for the object and its members x and y. In addition, the x and y members inside the object are initialized to 0 because of the assignment statements in the declarations of these two members.

                             

Example:
                             public class SimpleRectangle
                             {
                                public int width = 0;
                                public int height = 0;
                                public SimplePoint origin = new SimplePoint();
                              }

Here the segment of code declares a class SimpleRectangle-- that contains two integer members, width and height.
SimpleRectangle also contains a third member, origin, whose data type is SimplePoint.
Here the class name SimplePoint is used in a variable declaration as the variable's type. We can use the name of a class anywhere we can use the name of a primitive type.
 As with SimplePoint, when we create a new SimpleRectangle object, space is allocated for the object and its members, and the members are initialized according to their declarations.
 The initialization for the origin member creates a SimplePoint object with this code: new SimplePoint() as illustrated here:
                 

This diagram shows the difference between primitive types and reference types


0 comments :

Post a Comment