Sunday, April 8, 2012

Look here for all the components of Java.

Here we will learn some basics of Java components like

1- String

2- Collection

3- Thread

4- Override & Overload

5- Exceptions

6- Access Modifier

Basic of Java is Object. Everythign in Java except premitives are Object it means every thing you declare in Java will extend Object class(Implicitly / explicitly).

There are 9 method in Object class.

Note : Even Wait, Notify, NotifyAll methods are related to Thread, these methods are part of Object class. Because these methods works on the lock and lock belongs to Object and not Thread.

Method equals() internally uses '==' to compare to references. so in case of Object or any other class there is no difference between theses two. (Unless given class override equals() methods which is true in case of String class)

String class overrides equals() methods to provide different functionality.

A class is the combination of variable(State) and Method(Behavior).

Variable in JAVA.

1- Instance Variable : Variables defined outside any method in class. No need to initialize before use.

2- Local Variable: Variables defined inside any method. Need to initialize before use for the first time.

Methods in JAVA

Here is a typical Method Declaration.

Public static void method(int i, String r) throws Exception{// Do something here}

Required element of method declaration are return type, name,() & {}. Others are optional.

Method Declaration have 6 component in given order

1- Modifier

2- Return Type

3- Method Name

4- Parameter list

5- Exception

6- Method Body

Note**: Method Signature comprise method name and parameter type.

Overloading Method: Methods within java class can have same name if they have different parameter(Type/Number). This scenario is called Overloading. Java can distinguish between methods with different method signatures. Overloaded methods are differentiated by the number and type of the argument passed into method.

Note**: Return type is not the part of method signature so compiler does not consider it while differentiating method.

Rules of Overloading:

1- Method name needs to be same.

2- Method parameter needs to be different.

Constructor :

Every class in Java contains constructor(Implicit / Explicit), which is used to create object from class blueprint. Constructor name should be same as class name. Constructor is not a Method.

Class basic{

public basic(){} //Constructor it does not have a return type

public void basic(){} //Method it does have a return type.

}

It is not mandatory to provide constructor for the class. If you don't write, compiler will provide one.

Class can have more then one constructor(Overloaded). If class has explicit constructor then compiler would not provide no argument constructon for the same class.

While initialising, constructor will call super class constructor(Implicitly / Explicitly).

First line of constructor should be this/super.

Constructor can have different Access Modifier(public/protected/private).

Controlling Access to Class Member: Look @ this link

Instance & Class Member:

No comments:

Post a Comment