Gadgets

Monday, 25 February 2013

Constrauctor with example


Constructor:-
Constructor is a specialized method. Constructor name should be equals to class name.
 A constructor is similar to a method but Constructors doesn’t have return type (void, int,…).
A constructor does not return any value. A constructor is called & executed at the time of creating the object automatically. 
They two type of constructors
     1.       Default constructor:-
Class Student
{
Student()         //default constructor
               {
                     }
}
        2.       Parameterized constructor:-
Class Student
{
Student(par1,par2…)         //parameterized  constructor
               {
                     }
}

Example Program:-

public class Constructor_Example {
   
    public static void main(String args[])
    {
          Students ce= new Students();
          Students ce1= new Students(1214,"narendar");
         
    }
   
}
class Students{

    String collegename="VKITS";            // dynamic initialization
   
    Students(){            //default constructor
 
    System.out.println("default constructor");

}
    Students(int hno,String name)   //parameterized constructor
    {
        this.collegename=collegename;
       System.out.println("Student details : "+hno);
       System.out.println(name);
       System.out.println(collegename);
     }
}
Constructor overloading:-
Writing two or more constructors with same name with difference parameter is called constructor overloading.

Abstract Class and Method with Example


Abstract Methods:-
If methods has no body that methods called abstract methods.
Abstract methods create by using abstract keyword. We should be declare abstract methods in one class (super class) and implement in other class (sub class).
 abstract returntype abstractmethodname(par1,par2…..); 
Abstract Class:-
Ø  An abstract class is creating by using abstract keyword.
Ø  An abstract class contains instance variables & abstract methods and normal methods.
Ø  It is not possible to create objects to abstract class. But we can create a reference variable of abstract class.
Ø  All the abstract methods of the abstract class should be implemented in its sub classes.  If any method is not implemented, then that sub class should be declared as ‘abstract’.
Syntax:-
abstract class abstractclassname
{
// instance variables.
// normal methods and abstract methods.
}
Example Program :-
public class Abstract_Example {
    public void main(String arg[])
    {
       
    }
                
}
abstract class Animal{
    int legs,eyes;
    String name;
   
    abstract void Tiger();
    void Loin(){
        legs=4;
        eyes=2;
        name="lion";
        System.out.println("normal method details :legs "+legs+"eyes "+eyes+"name "+name);
    }
}
class Ani extends Animal{


    void Tiger() {
        legs=4;
        eyes=2;
        name="tiger";
        System.out.println("abstract method details :legs "+legs+"eyes "+eyes+"name "+name);
    }
   
}

Saturday, 23 February 2013

Inner class in java with example


Inner class: -
Inner class is a class written in another class. It is a security mechanism. We can’t touch the inner class directly. Only authorization can use it. We must use private to inner class. We can’t create object directly to inner class.
Example program: -
public class BankAccount {
public static void main(String a[])
{
 Withdraw w=new Withdraw(); 
  }
}
class Bank
{
    private class Account   //inner class
    {
        int bal=0;
        Account(int bal)
        {
            this.bal=bal;
        }
    }
public void withdraw()
{
    Account a1=new Account(5000);
    int amt=200;
    a1.bal=a1.bal-amt;
    System.out.println("withdraw ammount  : "+amt );
    System.out.println("available bal : "+a1.bal);
}
}
class Withdraw
{
    Withdraw()
    {
        Bank b=new Bank();
        b.withdraw();
    }
}