Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Akash Agrawal
core java
2 Answer(s)      2 years and 2 months ago
Posted in : Java Beginners


Hi,

can any one exain me the concept of static and dynamic loading in core java??


Advertisement
View Answers

October 4, 2012 at 2:25 PM


    /* program for to display the product details by using jdbc.
       For example take the product table with some columns like 

            product
                  pid
                  pname
                  price

       database : Oracle
       username : chavan
       password: chavan

    */

    import java.sql.DriverManager;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    public class ProductDetails {

        static Connection con;
        static PreparedStatement ps;
        static ResultSet rs;

         public static void main(String args[]) {
              try{
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
                  "chavan","chavan");
                    ps=con.prepareStatement("select * from product");
            rs=ps.executeQuery();
                    while(rs.next()) {
                System.out.println("pid="+rs.getInt("pid")+"   pname="+
       rs.getString("pname")+"  price="+rs.getDouble("price"));

            }
               }
               catch(ClassNotFoundException cnfException) {
            cnfException.printStackTrace();
           }
           catch(SQLException sqlException) {
            sqlException.printStacKTrace();
           }

               finally {

            try{
                if(rs!=null)
                                 rs.close();
                if(ps!=null)
                                 ps.close();
                    if(con!=null)
                                 con.close();
                    }
            catch(SQLException sqlException) {
                sqlException.printStackTrace();
            }
           }       
         }


    }//end to class

    Note : before run the program set the class path to 
     ojdbc14.jar file


January 3, 2013 at 11:46 AM


Hi Akash,

static loading done by compiler at compile time. But dynamic loading done by jvm at run time. Method overloading and method overhiding are examples for static loading. Method overriding is example for dynamic loading.

overloading:


two or more methods contain same method name and different number of parameters or order of parameter or type of parameters.

ex: method1(int a,float b) {} method1(float b,int a) {}

In this example the method name is same and both take two parameters one is int and anoter one is float. But hear the oreder is different.

ex: class Sample { void method1(byte... b) { System.out.println(" byte variable argument"); }

      public static void main(String[] args) {
         Sample s=new Sample();
         byte a=2;
         s.method1(a);  //  observe this line carefully
      }
   }

  o/p : byte variable argument


 program description:  The static loading(overloading &over hiding) done by the compiler  "" based on reference variable"".  
       s.method1(a). In this the complier perform the static loading based on "s". this s variable belong to Sample class type. In this the compiler check any metod1 is there or not in the Sample class . If it is there, it check the argument of the method1 is it byte or not. If it is byte the compiler decides for execution.But if it is not their it perform the "winding operation ".

" it conver the byte into short and it check for method1(short s). If it is their it decide that method for execution. If it aslo not there it conver the short into int

and it check for method1(int) and repeat the operation unitl

byte->short->int-> long ->float-> double.

method1(double). If this method also not there in Sample it perform the "Boxing "

In boxing the compiler convert the byte vale which is passed in method1(byte) into Byte class object and search a method1(Byte). If it is their it decide the method1(Byte) for execution.If it is not their it check the method1 which take the argument as Byte super class like method1(Object) .If it is their it decide the method1(Object) for execution. If it is also not their the compiler search a method which take as variable argumet line

method1(byte... b) or method1(Byte... b) or method1(short...)or .... method1(double... ). If it is their the compiler decide the method for execution. This method also not theri it show compile thime error like "can not find symble" .

rules: according to the overloading 1.the compiler expect only one metho which is matched with given arguments. 2.the compiler give the first preority for winding and it give nexe preority for boxing. 3.the compiler give the last preority for variable argument. 4. the compiler perform the static loading based on reverence variable( example s). 5. the overloading for instance and static methods but not a constructors.

over hiding:


method over hiding only for static methods, not for instance methods. Whenever we declare the static method in side a class , that static methods only for that class. It it not possible to access those static method by another class like subclasses and any another classes.

ex: class Parent { public static void method1() { System.out.pritnln("Parent static method"); } }

 class Child extends Parent {
    public static void method1() {
     System.out.println("Child static method");
    }

    public static void main(String arg[]) {

      Parent p=new Parent();
      p.method1(); // Parent static method

      Child c=new Child();
      c.method1(); // child static method

      Parent p1=new Child();
      p1.method1(); // Parent static method
    }
 }


This method overhinding performed by the compiler based on the reference variable. In p.method1() we are using p which is belong to Parent type hence the compiler decide the method1() in Parent type. In c.method1() we are using c which is belong to Child type hence the compiler decide the method1() in child class. In p1.method1() hear also p1 is type of Parent hence the compiler decide the method1() in Parent class for execution.

To looking this is same like method overriding but it is not overriding. Method overriding only for instance method not for static methods. But over hiding only for static methods. In the method1() in Parent and Chid both are different.

rules: 1. it is only for static members 2. it is performed by the compiler at compile time based on refernce variable. 3. it is not for constructors and instance methods.

overriding:


overriding is performed by the jvm at runtime based on object reference (address or hashcode). 

" a method in super and sub classes contain same method signature( it mean method and parameters) and same return type or parent method covarient return type (for example if parent method return type is Object then it child method return type is Object or Object subclasses)."

