Sunday, August 9, 2009

Imortant Revision Notes for Classes and Constructors in Java

Class Declaration--> Syntax of class declaration is as follows:

(class modifiers) class (class name)
               (extends clause) (implements clause)  // Class header

{ // Class body
    -- field declarations
     --method declarations
     --nested class declarations
     --nested interface declarations
     --constructor declarations
     --initializer blocks

Methods of a Class--> They are the class members and are also called operations. They define behaviour of the class. 

Syntax of their declaration is as follows:

(method modifiers) (return type) (method name) ((formal parameter list))
         (throws clause) // Method prototype

{ // Method body
    --local variable declarations
    --nested local class declarations

Method Overloading--> This means several methods share same name but have different argument list.

Constructor--> It is a special method which is used to initialize state of an object when it is created using new operator. 

It has following syntax:

(accessibility modifier) (class name) ((formal parameter list))
            (throws clause) // Constructor header
  { // Constructor body
    --local variable declarations
    --nested local class declarations

However there are few constraints for a method to be constructor:
a) Their modifiers can be only accessibility modifiers.
b) They cannot have a return type
c) Their should be same as the class name.

Default Constructor--> It is the constructor with no argument. Its syntax is: classname()

Implicit Default Constructor-->If no constructor is defined in the class, then implicit default constructor is provided by java.

Its syntax is:


No comments:

Search Amazon for Best Books on Java J2EE


blogarama - the blog directory

Search your favourite topics