Sunday, December 18, 2011

All karma No luck

There is nothing called luck ..... it's only your karma.... this is how you have chosen your life

Thursday, July 28, 2011

Abstract Class vs Interface(When to use abstract and Interface)

Abstract Classes versus Interfaces

1) Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead.
Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way. Think of Comparable or Cloneable, for example.
By comparison, abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).

2) If you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method. If you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change.

When to use interface and abstract class in Java

 In order to answer this question, you need to have good understanding of OOPS concepts likPolymorphismEncapsulationAbstraction and Inheritance. Also familiarity with coupling and cohesion is important. You at least should know that effort of designing should lead to reduce coupling and increased cohesion, ease of maintenance etc. In this part, we will see some scenarios, guidelines, rules which can help you to decide when to use abstract class and interface in Java.

1) In Java particularly, decision between choosing Abstract class and interface may influence by the fact that multiple inheritance is not supported in Java. One class can only extend another class in Java. If you choose abstract class over interface than you lost your chance to extend another class, while at the same time you can implement multiple interfaces to show that you have multiple capability. One of the common example, in favor of interface over abstract class is Thread vs Runnable case. If you want to execute a task and need run() method it's better to implement Runnable interface than extending Thread class.

2) Let's see another case where an abstract class suits better than interface. Since abstract class can include concrete methods, it’s great for maintenance point of view, particularly when your base class is evolving and keep changing. If you need a functionality across all your implementation e.g. a common method, than, you need to change every single implementation to include that change if  you have chosen interface to describe your base class. Abstract class comes handy in this case because you can just define new functionality in abstract super class and every sub class will automatically gets it. In short, abstract class are great in terms of evolving functionality. If you are using interface, you need to exercise extra care while defining contracts because its not easy to change them once published.

3) Interface in Java is great for defining Types. Programming for interfaces than implementation is also one of the useful Object oriented design principle which suggests benefit of using interface as argument to function, return type etc.

4) One more general rule of when to use abstract class and interface is to find out whether a certain class will form a IS-A hierarchy or CAN-DO-THIS hierarchy. If you know that you will be creating classes e.g. CircleSquare than it's better to create an abstract class Shape which can have area() and perimeter() as abstract method, rather than defining Shape as interface in Java. On the other hand if you are going to create classes which can do thinks like, can fly, you can use interface Flyable instead of abstract class.

5) Interface generally define capability e.g. Runnable can run()Callable can call()Displayable can display(). So if you need to define capability, consider using interface. Since a class can have multiple capabilities i.e. a class can be Runnable as well as Displayable at same time. As discussed in first point, Since java does not allow multiple inheritance at class levelonly way to provide multiple capability is viainterfaces.


6) Let's see another example of where to use Abstract class and Interface in Java, which is related to earlier point. Suppose you have lot of classes to model which are birds, which can fly, than creating a base abstract class as Bird would be appropriate  but if you have to model other things along with Birds, which can fly e.g. AirplanesBalloons or Kites than it's better to create interface Flyable to represent flying functionality. In conclusion, if you need to provide a functionality which is used by same type of class than use Abstract class and if functionality can be used by completely unrelated classes than use interface.

7) Another interesting use of Abstract class and interface is defining contract using interface and providing skeletal using abstract class. java.util.List from Java collection framework is a good example of this pattern. List is declared as interface and extends Collection and Iterable interface and AbstractList is an abstract class which implements ListAbstractList provides skeletal implementation of Listinterface. Benefit of using this approach is that it minimize the effort to implement this interface by concrete class e.g. ArrayList or LinkedList. If you don't use skeletal implementation e.g. abstract class and instead decide to implement List interface than not only you need to implement all List methods but also you might be duplicating common code. Abstract class in this case reduce effort to implement interface.

8) Interface also provide more decoupling than abstract class because interface doesn't contain any implementation detail, while abstract class may contain default implementation which may couple them with other class or resource.

9) Using interface also help while implementinDependency Injection design pattern and makes testing easy. Many mock testing framework utilize this behavior.

That's all on When to use Abstract class and interface in Java. Though discussion here is centered around Java but given concept of abstract class and interface goes beyond Java and also applicable to other Object oriented language, some of the tips are also applicable to other OOPS languages. Key thing to remember here is there definition of abstract class and interface e.g. in C++ and C# it varies a lot like in C++ and Java. Single most difference is multiple inheritance. We have also discussed some key differences between abstract class and interface in Java, which influence decision of choosing abstract class over interface or vice-versa. Last thing to remember is that interface is extremely difficult to evolve, so put extra care while designing interfaces.

