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