Thursday, 5 January 2012

Constructor


Constructor:

-          It is a specialized method od a  class which is invoked when an object is created for the class.
-          The difference occurs in the following areas:
o    Name
o    Return Type
o    Type of Calling
o    Time of Calling
o    Frequency of Calling
o    Purpose

Name:
                Constructor Name and Class Name should be same.
                Ex:                   
     

Return Type:
                Constructor name shouldn't have any return type.
Type of Calling:
                Constructor is called as soon as object is created. It is the first method called by the Object.            
Time of Calling:
                A Constructor is Called Implicitly.
Frequency of Calling:
A constructor will be called when the object of the respective class is created and also when the objects of the derived class are created.
Purpose:
The Main purpose of the constructor is, It carries the code which need to executed as soon as object is created.
               
There are 2 types of constructors:
a)       Parameterized constructor.
b)      Un-parameterized constructor/ Zero argument constructor/ Default/ Block

Ex:






          With in the class if there are more than one constructor, then it is called as ‘Constructor Overloading’


-          In this case, by default Jcompiler calls EMP() i.e default constructor. If you want to call the parameterized constructor first, pass parameters while Object declaration.


-          One constructor of a class can call another constructor of the same class. This is called constructor chaining.







Wednesday, 4 January 2012

PATH & CLASSPATH Setting

PATH & CLASSPATH Settings:

PATH:
-          It's an environment variable used by OS to locate the Software.
-          The following path you need to configure.
1.       Identify Java jdk in C:\\programs files.


2.       Goto bin dir in jdk.



3.       Copy the url.


4.       Open ‘Control Panel\System and Security\System’



5.       Click on Advanced System Settings.



6.       Click on Environment variables.


7.       Click on New in Uer variables to set Path. ( you can use system variables, but system restart is required to make changes)

8.       Enter the following details:
a.        Variable Name:             PATH
b.       Variable Value:             C:\Program Files\Java\jdk1.6\bin;


9.       Click on Ok.
10.    Goto java jdk folder.
11.    Open jre> lib folder.


12.    Search for rt.jar (runtime jar)
13.    Copy the URL. (You cant copy rt.jar included in the URL)
14.    Come back to Environment variables..
15.    Click on New in user variables to set class path.
16.    Enter the following details:
a.        Variable Name:             CLASSPATH
b.       Variable Value:             C:\Program Files\Java\jdk1.6.0_29\jre\lib\rt.jar;


17.    Click OK. + OK.











First Java Program


/* First Java Program */

1.       Print Hello World using Java Program.

class HelloWorld
{
                Public static void main( String args[] )
                {
                                System.out.println( " Hello World..!!! ");
                }
}

-          Save the file with HelloWorld.java in any folder. ( You can use any name for the file, but need to execute with class name only ).
-          Open cmd. ( Goto Run, type cmd)
-          Compile & Execute the application as follows:


Javac is used to compile the application.
Java is used to execute the application.

Program Explanation:
*       In Java, Entry point is main().
*       Hence JVM first calls main(). To understand the java architecture, refers to above article.
*       We define the class HelloWorld inorder to encapsulate main method.
*       Here, class = Keyword and HelloWorld = identifier.
*       Every class name should be Uppercased, if it is concate nated, each word must start with uppercase.
Ex1: Hello
Ex2: HelloWorld

                Description about main():
*       Public:
Main is defined as public because to give permission to JVM to call it.
*       Static:
Main is defined as static because the JVM can call it without object creation.
*       Void:
It is declared as void because, JVM doesn't need any response back from main().
*       String args[]:
Main contains args[] as parameter because it provides the facility to input variables from command prompt.

                Compiling and Execution:




Types of Variables in Java


Types of Variables:

There are various variables on the basis of scope in Java. They are:
1.       Class Variables;
2.       Instance Variables;
3.       Local Variables;
4.       Static Variables;
5.       Automatic Variables;

Class Variables:
-          These are accessible with in a class and its object.
-          These must be declared inside the class before they are used.

Instance Variables:
-          These are declared inside a class and are created when the class is instantiated.
-          Object gives different values to these instance variables.

Local Variables:
-          They are declared inside the methods.
-          Their scope is within the block of code in which they are defined.
-          They can’t be accessed outside the code.

Static Variables:
-          These variables have memory allocated only once, but they are globally accessed through all instances of the class.
-          Even if the instance is destroyed, these variables are not destroyed.

Automatic Variables:
-          These are accessible within the function in which they are declared.
-          The automatic variables are created when a function is called and are destroyed when we exit the function.

*       For allocation of memory, we use new.
*       For DE allocation of memory, we use Garbage Collector.

Java Architecture


Java Architecture:


Features of OOPs


Features of Object Oriented Programming Language:
-          We have 3 main features, and 8+ basic features.
1.       Encapsulation
2.       Polymorphism
3.       Inheritance

Encapsulation:
Combining the data and the eligible code that acts upon the data into a single unit of an organization so that they are safe from outside interface is called an Encapsulation.





Ø  It hides the non-essential features of an object and provides access privileges only to the essential components.
Ø  It secures data by preventing free-flow of data throughout the application.
Ø  Class represents the basis of Encapsulation, Objects implement Encapsulation.

Inheritance:
Ø  A new entity acquiring the features from the existing entities is called ‘Inheritance’.
Ø  The New Class which inherits the properties is called as ‘child class’, the existing class which is inherited is called as ‘parent class’.



Ø  The Subclass inherits properties from Base class.

Polymorphism:
Ø  ‘Poly’means ‘many’
‘morphism’means ‘forms’
Ø  One interface many forms is the Key concept of polymorphism.
Ø  Polymorphism offers extensibility of code which refers to only one method can be used to get only one functionality but it can be executed to get other functionality.
Ø  This improves flexibility in processing and compactness in application development is achieved.
Ø  With the help of polymorphism, we can generate multiple services of an object with a single method, the behavior of object varies wrt context.


Classes and Objects


Classes and Objects:

-          A class is an user defined datatype which consists of methods and memberVariables.
-          An object consists of the data and the code that acts up on the data.
Ex:
                Class Emp  {
                                int empno;                                          //member variable
                                String ename;                                    //member variable
                                float salary;                                         //member variable
                               
                                public String getEname() {             //method
                                                return ename;
                                }
                               
}

To access methods or memberVariables we need instances, like
                Emp emp1 = new Emp();
                emp1.getEname();


-          In simple language,
Class is a blueprint of an Object, Object is an instance of a class.
-          In OOPs, the fundamental data storage is an Object. i.e..



-          Hence,
A class is a conceptual part of a program ,
An object is the physical representation of a class.
-          Any object must possess the following characteristics:

o    State
§  It represents the data.
§  It is indicated by attributes , values for these attributes.
o    Behavior
§  It represents the methods.
§  It refers to the change of the state over a period of time.
o    Identity
§  It represents the name of the object.
§  Each object must have an unique identity.