Wednesday, June 2, 2010

Cheat Sheet

****************************************
1. Multiple Inheritance :
****************************************
Java does not allow multiple inheritance unlike C++. For ex: a child inherits behavior from two parents. So if you consider a child as an class, then it should be able to inherit from both Mom class and Dad class. So it makes sense to have this multiple inheritance. However Java does not support this. Reason as cited by many authors is, design complexity outweighs its advantages. So Java does not have it.
* However Java supports implementaion of multiple interfaces.

****************************************
2. Operator Overloading :
****************************************
There is no Operator overloading in Java. Java does overloading of + sign to concatenate strings. That is the only overloaded operator in Java. Reason for not providing this option in Java is, problems associated with operator overloading outweigh its advantages. Code becomes confusing and difficult to sustain. So they avoided it.

****************************************
3. Order of allocation in creating a new object :
****************************************
(a) Constructor of the super class is invoked. This takes first precedence.
(b) Initialization of class and object attributes. These are not attributes initialized in the constructor. But instance variables of scope class and object.
(c) Code in the constructor executes.

****************************************
4. Best way to do error handling :
****************************************
Throw Exceptions to the logical point. Caller should be able to fix the problem and re-invoke it.
Fixing the problem could be through
(a) Retry an operation
(b) Collect different data from users.
(c) Report problem back to users.

****************************************
5. Attribute scopes :
****************************************
(a) Local attributes : Declared and initialized inside methods of a class. If local attributes have same name as object attributes, you can specifically invoke object attributes by using "this" operator. "this" refers to the object here.
(b) Object attributes: Not shared between different object instances of a class. These are non-static data members of a class definition.
(c) Class attributes: Shared between all object instances created from that class. These are static data members of a class definition.

****************************************
6. Copying objects :
****************************************
Objects have references to sub objects. This leads to two types of copying - Shallow Versus Deep Copy. You need to provide an implementation for copy method to do a deep copy by traversing through all sub-objects following references. Else you will end up in shallow copy.

****************************************
7. Comparing objects :
****************************************
Just like we have Shallow Versus Deep copy, we also have Shallow versus Deep compare. So you need to provide an implementation for compare method to do a deep compare by traversing through all sub-objects following references. Else you will end up in shallow compare.

****************************************
8. Dangling pointers:
****************************************
TBD

****************************************
9. Garbage Collection:
****************************************
TBD.
Does Java support GC of circular references? - TBD

****************************************
10. MT-Safe
****************************************
1. How does locks work on synchronized methods?
Refer to this tutorial

****************************************
11. Call by value or Call by reference?
****************************************
Refer to this detailed article
CodeGuru Article

****************************************
12. Nested Classes
****************************************
Static inner nested class:
JavaWorld Article

Inner Classes
JavaWorld Article

****************************************
13. Collection
****************************************
Two concepts:
a) Collection :
It is a group of individual elements, often with some rule applied to them. Contains

(i) List -
* A List holds the elements in a particular sequence.
* Allows duplicate values.

(ii) Set -
* No sequence. Just a bag of elements.
* Does not allow any duplicate elements.

b) Map :
(i) It is a group of key-value object pairs. A Map can return a Set of its keys, a Collection of its values, or a Set of its pairs.
(ii) Like Arrays, it is possible to have multiple dimensions without extra implementation. This can be obtained by making value of a key another map.

Commonly used data structures :
-----------------------------------------------
Interface : Concrete Implementation
-----------------------------------------------
Set : HashSet
List : ArrayList
Queue : LinkedList
Map : HashMap / HashTable
SortedSet : Dictionary
----------------------------------------------

Unordered Collection Interface:
1. Collection
2. Set
3. List
4. Queue
5. Map

Ordered Collection Interfaces
1. SortedSet
2. SortedMap

Summary of interfaces


******************** END of CHEAT SHEET ********************