Sunday, July 29, 2012

How Interfaces are used internally in Java

Before reading this post, I recommend you to go through the following posts:
Interfaces in Java
Runtime Polymorphism in Java
Abstract Classes in Java : Runtime polymorphism

The interfaces have played a key role in making the architecture of Java robust and uniform. The interfaces are used in many places in Java. They are extensively used in JDBC architecture, Collections framework, Multi-threading etc., We will see in detail how interfaces have become crucial for Java to become robust and maintain the uniformity.

Interfaces used in JDBC:

Interfaces are so well used in architecture of JDBC by Java. I should say Java has got most of the flexibility mainly from interfaces and abstract classes. In fact, what Java has done is defined all the method-names in the interfaces. It did not give any implementation for any of the methods. So, what the  DBMS has to do is just use these method-names and build implementation according to the DBMS implementation. In case of migration from one DBMS to other DBMS literally, only the database driver URL, username, password for connection have to be changed.

Now consider a situation without interfaces in Java. In such cases, each DBMS will have its own set of method-implementations and method-names for specific functionalities in Java. Eventually, the method-names for database processing such as executing SQL stored procedures will differ from one DBMS to other DBMS. Now if we want to just change the DBMS we need to change the entire database processing code instead of just changing the driver URL, username and password.


Interfaces used in Multi-Threading:

Similarly interfaces are critical in view of the multi-threading. We know that a thread can be created by two methods.
1. Extending Thread class.
2. Implementing Runnable interface.

Now the first method can impair you in one way. If you extend Thread class, there is no way that you can extend another class since multiple inheritance is not allowed in Java. Now the second option is always good since we can create a class that implements any number of interfaces.

Interfaces used in Collections framework:

Another place where Java has made use of the interfaces is the Collections framework. The Collection framework is the most basic interface. It contains methods to add, remove, search and iterate over list. These are most general functionalities provided. The List, Set and Queue interfaces extend the Collections interface. These interfaces have their specific functionalities in addition to those provided by the Collection interface.

Related Posts:
Interfaces in Java
Runtime Polymorphism in Java
Abstract Classes in Java : Runtime polymorphism
Java Database Connectivity with MS Access -- Part 1
Java Database Connectivity with MS Access -- Part 2
Java Database Connectivity with MS Access -- Part 3

Saturday, July 28, 2012

Interfaces in Java

Before reading this post, I recommend you to read the following posts.
Runtime polymorphism in Java
Abstract Classes in Java:Runtime polymorphism

An interface is a collection of methods that must be implemented by the implementing class. An interface only tells the class what to do. Thus, the interfaces only have the declaration of member variables and methods. Implementing class defines all the methods declared in the interface. If a class implements an interface and does not implement all the methods then the class must be declared as abstract. Variables in interface automatically become static and final variables of the implementing class. Members of interface are implicitly public. A class cannot narrow the accessibility of an interface.

An object reference of an interface can refer to an object of implementing class. However, only methods that are present in the interface can be called using the object reference of interface.

Now lets put the interface into implementation. We consider the implementation of the Stack data structure. Below is the Stack interface and its implementing class MyStack.


The public class which uses the interface and its implementing class is as follows.




Interface Vs. Abstract Class:
Both interface and abstract class cannot have objects, but can have object references. In case of interface, object reference can reference an object of the implementing class. An object reference of abstract class can reference an object of subclass. However, abstract class can have non-abstract methods and instance variables but interface can have neither of them.

Java allows interfaces to be empty. An empty interface contains no variables and methods. It is used to mark a class to be having certain behavior.

Related Posts:
Abstract Classes in Java:Runtime polymorphism
Runtime polymorphism in Java
How Interfaces are used internally in Java
Related Posts Plugin for WordPress, Blogger...