ex: class Parent { Object method1(Integer a) { System.out.println("in parent a="+a); return a; } }

  class Child extends Parent {

    public Integer method1(Integer a) { // covarient return type
          System.out.println("in Child a="+a);
          return a;
    }

    public static void main(String args[]) {
       Parent p=new Child();
       p.method1(5);
    }
  }


 p.method1(Integer). In compile the the compiler take the p Which is Parent type. Hence it check any method1(Integer) their or not. In Parent it is their hence their is no problem

But in run time the jvm the reference(hashcode) of new Chile(). and at first it check is there any method1(Integer)in Child ,if it is their the jvm execute that method. In Child there is no any method1(Integer) the jvm search the method1(Integer) in Parent. If it is there it execute the method1(Integer). Hence method overriding performed by the jvm based on the reference of the object.

rules : 1. jvm perform the overriding based on reference of the object 2. jvm search any method at first in child and next in it's parent. 3. according to the overriding the the child class method return type is same as parent class method return type or it sub classes. 4. it is not possible to provide the weeker access specier for child class method

  in the above example the paent class method contain default access specier. Hence the sub class method access specier may be deafult or it higher access specier.

private < deafult < protected < public

    5. it is not posiible to incress the size or level of checked exceptions to the child class method.

  ex: 
    parent :    void method1() throws IOExcepton  size=1

    child  :    void method1()  throws IOExcepton size=1


   parent :    void method1() throws IOExcepton  size=1

    child  :    void method1()  throws IOExcepton,FileNotFoundException size=2 hence compile time error

  parent :    void method1() throws IOExcepton  

  child  :    void method1()  throws Excepton  compile time error because hear we are increse level it mean Exception is super class of IOException



 6. it is not possible to override the private, static ,final mehods and constructors.
 7.the method in super class is called overriden method and the method in supclass is called overriding method.
 8. it is done by the jvm at runtime based on reference.


Related Tutorials/Questions & Answers:
core java
core java  how to display characters stored in array in core java
core java
core java  basic java interview question
Advertisements
CORE JAVA
CORE JAVA  CORE JAVA PPT NEED WITH SOURCE CODE EXPLANATION CAN U ??   Core Java Tutorials
core java
core java  i need core java material   Hello Friend, Please visit the following link: Core Java Thanks
core java
core java  Hi, can any one expain me serialization,Deseralization and exterenalization in core java
core java
core java  Hi, can any one exain me the concept of static and dynamic loading in core java
Core JAva
Core JAva  how to swap 2 variables without temp in java
core java
core java  how can we justify java technology is robust
CORE JAVA
CORE JAVA  What is Garbage collection in java? What is the role of a developer for garbage collection
core java
core java  its compulsory to save file name and class name is same in java
Core Java
Core Java  How to execute cmd command through java?? Give Code of them
core java
core java  write a java program to view product details from product table
Core Java
Core Java  Hi, Can any one please share the code for Binary search in java without using builtin function
Core Java
Core Java  Please write a Java Program to design login form and store the values in file & validate and display the MainForm
core java
core java  In java primitive variables will get its default value automatically after declaration. Then why it is mandatory to initialize a variable before using
core java
core java  Hello sir,What is logic behinde the core java programms,How may programmas are there,for example,sorting of two numbers,grade of the student details,fibonice serice,paldroma,incremting of the program,asscedding
core java
core java  Hello sir,What is logic behinde the core java programms,How may programmas are there,for example,sorting of two numbers,grade of the student details,fibonice serice,paldroma,incremting of the program,asscedding
Core Java
Core Java  Hi, Can any one please expain me why derived data types are required in java as we have primitive data types with us
Core Java
Core Java  Hi, Can any one please expain me why derived data types are required in java as we have primitive data types with us
Core Java
Core Java  How to load class dynamically in java ?   To load class dynamically you can use Class class method Class.forName("abc.xyz.MyClass"); This method load the given class at run time
Core Java
Core Java  Hi, can any one please tell me the uses of return type,"Super" and "this" calling statement in Java?? why do we required this,super calling statement?? why return type is required
Core Java
Core Java  what is a class
Core Java
Core Java   How can i write own compile time and runtime exceptions in java   Hello Friend, Please visit the following links: http://www.roseindia.net/java/exceptions/how-to-throw-exceptions.shtml http
Core Java
Core Java  Is Java supports Multiple Inheritance? Then How ?   Hi Friend, Java does not support multiple inheritance but it can be achieved by using the interface. In Java, Multiple Inheritance can be achieved through
Core java
Core java  How to use hyperlink that is href tag in core java without swing, frames etc. My code is StringBuffer oBodyStringBuffer = new StringBuffer("Message Classification: Restricted.\n\n
CORE JAVA
CORE JAVA  What is called Aggregation and Composition
core java
core java  surch the word in the given file
Core Java
Core Java   Hi, Can any one please tell me the program to print the below matrix in a spiral order. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Thanks a lat in advance
core java
core java  I am having 10 countries in the data base as back end.in front end i am having a button in web page like SHOWCOUNTRIESLIST so i need java code how to retrive that 10 countries from back end to my page,,pls help me
core java - Java Beginners
core java  i want to get java projects in core java
core java
core java  how to write or update into excel file using...; Please visit the following links: http://www.roseindia.net/tutorial/java/poi/insertIntoExcelFileData.html http://www.roseindia.net/answers/viewqa/Java
 

 

 

DMCA.com