Wednesday 20 November 2013

Session 9:- Overloading In Java

        Overloading

  •  In a class, we have more than one method, with the same name but change in the parameter or signature is called as method overloading.
  • Change in the signature means there has to be change, 
  • In the number of parameter. 
  • Change in the data type of the parameter. 
  • There has to change in the sequence of the parameter. 
  • In case of overloading return type is not considered and the Access Specifier also not consider.  
  • Same rule is applicable even for constructor overloading.
Example 13:- Method Overloading

public class Calculator
{
public void addition(int a, int b)
{
int sum=a+b;
System.out.println ("Ans is:-"+sum);
}
public void addition(int a, int b, double c)
{
double sum=a+b+c;
System.out.println ("Ans :"+sum);
}
public void addition(int a, double c, int b)
{
double sum=a+b+c;
System.out.println("Ans :"+sum);
}
public double addition(double c ,int b, int a)
{
double sum=a+b+c;
return sum;
}
}
public class Test{

public static void main(String args[])
{
Calculator c= new Calculator();
 c. addition(12,20);
c. addition(4,10,7.9);
//Overloaded Methods
c. addition(1,3.5,34);
double sum= c. addition(0.3,25,30);
System.out.println ("Ans:"+sum);
}
}

Advantage:-
  • We can create only one object at time and call any number of methods by using the object reference variable.
Example 14:- Constructor Overloading

