Saturday, March 30, 2013

Difference between @+id and @android:id, android

While working on a test android application, I noticed two different way of defining ids i.e. @+id and @android:id. Out of curiosity, I googled differences between them and highlighted good differences between two.

1. @+id:
Example <Button android:id="@+id/button_add" ..../>
By defining @+id/button_add we are creating an id named button_add in the namespace of our application which can be referred in our application as @id/button_add.

2. @android:id
<ListView
android:id="@android:id/list"
 By defining @android:id/list means you are referring to an id defined in the android framework namespace. The android framework knows only about the ids in the android namespace. Android framework identifies a list element using android:id/list. It won't work if you change list to any other id like @android:id/listAddNew.

Will add more details about it in future post.

Tuesday, March 26, 2013

Redirecting output from standard output to text file, Java

There are times when you need to redirect all your output to a text file. It is very easy with command line execution by using  '>' output. However in case you want to do it programatically there is very easy way.

Following are the steps to follow:
Add following code block in starting of main function of your application (assuming you want to redirect all of your output to a text file in case not add the code at point from where you want your output to redirect to text file).
1. Create new file where you want your output to store, say out.txt here
File file = new File("out.txt");
2.  Now get output stream of this file. Using this stream, output will be sent to out.txt
FileOutputStream fileOutputStream = new FileOutputStream(file);
PrintStream printStream = new PrintStream(fileOutputStream);
3. And the last step is to update the output stream of system to our file stream instead of default console stream.
System.setOut(printStream); 
Now all is done and all of your console output will be redirected to the out.txt file. In case you want to revert the output to default console you can do by:
System.setOut(System.out);

Sunday, March 10, 2013

src refspec master does not match any, Github

Got this error while pushing changes to newly created repo using windows Github client tool.

This stackoverflow post helped out: http://stackoverflow.com/questions/4181861/src-refspec-master-does-not-match-any-when-pushing-commits-in-git

Solution:
$~ mkdir repo && cd repo
$~ git remote add origin /path/to/origin.git
$~ git add .
Oops! Never committed the origin (source) branch!
$~ git push -u origin master
error: src refspec master does not match any.
Fix:
$~ git commit -m 'initial commit'
$~ git push origin master
Hope it helps

Friday, March 8, 2013

Goodle Drive SDK

Google drive SDK is easy to use API for integrating google drive service to existing web server. Following links are helpful for understanding drive sdk
1. Creating a new file using drive API service (by Google): https://developers.google.com/drive/integrate-create#create_a_new_file_from_the_drive_ui

2. https://developers.google.com/drive/v2/reference/files

Thursday, March 7, 2013

Garbage collection in C#

Useful links to summarize Garbage collection working:

1. Dispose Pattern (used for freeing up unmanaged resources) : http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx
2. Dispose Method (exist with each disposable object) : http://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx

3. In case we are using Dispose method call then order of dispose method call is significant. E.g. If object A allocates object B which in turn allocates object C then dispose method of object A should call dispose method of object B, dispose method of object B should call dispose method of object C.

4. Most common memory leaks in WPF and their fixes: http://svetoslavsavov.blogspot.com/2010/05/memory-leaks-in-wpf-applications.html
Brief: 1. Event handlers registered with parent object.
2. Registering event with static objects. Static members never releases memory so window won't be able to dispose properly.
3. Timers: Associating events with timers can cause memory leaks because timer won't stop automatically and event will keep on calling.
4. Data Binding: Make sure data binding on control should use the dependency property because only they are disposed by the GC. If you bind a control with a non dependency property then that control will not be disposed.
5. Changing text property too much. Keep limit on no. of operations to keep in undo stack. 
 
 
 
 
 
 
 

Monday, March 4, 2013

Object oriented programming concepts (part 1)

1. Program to an interface, not an implementation
This literally means program to a supertype i.e. the declared type of the variables should be a supertype, usually an abstract class or interface, so that a particular variable holding interface object can have any type during runtime and our code should not be limited to any particular data type. Using this concept, we can exploit the polymorphism in OOPS.

2. Is Java, pass by value or pass by reference?
Java is pass by value but in true sense, it is passing value of references of objects.
More details: stackoverflow/is-java-pass-by-reference

3. Primitive data types are passed by value while non primitive types (like class, interface, string) are passed by reference.Java only supports pass by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object .

4. Encapsulation and Abstraction
"encapsulation is concrete, abstraction is...abstract! ;-) encapsulation is an object you can use, abstraction is an ideal you can only discuss. encapsulation is why you wear underwear, abstraction is how you explain the difference between underwear and swimsuits". By a stackoverflow user comment

5. Abstract class vs Interface
No implementation in interface however abstract class have both implementation as well as declarations. Total Abstract class is almost similar to interface with an exception that abstract class can inherit only one super class however interface can inherit as much number of interface as required. Abstract class does not have instance.
a) Interface
Interface cannot be final.
Interface can have variables but they must be static, public, final which means interface can declare only constants.
An interface can extend only other interfaces.
Interface can't be instantiated.
Interface methods could be only public and abstract. No other modifier permitted.
All methods of interface are abstract methods.
Adding a new method necessitate to modify subclasses.
Cannot contain Constructor.
Interfaces are somewhat slow as they require method finding process before calling actual method
Used when something in design changes more frequently.

b) Abstract Class
Abstract class can provide complete default functionality or details to be overridden.
Contains both implementation methods as well as abstract methods.
In a class if there is even a one abstract method, whole class should be declared as abstract class.
Cannot mark a class as both abstract and final
It cannot be instantiated.
Abstract class could be public, private or protected.
Adding a new method does not necessitate to modify subclasses.
Can contain CONSTRUCTOR.
Used when various implementation are of the same kind and use common behavior.
A non abstract method can access an abstract method.

6.  COMPOSITION
"Imagine a software firm that is composed of different Business Units (or departments) like Storage BU, Networking BU. Automobile BU. The life time of these Business Units is governed by the lifetime of the organization. In other words, these Business Units cannot exist independently without the firm. The firm is COMPOSED OF business units."

7. ASSOCIATION
"The software firm may have external caterers serving food to the employees. These caterers are NOT PART OF the firm. However, they are ASSOCIATED with the firm. The caterers can exist even if our software firm is closed down. They may serve another firm! Thus the lifetime of caterers is not governed by the lifetime of the software firm."

8. AGGREGATION
"Consider a Car manufacturing unit. We can think of Car as a whole entity and Car Wheel as part of the Car. (at this point, it may look like composition..hold on) The wheel can be created weeks ahead of time, and it can sit in a warehouse before being placed on a car during assembly. In this example, the Wheel class's instance clearly lives independently of the Car class's instance. Thus, unlike composition, in aggregation, life cycles of the objects involved are not tightly coupled."
Credits: http://stackoverflow.com/questions/731802/what-is-the-difference-between-composition-and-association-relationship

9. Overloading
a) MUST change the argument list
b) CAN change the return type
c) CAN change the access modifier
d) CAN declare new or broader checked exceptions
e) A method can be overloaded in the same class or in a subclass

10 Overriding
a)The overriding method cannot have a more restrictive access modifier than the method being overridden (Ex:  Cannot override a method marked public and make it protected).
b) Cannot override a final method.
c) Cannot override a static method.
To prevent a method from getting over ridden use "final" keyword.

11 Constructor
a) Constructor cannot be inherited and they do not have any return type.
b) Cannot be final, abstract, static in comparison to methods
c) "this" keyword is used to access constructor in the same class. "Super" keyword is used to access constructor in super class.