Friday, February 1, 2013

FUCKING GAME OF METHOD OVERLOADING AND OVERRIDING


This ll demonstrate the concepts of static binding and dynamic binding , role of access modifiers in the overloading and overriding. Static Binding vs Dynamic binding Java Here are few important difference between static and dynamic binding in Java written in point format. knowledge of static and dynamic binding is require to understand Java code and find out any bugs and issue while running Java program. It also helps in troubleshooting and debugging in Java.
 1) Static binding in Java occurs during Compile time while Dynamic binding occurs during Runtime.

 2) private, final and static methods and variables uses static binding and bonded by compiler while virtual methods are bonded during runtime based upon runtime object.

 3) Static binding uses Type(Class in Java) information for binding while Dynamic binding uses Object to resolve binding.

4) Overloaded methods are bonded using static binding while overridden methods are bonded during run time. here is an example which will help you to understand both static and dynamic binding in Java.

5) Dynamic binding is slower than static binding because it occurs in runtime and spends some time to find out actual method to be called.

Here is project Structure




package com.source;
//Demo class that has all the overloaded and overriding methods 

package com.Java.one;

public class DemoClass {
  
    // overloaded constuctor
    public DemoClass(String one) {

     System.out.println("One argument constructor is called");
    }


    private DemoClass(String one, String two) {

     System.out.println("Two argument constructor is called");
    }

    protected DemoClass( int a,int b,int c) {

     System.out.println("Three argument constructor is called");
    
}

     DemoClass(int a,int b,int c,int d) {

      System.out.println("Four argument constructor is called");
    }

  //   constructor can't be static or final or static final
    /*public static  final DemoClass(float a){
     constructor can't be static or final or static final
    }
    */
    
    
     // ====================functions that has to be overridden
     public void overridePublicMethod(){

      System.out.println("Public method can be overridden");
     }
    
     private void overridePrivateMethod(){

      System.out.println("Private method can't be overridden");
     }
    

     protected void overrideProtectedMethod(){

      System.out.println("Proctected method can be overridden");
     }

    
      void overrideDefaultMethod(){

      System.out.println("default method can be overridden");
     }

    
     static void overrideStaticMethod(){

      System.out.println("Static  method can't be overridden");
     }
    

     public  final void overrideFinalMethod(){

      System.out.println("Final method can.t be overridden");
     }

}


=================END OF CLASS================================


// Class that extends the DemoClass

package com.Java.one;

public class ChildClassInSamePackage extends DemoClass{

    ChildClassInSamePackage(int a, int b, int c, int d) {
     super(a, b, c, d);
   // you have to call Base class constructor before you call child class one
    }

    protected ChildClassInSamePackage(int a, int b, int c) {
     super(a,b,c);
     System.out.println("Constructor of Child class is called");
     
    
    }

    public ChildClassInSamePackage(String one) {
     super(one);
    }


    // Lets see how and what methods of Base class can be overriden
    
    @Override
    public void overridePublicMethod() {
     super.overridePublicMethod();
     // this ll call the super class method
    }
    
@Override
void overrideDefaultMethod() {
    System.out.println("This is overriden method of Base class " +
      "and we are changing the Defination of " +
      "this mathod in the child class");
}
    



    @Override
    protected void overrideProtectedMethod() {
     System.out.println("This is protected overriden method of Base class " +
      "and we are changing the Defination of " +
     "this mathod in the child class");
    }
    
}

============================END OF CLASS=============================

// Class that demonstrate static Binding
package com.Java.one;

import java.util.Collection;
import java.util.HashSet;

public class StaticBinding {
   
 //overloaded method takes Collection argument
 public Collection sort(Collection c){

     System.out.println("Inside Collection sort method");
     return c;
 }
 
   
   //another overloaded method which takes HashSet argument which is sub class

 public Collection sort(HashSet hs){

     System.out.println("Inside HashSet sort method");
     return hs;
 }
    
 }

============================END OF CLASS=============================



// Class Demonstrate Dynamic Binding

package com.Java.one;

class Vehicle {

