Thursday, August 6, 2009

java - Abstraction, Interfaces and Inheritance

Abstraction :

Abstraction refers to the ability to make a class abstract in OOP. An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class.

Abstract class:
Use keyword abstract to declare abstract class. (public abstract class Abstractclassdemo { })

Abstract method :

If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.

If method is abstract the class should be abstract.

Ex : Abstraction is useful when extending plugin. If you need to fetch data(methods, fields) from Ad Tech plugin they will allow you to extend that plugin to use method and fields. But, they don't want to instantiate. Because, you are not going to set any values. But, they might have abstract method in their plugin. They want you to use the method child class by override the abstract method.

Interface:
Interface is useful if you want some one to force to use the methods in the child classes. You need to use implement keyword for interface. If you implement you need to use all methods declared in the interface.

Inheritance in java
Person (Parent)
  1. Employee (First Child)
  2. Owner (Second Child)

Both the child belongs to parent (Person).
Employee extends Person
Owner extends Person

1st child : When you have object you can access methods from the own Employee object. Also, you can access method from Parent object (Person)
2nd child : When you have object you can access methods from the own Owner object. Also, you can access method from Parent object (Person)

Parent : When you have Parent Object (Person) you can access only person methods (only the parent method). If you want to access child methods you need to typecast the Parent object to child object (Employee child or Owner child) whichever you want.

Note : You cannot typecast the child object to child object or child object to parent object. Only parent can typecast to any child you want.

Wednesday, August 5, 2009

Java - Collections sample Code

package helloworld;

import java.util.*;

public class TestCollections {
public static void main(String[] args) {
TestCollections arh = new TestCollections();
arh.arrayEx();
arh.hashTableEx();
arh.arrayList();
arh.hashSet();
arh.hashMap();
}

public void arrayEx() {
String[] test = new String[6];
int[] b = { 0, 1, 2, 3, 4 };
String[] testStore = { "Gube", "Kavi", "siva", "arivu", "Bog" };
for (int i : b) {
// System.out.println(i + " " + testStore[i]);
test[i] = testStore[i];
}
}

public void hashTableEx() {
Hashtable testHash = new Hashtable();
testHash.put("Gubs", new String("Siva"));
testHash.put("Arivu", new String("Mom"));
Enumeration t;
t = testHash.keys();
while (t.hasMoreElements()) {
String str = t.nextElement();
System.out.println("Print Hash " + testHash.get(str));
}
}

public void arrayList() {
List l1 = new ArrayList();
l1.add("Gubs");
l1.add("Kavitha");
l1.add("Arivu");

System.out.println("Array List Elements : " + l1);
}

public void hashSet() {
Set h1 = new HashSet();
h1.add("Gubs");
h1.add("Kavi");
System.out.println("Hash Set Elements : " + h1);
}

public void hashMap() {
Map hm = new HashMap();
hm.put("Gubs", "Kavi");
hm.put("Siva", "Arivu");
System.out.println("Hash Map Elements : " + hm);
}
}




LinkedHashMap
TreeMap
HashMap
Set (unique objects no duplicates)
List (list of objects no need to specify size like array its growable)

Collections.binarySearch
Collections.emptyList()
Collections.emptyMap()
Collections.emptySet()

Java - Jackrabbit + activemq + tomcat in linux

Jackrabbit:
----------
Download apache jackrabbit jar file from browser : http://jackrabbit.apache.org/downloads.html
Command below to run jackrabbit in localhost port 8085
Jackrabbit needs to run before Operative.One application started :
java -jar jackrabbit-standalone-2.1.0.jar -p 8085 &

Activemq:
--------
Download apache activemq from browser : http://activemq.apache.org/download.html
untar activemq (tar -xvzf apache-activemq-*-bin.tar.gz)
Run activemq : a) cd [activemq_install_dir] b) ./bin/activemq &

Activemq memory leak issue prolong for longtime where consumer fails to read messages and hangs often. Explained neat by correcting memory limit in activemq.xml we can resolve this issue. Read article :

http://blogs.sourceallies.com/2014/10/activemq-memory-tuning/

How to add apache tomcat server :
---------------------------------
1) Download apache tomcat server 6.0 from browser : http://tomcat.apache.org/
2) Untar in your box. (tar -xvzf apache-tomcat-*.tar.gz)
3) In eclipse under server tab right click New -> Servers -> Apache (Tomcat v 6.0)-> Next give the apache tomcat located path and click finish.
4) In server tab apache tomcat will exist. Select right click and start. Stop also select tomcat and do stop. If tomcat is not getting stopped then find the process (ps -ef) and kill the process from terminal

In VM(Virtual Machine) Arguments before you start apache tomcat edit and update configuration in front heap memories to avoid Out of memory in tomcat start : -Xms512m -Xmx1024m -XX:MaxPermSize=256m

Java - Tutorial Links and Start up

Useful Java tutorial links

http://www.tutorialspoint.com/java/java_basic_operators.htm - Tutorial point start up. If you already familiar in Object Oriented its easy to start with this link.

http://download-llnw.oracle.com/javase/tutorial/java/concepts/object.html

http://www.planetpdf.com/developer/article.asp?ContentID=6632

http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

http://java.dzone.com/videos/brief-history-java-and-jdbc - Write once run anywhere

http://java.sun.com/docs/books/tutorial/java/index.html
http://java.sun.com/docs/books/tutorial/

About Ant - Another Neat Tool :
http://codefeed.com/tutorial/ant_intro.html
http://en.wikipedia.org/wiki/Apache_Ant
(Ant - Another Neat Tool - build processing tool.

JAR - Java ARchive. (Archive the class files into one JAR File)
Operator Precedence easy - BODMAS - Brackets of , division, multiplication, addition, subtraction

set up java - This link is internal for Operative.
http://tech.internal.operative.com/node/926
http://tech.internal.operative.com/node/1111 -

Install java in linux :
sudo apt-get install sun-java6-jdk mysql-server-5.1 ubuntu-restricted-extras libmysql-java libsvn-java

In java for boolean field getter / setter the getter, setter should be like below. (column : is_status)


public boolean isIsStatus() {
return isStatus;
}

public void setIsStatus(boolean isStatus) {
this.isStatus = isStatus;
}
// Below script tag for SyntaxHighLighter