Saturday, February 21, 2009

Here are a few gotchas about inheritance in java. It is followed by an example which highlights some of these.- private instance methods and variables are not inherited. Similarly, final instance methods and variables are not inherited. If you try it, you will get compile time error.- Static methods and static variables cannot be inherited. If static methods are defined again in the sub class, it amounts to redefinition and has nothing to do with overriding. It is also called static method hiding. Similarly if static variables are defined again in the sub class it hides the static variables with same name in the super class.-Similar to static variables , if instance variables are defined again in subclass, it hides the variables with same name in the super class. - For redefinition/hiding it is the same name that matters, the type and access modifiers can be different.- Since static members(variables and methods) belong to a class and not an instance, it is always a good practice to access static members with class name. If they are called using reference type, the reference type and not the object type determines which static method will be called.- Object type determines which overloaded method is called at runtime but reference type determines whether a method can be called i.e. if the method isn't defined in reference type definition, you will get a compile time error.- Reference type and not the object type determines which instance variable is called at runtime.- When you override a method, you can still get the original method by using super.thatMethod() .

//Accesses the static variable of the reference type System.out.println(si.types);

//Accesses the instance variable of the reference type System.out.println(si.wholesaleManufacturer);

//Calls to static methods and static variables //should be made using the classname to avoid confusion StringInstruments.superDefinition(); ClassicalViolin.superDefinition(); System.out.println(StringInstruments.types); System.out.println(ClassicalViolin.types);

Thursday, February 5, 2009

Binary tree is a data structure in which each node has atmost 2 child subtree's, each of which is a binary tree. The child subtree's are called left and right.

A Binary search tree(BST) is a binary tree in which each node has a value, the node values of the left subtree contains only values less than the node value and the node values of the right subtree contains only values greater than the node value. The search tree could allow duplicate values, depending on the implementation.

Following is my implementation of binary search tree in Java with generics.

/** * Remove from the tree. * @param item is the item to remove. * @throws IllegalArgumentException if item is not found. */ public void delete(T item) { this.deleteNode(root,item); }

/** * Internal method to delete a node. * @param subTree is the subtree. * @param item is the item to be deleted. * @return the new subtree. * @throws IllegalArgumentException if the item is not present. */ private Node<T> deleteNode(Node<T> subtree, T item) { //Search for the node first if (subtree != null) { if ((subtree.elem).compareTo(item) < 0) { subtree.rightChild = deleteNode(subtree.rightChild, item); } else if ((subtree.elem).compareTo(item) > 0) { subtree.leftChild = deleteNode(subtree.leftChild, item); } else { /* Found a match. * There are 3 possibilities: * Node is leaf: * Easy, Just delete the node but this is implicitly * handled as part of node has 1 child (see below) * Node has 1 child: * Delete the node and put the child node in its place * Node has 2 children: * Find the leftmost child in the right subtree, * replace the node to be deleted with this child. * Then delete that child node. */ if ((subtree.leftChild != null) && (subtree.rightChild != null)) { //Node has 2 children //Find the leftmost child of the right subtree and //make it the current node, then delete the //leftmost child of the right subtree Node<T> node = findLeftmostChild(subtree.rightChild); subtree.elem = node.elem; subtree.rightChild = deleteNode(subtree.rightChild,node.elem); } else if (subtree.leftChild != null) { //Node has only 1 child i.e. left child subtree = subtree.leftChild; } else { //Node can either have no children or just have 1 right child subtree = subtree.rightChild; } }

About Me

I am a senior software engineer with interests in designing and developing mission-critical applications on Java platform. I love developing software applications and Java Technology. I am also deeply interested in topics such as cloud computing and Web 2.0 technologies. I will be using this blog to share my experience, thoughts and knowledge about matters related to software technology.