 public void start() {

     System.out.println("Inside start method of Vehicle");
 }
}

class Car extends Vehicle {

 @Override
 public void start() {
     System.out.println("Inside start method of Car");
 }
}


===============================END OF CLASS==========================

//  Class which is in other Class which extends DemoClass
package com.Java.two;
import com.Java.one.DemoClass;

public class ChildClassInOtherPackage extends DemoClass{
    public ChildClassInOtherPackage(String one) {
     super(one);  
    }
   
    @Override
    public void overrideProtectedMethod() {

     super.overrideProtectedMethod();
  System.out.println("Overriding...This method has to be public for making it " +
    "accessible from other package 
         although it's overriding a protected.");
    }
    

    @Override
    public void overridePublicMethod() {
     super.overridePublicMethod();
     //ok
    }
}

==========================END OF CLASS===============================

// MAIN CLASS

package com.Java.one;
import java.util.Collection;
import java.util.HashSet;

import com.Java.two.ChildClassInOtherPackage;

public class EntryPointClass {
    public static void main(String args[]){

System.out.println("==========DemoClass==============");

     DemoClass demoOneArg = new DemoClass("hi");
     // Methods that can be called using Base class
     
     demoOneArg.overrideDefaultMethod();
     demoOneArg.overrideFinalMethod();
     demoOneArg.overrideProtectedMethod();
     demoOneArg.overridePublicMethod();
     DemoClass.overrideStaticMethod();
     
     System.out.println("========ChildClassInSamePackage=======");

     ChildClassInSamePackage chClass = new ChildClassInSamePackage(1, 2, 3) ;
     chClass.overrideDefaultMethod();
     chClass.overrideFinalMethod();
     chClass.overrideProtectedMethod();
     chClass.overridePublicMethod();
     ChildClassInSamePackage.overrideStaticMethod();
     
  System.out.println("========ChildClassInOthPerackage=========");
     ChildClassInOtherPackage chOtherPak = new ChildClassInOtherPackage("Hello");
     
     chOtherPak.overrideFinalMethod();
     chOtherPak.overrideProtectedMethod(); // This method has to be public
     chOtherPak.overridePublicMethod();
//ChildClassInOtherPackage.overrideStaticMethdo(); //not allowed if in 
                                                   other package
     
     System.out.println("===StaticBinding===");
     
     /* we have overloaded sort() method, one of which accept Collection and
      * other accept HashSet. we have called sort() method with HashSet as
      * object but referenced with type Collection and when we run method with
      * collection as argument type gets called because it was bonded on compile
      * time based on type of variable (Static binding)  which was collection.
*/
         Collection c = new HashSet();
         StaticBinding et = new StaticBinding();
         et.sort(c);    
    System.out.println("================DynaimicBinding========================");  

         /*Car extends Vehicle and overrides its start() method and when we call start()
          * method from a reference variable of type Vehicle, it doesn't call start()
          * method from Vehicle class instead it calls start() method from Car
          * subclass because object referenced by Vehicle type is a Car object.*/
         
          Vehicle vehicle = new Car(); //here Type is vehicle but object will be Car
          vehicle.start();        //Car's start called because start() 
                                                is overridden method
  
           }
}

OUTPUT :

==================DemoClass==========================
One argument constructor is called
default method can be overridden
Final method can.t be overridden
Proctected method can be overridden
Public method can be overridden
Static  method can't be overridden

================ChildClassInSamePackage===============
Three argument constructor is called
Constructor of Child class is called
This is overriden method of Base class and we are changing 
the Defination of this mathod in the child class
Final method can.t be overridden

This is protected overriden method of Base class and 
we are changing the Defination of this mathod in the child class
Public method can be overridden

Static  method can't be overridden

================ChildClassInOtherPackage===================
One argument constructor is called
Final method can.t be overridden
Proctected method can be overridden

Overriding...This method has to be public for making it 
accessible from other package although it's overriding a protected.

Public method can be overridden

================StaticBinding==============================
Inside Collection sort method

================DynaimicBinding============================
Inside start method of Car


=========================END OF CONCEPT=====================

No comments:

Post a Comment