Monday, April 22, 2013

For loop in java

For loop in java


In for loop initialization, condition check
and increment is done in one loop
format of for loop

for(initialization : condition : increment)

We can take the same example of table for
better understanding
class table
{
 public static void main(String[] args)
{
      int num=5,table=0,j;
      for(j=1;j<11;j++)
      {
          table=j*5;
         
          System.out.println(+table);
      }
}
}

In above program
            for(j=1;j<11;j++)
initialization is done
j=1
than condition is check
j<11
if it is true then body of loop is executed
after execution of body of loop
increment is done
j++
now j=2
then again condition check
this procedure is repeated until the condition is false
When condition gets false then execution of program goes out of the loop

Method Overriding



Method Overriding


In inheritance when we declare same method in
Parent class as well as in child class is called as method
overriding

suppose we declare method  
 void display(); in super class A

and take new class B extends  class A
and declare the same 
method void display();in  child class B

Then same method we declare in class B
Override the method declare in class A

Example
class A
{
void display() // method declaration
{
System.out.println("abc");
}
}
class B extends A
{

void display() // again we declare same method
{
System.out.println("xyz");


}
}

class example
{
public static void main(String [] args)
{
B obj=new B();
obj.display();
}
}
What is actually method overriding
Here in above program there are two similar
Methods
void display();
so what output we get from this program
method in child class override the method in parent
class
hence we get  output xyz

Its called as method overriding

Method Overriding



Method Overriding


In inheritance when we declare same method in
Parent class as well as in child class is called as method
overriding

suppose we declare method  
 void display(); in super class A

and take new class B extends  class A
and declare the same 
method void display();in  child class B

Then same method we declare in class B
Override the method declare in class A

Example
class A
{
void display() // method declaration
{
System.out.println("abc");
}
}
class B extends A
{

void display() // again we declare same method
{
System.out.println("xyz");


}
}

class example
{
public static void main(String [] args)
{
B obj=new B();
obj.display();
}
}
What is actually method overriding
Here in above program there are two similar
Methods
void display();
so what output we get from this program
method in child class override the method in parent
class
hence we get  output xyz

Its called as method overriding

Inheritance in Java



Inheritance in Java


·       Inheritance is very important concept in java

  • Java allows you to extends a class  with another class
  •  Is use for sharing between two classes (from parent class to child  class)    
  • extends keyword provide inheritance in java


Example for your understanding 

class A         // its our base class or parent class
{
int a=12;   //initialization the value of variable a=2
}

class B extends A    // its our derived class or child class
{
void display()       // declaration of method display
{

System.out.println(+a);    // for print value of a which initialize in claas A
}
}

class demo // main class
{
public static void main(String [] args)   // main method
{

B obj=new B();    //create object for class B

obj.display();    //accessing method display
}
} 

Firstly remember that
// after this line you can give comment
Anything you written after // is your comment
For that hole line only


Explanation of the above program

Here we take three classes
class A
class B
and class demo its our main class

class A is our parent class
and
class B extends A
means
all properties of patent class A is extended
by the child class B

we declare method display in class B

System.out.println(+a);

Here we want to print the value of
'a'
but value is initialize in class A
we know that by extend keyword
we can access all properties of class A
and we given value of a=12 in class A

hence in above program we get output
12

Thursday, April 18, 2013

Method declaration in Java

Method declaration in Java

Why method is necessary
It is necessary for operation on data in the
present in class

format for the method
type methodename (parameter)
{
Body of method
}

Example:


    class A
{
    int c,x,y;
   
    void cal(int x,int y)    //IT IS YOUR METHOD
    {
       
        c=x+y;
        System.out.println(+c);
       
    }   
}




class Method //here you can take any class name
 {
    public static void main(String[] args)
    {
        A obj=new A();  //CREATING OBJECT FOR CLASS A
       
        obj.cal(12,2);  //ACCESSING METHOD
    }
}
Here in above program we make to classes method
Is our main class for our main method
And class A is our second class
In which we take method cal
There is necessary to create the object of the class A
In our main class( here class method)
That’s why we create object of the class A in main class
        A obj=new A(); 
A is our class name
Obj is user defined you can take
Any name or word here

In class A we declare method
  void cal(int x,int y)
for accessing this method from main class
obj.cal(your parameter )
is require to declare in main class

Use of ++ and – in java

Use of ++ and – in java

++ is increment operator for increment by 1
And – is decrement operator for decrement by 1


Types of ++
There are two types of ++
Post increment :  a++ here value of a is print first then increment is done
 
Example
Class postincrement
{
public static void main( string[] args)
{
Int a=10;
a++;
System.out.println(+a);
}
}       
In above example we get output 10
If we take ++a( pre increment ) instead of a++ then we get output 11

Types of --


There are two types of --
Post decrement :  a-- here value of a is print first then decrement is done

 
Example
Class postdecrement
{
public static void main( string[] args)
{
Int a=10;
a--;
System.out.println(+a);
}
}       
In above example we get output 10
If we take --a( pre decrement ) instead of a-- then we get output 9