public class Student {
String name;
int age;
String email;
public Student()
{
System.out.println("default constructor");
}
public Student(String name)
{
this.name=name;
// local variable and instance variable name same
System.out.println ("one parameter constructor”);
System.out.println ("Name:-"+this.name);
}

public Student(String name, int age)
{
this.name=name;
this.age=age;
System.out.println ("two parameter constructor”);
System.out.println ("Name:-"+this.name);
System.out.println ("Age:-"+this.age);
}
public Student(String name, String email, int age)
{
this.name=name;
this.age=age;
this.email=email;
System.out.println("Three parameter constructor");
System.out.println ("Name:-"+this.name);
System.out.println ("Age:-"+this.age);
System.out.println ("Email:-"+this.email);
}
}
public class Test{
public static void main(String args[]) {
Student s= new Student();
Student s1= new Student("JNext");
Student s2= new Student("JNext",23);
Student s3= new Student("JNext","info@JNext.co.in",23);
}
}
OUTPUT:
default constructor
one parameter constructor
Name:-JNext
two parameter constructor
Name:-JNext
Age:-23
Three parameter constructor
Name:-JNext
Age:-23
Email:-info@JNext.co.in





Wednesday 6 November 2013

Session 8:- Constructor

      Constructor
  • Constructor is one which is made for constructing or crating object and to initialize the  properties (States) when we create an object.
  •  Constructor name is must be same as class name.
  •  Constructor is not made for any returning the value .It can't return so constructor has not any  return type.
  • The constructor must be present is same appropriate class.
  •  When we create a class than automatically constructor created, that is called default constructor.
  • The default constructor will be public and zero parametrized.
  •  In any class we can write our own constructor which can be parameter or zero parametrized and which is called as user defined or custom constructor.
  •  When we write our own constructor than default constructor will be automatically destroyed.
  • The purpose of our own constructor is to initialize properties or states of object at the time of object creation.

Example :-Constructor
            public class Book{
               String title;
               int price;
               public Book()   
                   {   
              // Default Constructor, it is not visible !!!.
              // Instance variable Assign default value
                     }
          public Book(String name , int p)
                {

               // custom Constructor
                   title=name;
                    price=p;
              }
                         }
          public class Run{
          public static void main(String args[]){
            Book b=new Book();
            Book b1=new Book(“inside java”,100000);
                }
               }    
          NOTE
  • The local variable & instance variable name can be same at that time local scope variable dominate over the instance variable we can use “thiskeyword to differentiate local and instance variable.
  •  When we use “this” with a variable than it’s refer to instance variable.
  • Constructor can return the value:-
  1. java perspective constructor can't Return the value but JVM perspective constructor can return the value because the object create by the jvm and that time constructor call and you store the object in some reference variable so constructor can return the value and that value is reference or address of created object.
Example :-

     public class Car     
    {       
     String name;   
     long  price;   
     public Car(String name, int price) 
          {                    
         this.name=name;
     // instance variable = local variable                             this.price=price;         
          System.out.println (this.price);

          System.out.println (this.name);

         }     
       } 
    public class Run{   
    Public static void main (String args[]) 
    {     
    Car c=new Car(“Audi”,10000000);
          }
        }
OUTPUT:-
              Audi
              10000000

Example :-

public class BabyGirl {
     String clr;
     String hclr;
     long price;
     String eyeclr;
    public BabyGirl()
    {    
    clr="white";
    hclr="black";
    eyeclr="brown";
    System.out.println (“Color:-”+clr);
    System.out.println (“Hair color:-”+hclr);
    System.out.println (“Eye color:”+eyeclr); 
     }
      public class Run {
 public static void main(String[] args) {
    new BabyGirl();
     }
           } 
        OUTPUT:-
Color:-white
Hair color:-black
Eye color:-brown

Friday 25 October 2013

Session 7:- Package In java.

Introduction about packages:-

  •  Package is nothing but it is folder.
  •  In java we use packages for better Maintains of the application, project search easily.
  • Every package can have direct classes or it can have again package (sub packages).
  • To access any member (instance variable, instance method etc) outside the package but the member should be public.
  • Within package we no need to import class of the package. 
  •  * will import all the class which is directly under the package (not a sub package).  
  • We can’t import more than same java file having the different package. 
  • All 3000+ inbuilt java class. Modularizes many packages.
    • Ex. - java.lang.*;
    •  -java.sql.*;
    •   - java.io.*;
    • - java.util.*;
    • - java.awt.*;
  • All the class which is under java.lang package will be available implicitly and we don’t have to import. 
  •  Package declaration must be first executable statement.  
  • De-referred object which are present in heap will be collected by Garbage Collector and re-uses memory. 
  • When object in which is present in heap can have multiple reference. 
  • But single reference can refer or point to only one object at same time or given time.  
  • When an instance is of primitive Data type that is called as HAS-A Relationship.   
  • When an instance of variable is non-primitive data type that is called as Association Relationship. 


Example 9:- Package

public class Person{
            // instance variable
        int age=25; //HAS-Relationship
String name="JNext Training & Development !!!!!! SUPER"; // Association Relationship
Mobile m; //Association Relationship
// some Method
 }
public class Mobile
{
    double price; //HAS-Relationship
    String company_name; // Association Relationship
    // some method }

Thursday 24 October 2013

Session 6:- Object & Class

         Object:-

  • Object is real world entity which has States & Behavior.
Ex: Pen,Mobile,Person,Book,Car,Tv,College,Computer,fan,Bus,Company..etc
  •  States generally represent property of object.
  •  Behavior represents an action or functionality.
  •  When we Create an object, we required
    • Plan or master copy, it's called Class.
    •  Plan or Class contains states and behavior which will share with another object.
  • Any number of objects created in class.
  •  The entire object works independently. 
  • When object is modified or destroyed it may not affect in another object.
  •  Consider this women is one object so  It has states and behavior.

    (States)    (Behavior)
    -lags       -playing    
    -cap        -walk        
    -hands     -talk    
    -stick      -stand  
    -eyes       -etc   
    -etc
    • States of class can be called as instance variable.
    • Behavior of class can be called as instance method.
    • In general is called as instance member of class because these members are shared with multiple instances.
    • Sometime the instance variable will be initializing to a value or data and then variable value is same in the all instance.
    • If We don’t initialize instance variable than the instance variable value are different for different object.but when we create the object at that time its take default value for that variable.Default value is depend on the data type if instance variable data type int,byte,short,long than its take 0. for double and flout 0.0.
    • for boolean false
    • for String null
    •  In java the object created in heap memory.
    •  The allocation memory will be taken care by JVM.
    •  JVM is allocation new memory when we use keyword new. 

    Syntax:- Object

    Class-name variable name= new Class-name();
      Ex: Fan f =new Fan ();
      •  Left side key word new Fan(); is the object of the Fan class.
      • f is the reference variable.
      •  Fan is data type of the object.
    •   To access multiple member of an object we required reference variable.
    •   Reference variable data type must be same as object type.
                
                         Tree t = new Tree ();        /*compare these two statement
                          int   i = 20;                       Tree and int is data type t and i variable and  
                                                                    After = sign is value.*/
    Syntax:- Class

                class class-name
                {
                    // instance variable (state)
                    // instance method (behavior)
                }
    NOTE:- Class name Start with capital latter(industry standard).

    Ex: class Student      //
    {
       // instance variable 
       String name;
       int rollno;
     //  instance method
    public void  reading()
       {
            System.out.println("Reading");
        }                                   

    •   Class can also be treated as a custom data type.
    •   Reference variable always categories of non-primitive variable.
    •   Any number of reference variable can be refer a single object.
    Ex:
                       L.H.S              R.H.S
                 Fan     f       =    new        Fan();


    •  new Fan(); is object of class Fan.
    •   new keyword is use for create the object of class and initialize heap memory. 
    •  f is reference variable.(non-primitive variable).
    •   Fan is data type of object. Data type is same as the object name.
    •  When a new object is created that object will be shared with the entire instance variable and these instance variables are initialize to default value.
    •  Default values of an instance variable depend on data type of the variable.
    •  Non-primitive instance variable are initialize to NULL.
    •   Primitive instance variable are initialize to appropriate respective value.
    •   Calling a method by passing a primitive value is called as call by value. 
    •   Calling a method by passing an object reference or reference variable is called call by reference.  
  •  Example  :- Call by value
    public class Calculator{
    public void  addNo(int n1,int n2)
    {    
    int n3;
     n3=n1+n2;
    System.out.println (n3);
    }
                  }
    public class Math{
    public static void main(String args[])
    {
    // 1st  way to run this program
    new Calculator().addNo(20,30);
    /* 2nd  way to call this method
    Calculator c=new Calculator ();
    c.addNo(10, 20);
     
  • Example  :- Call by Reference 

    public class Calculator {
    int a=12;
    nt b=34;
    public void  addNo(Calculator c)
    {
     int n3;
     n3=c.a+c.b;
    System.out.println (n3);
    }               
                  }
    public class Math{
    public static void main(String args[])
    {
    Calculator c=new Calculator ();
    c. addNo(c);

    /* when we use new keyword then object is created in heap memory and memory will be initialize. Here we passing the Calculator class object or reference so it called call by reference */

    }