PS: Effective Java, which is one of the best book on Java programming also has couple of items on interface and abstract class. Joshua Bloch has advised to prefer interface over abstract class in some scenario, which is worth reading.


Read more: http://javarevisited.blogspot.com/2013/05/difference-between-abstract-class-vs-interface-java-when-prefer-over-design-oops.html#ixzz2YlPVcryB

Friday, June 10, 2011

ArrayList Source code (Refer for -->fail fast and other doubts like iterator )

http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Core/Collections-Jar-Zip-Logging-regex/java/util/ArrayList.java.htm

Thursday, June 9, 2011

How to configure ADB on Ubuntu 10.10 (USB problem)




  for the G-Tablet

1. Install Sun JRE and SDK
Unfortunately Ubuntu no longer hosts the SUN SDK/JRE so you will need to obtain this from an alternate source:

Run these commands:
sudo add-apt-repository ppa:sun-java-community-team/sun-java6
sudo apt-get update
sudo apt-get install sun-java6

2. Define your JAVA HOME and PATH:
Edit your .bashrc

Run this command:
sudo gedit $HOME/.bashrc

Add these lines to your .bashrc. Please ensure where you installed the Java libraries and your android sdk. The below reflects default installation directories. (Note last line that includes the Android SDK. You can add this now and install the SDK later. Just make sure if you change the default installation directory that you update your .bashrc file)

export JAVA_HOME="/usr/lib/jvm/java-6-sun-1.6.0.22;"
export PATH=$PATH:$JAVA_HOME/bin
export PATH=$PATH:$HOME"/android-sdk-linux_86/platform-tools"

Run this command to reload your .bashrc:
source ~/.bashrc

3. Install Android SDK:
Goto http://developer.android.com/sdk/index.html
Extract and Install By default, the SDK files are unpacked into a directory named android-sdk-<machine-platform> under your HOME directory.

Navigate to the Android Manager and launch:
cd ~/android-sdk-linux_86/tools
./android update sdk

This will automatically run the android manager and add/update the SDK. I typically install all the packages (for development) but typically for ADB connectivity all you need is the Android SDK Tools and Platform Tools. ADB will not be working properly until you configure the USB driver for the tablet. Thats next.

4. Configure USB Driver
We need to modify your device rules:
sudo gedit /etc/udev/rules.d/99-android.rules(or 51-android.rules these would depend on your ubuntu version)

Then, paste the following line into the file 99-android.rules file

#HTC
UBSYSTEM=="usb", SYSFS{idVendor}=="0bb4", MODE="0666"
SUBSYSTEM=="usb_device", SYSFS{idVendor}=="0bb4", MODE="0666"
#samsung 
UBSYSTEM=="usb", SYSFS{idVendor}=="04e8", MODE="0666"
SUBSYSTEM==”usb_device”, SYSFS{idVendor}==”04e8”,ATTRS{idProduct}=="689e", MODE=”0666”,OWNER="fstech"


Type the command--> "lsusb" on terminal to get the vendor id and product id of the plugged in device 



for example:



Bus 008 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 005: ID 04e8:689e Samsung Electronics Co., Ltd 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

here 04e8 is the vendor(Samsung or HTC ) id and 689e is the product id

**Save and Close gedit (or any other text editor)



**run the command -chmod a+rx /etc/udev/rules.d/51-android.rules


Restart the device manager:
sudo restart udev

Launch adb:
adb kill-server
nohup adb start-server
adb devices













export PATH=${PATH}:/home/fstech/Android/android-sdk-linux_x86/tools
ANDROID_TOOLS=/home/fstech/Android/android-sdk-linux_x86/platform-tools
ANDROID_NDK_ROOT=/home/fstech/Android/android-ndk-r6
export PATH=$ANDROID_TOOLS:$PATH
export PATH=${PATH}:/home/fstech/Android/android-ndk-r6
export PATH=$ANDROID_NDK_ROOT:$PATH



If you see your device your done... PM me if you have any questions...
 

How to include java docs -Eclipse

  1. In the Package Explorer window, choose the package or file for which you want to generate documentation.
  2. Choose File -> Export... -> Javadoc -> Next>
    1. If the dialog box displays the message The Javadoc command does not exist, then you need to click the Configure... button and locate javadoc.exe. You already have this file--it is probably in YourJavaDirectory/jdk1.5.0/bin/.