Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, June 10, 2011

trick to obtain object of type interface in java

this concept makes java so scalable and newer vesions of JDK [java development kit] are incorporated with new functionality without messing with the previous packages and classes using this.

in fact this is one of the key concepts upon which the JDK is built.in recommends use of Interfaces on to of hierachy.Thus:
Interfaces -----> implementing classes------> sub-classes


To understand this concept lets take an example :-
in JDBC [java database connectivity] we write the following line of code to create a connection:

Connection con = DriverManager.getConnection( "jdbcurl", "username" , "password") ;

from the above code it can be well understood that ' DriverManager ' is a class which has a static method getConnection() [which takes 3 arguments] and returns an object of type Connection.But ' Connection ' is an interface in java.So how can it ever have an object ?
Problem :- Interfaces cant have objects since they contain atleast one abstract method. So how does ' Connection ' Interface have an object [i.e. ' con ' here] ?

Solution[Concept behind] :-
we know once we have the appropriate object, then using any method is not a problem.But how to obtain object of Interface, coz dats not possible. Thus to obtain objects of type Interface the following trick is deployed

interface Interface
{
public void mymethod(); //abstract method without body
}
class B implements Interface
{
public void mymethod() //giving body to the method
{
System.out.println("Hello Geeks");
}
}

class A
{
public Interface mymethod2() //notice the return type of this method
{
Interface itr=new B(); //here is the juice of the program
//itr is an object of type Interface with content of class B
return itr;
}
}
public class InterfaceProject
{
public static void main(String[] args)
{
A aobj=new A();
Interface interface_obj=aobj.mymethod2(); //thus creating object of type interface
interface_obj.mymethod();
//finally mymethod() is called with interface_obj
}
}


similar trick is found in jdk at various instances: for example ' Servlet ' is an interface having certain concrete and certain abstract methods. this interface is implemented by classes like HttpServlet class and GenericServlet class.These classes give their own implementation to the methods defined abstract in the ' Servlet ' interface.

one should also understand that interface-object and object of type interface are not the same things.the former is not possible while the later is.

Saturday, May 7, 2011

top 5 Java IDE

Java applications have earned their reputation for being highly scalable and platform-independent .Since java(i.e. the java_development_kit or JDK) comes under the category of freewares and distributed widely as an open-source product hence the support available to it is also greater as compared to other programming languages.As such there are numerous IDEs (Integrated Development Enviornment) available using which u can develop your application.
I have been developing java applications for quite sometime and have used a number of IDEs over time. Here's how I rate them
:

1.MyEclipse
advantages: 1.lots of plugins for enterprise application development(JSF,JMS,EJB,Struts) 2.very little configuration required 3.almost everything is included within this one installer. all round solution 4.very well documented.lots of help available within it 5.a real smart intellisence. 6.design modes available for configuration files
disadvantages: 1.its not a freeware.one needs to buy n its expensive 2.system requirement is comparatively higher than other IDEs (min 1gb RAM required)

2. NetBeans

advantages:1.freeware. 2.very well documented and lots of support(both within itself & in the internet) 3.good number of plugins
disadvantages:1.UI looks crappy (metal), icons suck. 2.no refactoring or refactoring templates (try/catch, rename field, etc...). 3.no JSTL-EL support in syntax highlighting/code completion. 4.cannot wrap arbitrary text in editor.

3. Eclipse
advantages:1.its a freeware 2.lots of plugins available 3.highly scalable
disadvantages:1.very little inbuilt support 2.though its highly scalable.yet configuring them into projects can be a problem

4. IBM RAD [Rational Application Developer] & JDeveloper[ by Oracle] :i rate them equal because both has got their own pros n cons. i have used both of them. they are highly used by various corporate group because they come with real powerful websevers which gives a number of customization and services.They are packed with features.

6. BlueJ
advantages:1.its a freeware. 2.very small in size as such easy to transfer the software(one can even mail) 3. the UI is very simple [probably the best IDE for school kids] 4.good for core java applications (like applets)
disadvantages: 1.has got very little functionality 2.only core java aplications can be developed 3.plugin support is very low. 4.the typing intellisence is not very smart