Sunday, 7 February 2016

Method Overloading

By
  • Two or more methods in a class can have same name if their arguments list is different
  • Arguments could differ in
    • Number of arguments
    • Data type of arguments
    • Sequence of arguments
  • This feature is know as Method Overloading and also called static polymorphism
  • Binding of method call to its definition happen at compile time.
  • Static polymorphism is also called compile time polymorphism
  • Parameterized constructor have parameters which is passed by programmer while object creation 
  • Just like method we can overload constructors
  • Constructor can be invoked when object is created,either the parameterized or default constructor is invoked
  • this() can also be used to invoke constructor.
          Binding refers to the association if method call to it definition

class DisplayName{
 public void printFirstName(String s){
 for(int counter=0;counter<1;counter++){
  System.out.print(s);
 }
}
public void printFirstName(String s, int no){
 for(int counter=0;counter<no;counter++){
  System.out.print(s);
 }
}
public void printFirstName(String firstName,String lastName){
 System.out.println(firstName  + lastName);
 }
}
class Demo{
  public static void main(String args[]){
  DisplayName obj=new DisplayName();
  obj.printFistName(“Name:”);
  System.out.println(" ");
  obj.printFistName('jack',2);
 }
}

Output : Name:jackjack
             
Consider the Methods
                   printFirstName(String s);
                   printFirstName(String s,int no);
                   printFirstName(String firstName,String lastName);


These methods have same name but differ is only in arguments

Constructor Overloading
 Lets see some concept of parameterized constructors
 class Customer{
   int id;
   String name;
   public Customer(int id,String name){
    this.id = id;
    this.name = name;
   }
   public int getId(){
    return id;
    }        
  }
 class Retail{
   public static void main(String args[]){
   Customer custObj= new Customer();
   System.out.println("Customer id:"+custObj.getId());
  }



Output: Compile time error
Note: Whenever class is provided with parameterized constructor default constructor is not provided to it

 class Customer{
   int id;
   String name;
   public Customer(){
     System.out.println("Default Constructor called");
      id = 1;
      name = "XXXX";
     }
   public Customer(int id,String name){
     System.out.println("Parameterized Constructor called");
     this.id = id;
     this.name = name;
     }
   public int getId(){
        return id;
     }        
   public String getName(){
        return id;
    }
class Retail{   
   public static void main(String args[]){ 
   Customer custObj= new  Customer(); // line 1                         System.out.println("Customer     Id:"+custObj.getId());
   Customer custObj= new Customer(2,"Jack");
   System.out.println(" CustomerId:"+custObj.getId());       
   System.out.println("Customer     name:"+custObj.getName());
 }
Output: Default Constructor called
        Customer Id: 1
        Parameterized Constructor called
        Customer Id: 1
        Customer name: Jack

this() concept 

class Customer{
    int id;
    String name;
    public Customer(){
     System.out.println("Default Constructor called");
     id = 1;
     name = "XXXX";
    }
    public Customer(int id,String name){
      System.out.println("Parameterized Constructor called");
      this.name = name;
    }
    public int getId(){
        return id;
     }        
    public String getName(){
        return id;
  }
  class Retail{
        public static void main(String args[]){
        Customer custObj= new Customer(2,"Jack");
        System.out.println(" Customer Id:"+custObj.getId());       
        System.out.println("Customer name:"+custObj.getName());
 }

Output: Parameterized Constructor called
              Customer Id: 0
              Customer name: Jack
Why CustomerId is 0 as we haven't invoked default constructor here the concept of this() comes into play

class Customer{
  int id;
  String name;
  public Customer(){
  System.out.println("Default Constructor called");
  id = 1;
  name = "XXXX";
  }
  public Customer(int id,String name){
  System.out.println("Parameterized Constructor called");
  this(); // line 1
  this.name = name;
  }
  public int getId(){
   return id;
  }        
  public String getName(){
   return id;
  }
  class Retail{        
   public static void main(String args[]){
   Customer custObj= new Customer(2,"Jack");
   System.out.println(" Customer id:"+custObj.getId());                 System.out.println("Customer name:"+custObj.getName());
}
Note: In line 1 how this() is used 

Output: Compilation error why?
because this() should always be first statement of the calling constructor

Thanks for reading my post hope it helped you..
               

0 comments :

Post a Comment