Friday, December 12, 2008

Two silly questions for abstract classes

1) In which case we need : An abstract class with no abstract methods....

Case 1: I have seen many projects using the base class declared as Abstract. Example : Base class for DVOs, Action classes, DAOs etc. These class can contain some basic methods and properties which can be used or overridden by the subclasses. Here, the main purpose of the class is to create a structure (inheritance) not creating objects.

Case 2: Some adapter classes for AWT listeners are abstract with no abstract methods. For example java.awt.event.WindowAdapter . It represents the scenario when we dont want to implement all the methods of an interface. The subclass can override the methods for a particular functionality.

Case 3: In some cases, we need a class that should not be instantiated and the sub class should use or override some of the methods.

Example : javax.servlet.http.HttpServlet

___________________________________________________________

2) In which case we need : A class with all methods defined as abstract (or should we go for Interface always in this case)

Lets create the problem now :)

Wednesday, December 10, 2008

Difference between '\n' and '\f' and '\r'

  • carriage return = come to initial point of a line
  • new line= come to second line (next line).
  • form feed= some characters will be inserted at that place at run time.
Please put more light on the basic purpose of these characters.

Tuesday, December 9, 2008

How to print the diagonal elements of a square matrix in a single loop?

1st Diagonal:
for(int i=0;i< arr.length; i++)
SOP(arr[i][i]);
2nd Diagonal:
for(int i=0;i< arr.length; i++)
SOP(arr[i][arr.length - (i+1)]);

Abstraction VS Encapsulation

Here is some interesting info from http://www.codeguru.com/forum/archive/index.php/t-439501.html:

Abstraction is "selective ignorance". You choose to ignore the details of implementation be it in context of exposing interfaces/abstract classes in OOP or just plain functions or files or whatever that helps you categorize various components into independent entities that can take up some responsibility to add up to provide the complete behaviour expected of a system. It is above taking up a responsibility by a module / component / class / structure/function in itself and ignoring the details of how it will be actually implemented or implementation of any relationship/dependencies with other entities. Just that component and the functionality that it would provide.

In technology, when designing applications, it is not always possible that you think that way. The thinking becomes dirty by worrying about too many details all at the same time which might be needed but not for the specific task you want to accomplish. This is all part of the divide and rule policy that helps divide a particular task into contributing components and integrating them to build up the whole thing. That is what is abstraction.

Not really about hiding the implementation details but ignoring them. The hiding is what is called encapsulation. In JAVA using private/protected/public/internal/default access specifiers. Encapsulation is about hiding unncessary details that might change or might cause strong coupling between entities or the details which are not really required by other components to achieve what they take ownership of; which is never found to be beneficial.

Also find some useful content from http://stackoverflow.com/questions/24626?sort=newest:

Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object... encapsulation focuses upon the implementation that gives rise to this behavior... encapsulation is most often achieved through information hiding, which is the process of hiding all of the secrets of object that do not contribute to its essential characteristics

Monday, December 8, 2008

Why interface variables are public static and final

My understanding :
1) Why public : Any class or interface in any package should be able to use it. Interface is a contract not implementation.
2) Why final: As per OO design rules, attributes that are not an immutable constants should better not be publically accessible. (final means immutable)
3) why static : Static with final put the variable out of implementation. It will be treated as a constant. Remember : Interface is a contract not implementation.

Difference between Statement and Prepared Statement?

DBMS processes a query in following steps :
1. Parse the SQL query
2. Compile the SQL query
3. Plan(or optimize) the data acquisition path
4. Execute the optimized query / acquire and return data
Simple Statement: Executes all (1 to 4) for every query
PreparedStatement: pre-executes steps 1 to 3