Monday, August 13, 2012

Core Reusability

Code Reusability
Code reusability means reusing the existing classes.
In java code reusability is achieved by using two concepts:
1)      Composition
2)      Inheritance
Composition:
Composition means creating object of existing class as data member of new class.
it is a has-a relationship.
example: Student has a class room, library has books and Student has a library.
Composition is achevied by in 2 techniques:
1. Association:
Here association is defined as a Sudent connot exist without ClassRoom, means you cannot create a student object without creating ClassRoom class Object.
class Student{
ClassRoom croom; //here croom object must created.
}
we seen one-to-one, one-to-many,many-many,many-one are the associations relationships in java.
2. Aggregation:
Aggregation is also has-a relationship, but here the relationship is organized is loosely.
example: the relationship b/w the Library and Student. Here a Library Object is created with out creating the Student object.
class Library{
Student stu;// it is not mandatory to exist stu objecgt.
}

//existing class defined by user1
Point.java
Package pack1;
public class Point{
public int x;
public int y;
}
Javac –d . Point.java
Note:move Point.java to pack1.
//new class defined by user2
Package pack2;
Import pack1.Point;
public  class Circle {
public int radius;
public Point center=new Point();
}
Javac  -d . Circle.java
Note: move Circle.java to pack2;
Demo.java
public class Demo {
public static void main(String[] args) {
Circle c=new Circle();
c.radius=3;
c.center.x=1;
c.center.y=2;
System.out.println(c.radious);
System.out.println(c.center.x);
System.out.println(c.center.y);
}
}

Tuesday, July 24, 2012

creating database table by using jdbc


//Table creation
import java.sql.*;
import java.io.*;

public class JdbcDemo
{
            public static void main(String args[]) throws Exception
            {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//Type1
            Connection con=DriverManager.getConnection("jdbc:odbc:das","scott","tiger");
            Statement st=con.createStatement();
            int i=st.executeUpdate("create table employee4(eno number(10),ename varchar2(50), esal
             number(10))");
            System.out.println(i);
                        if(i==-1)
                        {
                                    System.out.println("Table created");
                        }
                        else
                        {
                                    System.out.println("Table not created.");
                        }
                        st.close();
                        con.close();
            }
}

Saturday, July 21, 2012

Why java is suitable for internet?

Java is suitable for internet because of two main reasons.

  1. It is system independent and hence its programs can run on any type of computer available on  internet.
  2. It eliminates a lot of security problems for data on internet.

What is the difference between an executable(.exe) file and .CLASS file?


The .exe file contains machine language instructions for the microprocessor and is system dependent. .CLASS file contains byte code instructions for the JVM and is system idependent.