Java Programming Fundamentals -1


1.  Object - When do we override Hash code method and when equals method?
hashCode() method : This method returns a hashcode value as an int for the object. Default implementation for hashcode() should be overridden in order to make searching of data faster. The implementation of hashCode() method for an user-defined object should be calculated based on the properties of the class which we wish to consider.
equals() method : This method returns a boolean which specifies whether two objects are equal or not. The default implementation of equals() method given by the Object Class uses the ‘==’ operator to compare two object references, and returns true only if they refer to the same object. But, we can meaningfully re-define this equals() method to have en equality check based on our own criterias.
2.  Composition & Encapsulation
Given that the inheritance relationship makes it hard to change the interface of a superclass, it is worth looking at an alternative approach provided by composition. It turns out that when your goal is code reuse, composition provides an approach that yields easier-to-change code.

If a class A encapsulates class B , it means that the specific instance of B got via A should only be modifiable through class A not to break the data encapsulation of class A.
3.  Run time polymorphism & Compile time polymorphism
Polymorphism is defined as one interface to control access to a general class of actions. Compile time polymorphism is functions and operators overloading. Runtime time polymorphism is done using inheritance and virtual functions. 
4.  Passby value & passby reference
Pass by Reference means the passing the address itself rather than passing the value and pass by valuemeans passing a copy of the value as an argument.
5.  Wrapper classes & Mutable classes

Wrapper classes are used to represent primitive values when an Object is required. A primitive wrapper class in the Java programming language is one of eight classes provided in the java.lang package to provide object methods for the eight primitive types. All of the primitive wrapper classes in Java are immutable. J2SE 5.0 introduced autoboxing of primitive types into their wrapper object, and automatic unboxing of the wrapper objects into their primitive value—the implicit conversion between the wrapper objects and primitive values.
With Java 5.0, additional wrapper classes were introduced in the java.util.concurrent.atomic package. These classes are mutable and cannot be used as a replacement for the regular wrapper classes. Instead, they provide atomic operations for addition, increment and assignment.

The atomic wrapper classes and their corresponding types are:
Primitive typeWrapper class
intAtomicInteger
longAtomicLong
booleanAtomicBoolean
VAtomicReference

Mutable Objects: When you have a reference to an instance of an object, the contents of that instance can be altered
Immutable Objects: When you have a reference to an instance of an object, the contents of that instance cannot be altered

6.  Synchronized block & Dead lock
In Java you can mark a method or a block of code as synchronized. Synchronized blocks can be used to avoid race conditions. A deadlock is a situation wherein two or more competing actions are each waiting for the other to finish, and thus neither ever does

7. Serialization

Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object. After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory. Most impressive is that the entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform. Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object. The ObjectOutputStream class contains many write methods for writing various data types, but one method in particular stands out:
public final void writeObject(Object x) throws IOException
The above method serializes an Object and sends it to the output stream. Similarly, the ObjectInputStream class contains the following method for deserializing an object:
public final Object readObject() throws IOException, 
                                 ClassNotFoundException
This method retrieves the next Object out of the stream and deserializes it. The return value is Object, so you will need to cast it to its appropriate data type.

9.  Class loader
A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class. Given the name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system.
Every Class object contains a reference to the ClassLoader that defined it. Class objects for array classes are not created by class loaders, but are created automatically as required by the Java runtime. The class loader for an array class, as returned by Class.getClassLoader() is the same as the class loader for its element type; if the element type is a primitive type, then the array class has no class loader. Applications implement subclasses of ClassLoader in order to extend the manner in which the Java virtual machine dynamically loads classes.
What is the difference between Class.forName() andClassLoader.loadClass()?
Both methods try to dynamically locate and load ajava.lang.Class object corresponding to a given class name. However, their behavior differs regarding whichjava.lang.ClassLoader they use for class loading and whether or not the resulting Class object is initialized.

By comparison, ClassLoader.loadClass() is an instance method and requires you to select a particular classloader, which may or may not be the loader that loads that calling code. If picking a specific loader to load the class is important to your design, you should use ClassLoader.loadClass() or the three-parameter version of forName() added in Java 2 Platform, Standard Edition (J2SE): Class.forName(String, boolean, ClassLoader).
Additionally, Class.forName()'s common form initializes the loaded class. The visible effect of this is the execution of the class's static initializers as well as byte code corresponding to initialization expressions of all static fields (this process occurs recursively for all the class's superclasses). This differs fromClassLoader.loadClass() behavior, which delays initialization until the class is used for the first time.


10. Types of inheritance
There exists basically three types of inheritance.
  • Multilevel inheritance
  • Multiple inheritance
  • Hierarchical inheritance
In single inheritance, one class extends one class only. In multilevel inheritance, the ladder of single inheritance increases. In multiple inheritance, one class directly extends more than one class and in hierarchical inheritance one class is extended by more than one class. Let us go in detail programmatically.
11. Hierarchy of execution for static block & constructor
  • Static blocks gets executed first in order they are declared in superclass then sublass.
  • Initlization blocks of superclass followed by superclass constructor.
  • Initilization blocks of subclass followed by supclass constructor

Comments

Popular posts from this blog

jQgrid reload with new data

Introduction to jQgrid

Rich Client based UI technologies- A Comparison of Thick Client UI tools