Difference between inheritance and polymorphism?

This can be a confusing topic because one can argue that sharing method attributes between a super or abstract class with a derived class could be described by either Polymorphism or Inheritance. One significant difference could be illustrated as to when or under what circumstances you would you use one or the other.

An ideal instance of using Inheritance would be when you want to create an entirely new class, but wish to borrow a group of existing attributes or methods resident in an existing Abstract or super Class, instead of re-inventing the wheel. If you had an Abstract Class Carnivore and wanted to create a subclass Cat, you could instantly inherit all the methods that were common in Carnivore that applied to Cat without writing new code.

Polymorphism could be best applied when you had an existing SubClass that you wanted to modify or add a feature that could borrow a method that existed in a higher class. Take the Cat subclass, which inherited attributes from Carnivore, and you wanted to add the method Stalk, which would generically describe how a carnivore approaches its prey. However its implementation in Cat would detail the stealth it uses that are unique to a Cat.

Inheritance is the object oriented feature by which the functionality and features of one class are made available to another class without having to copy paste the code.

Polymorphism is the object oriented feature by which multiple methods of a class can coexist with minor differences. Ex: method overloading.


About serialVersionUID in Java Serialization

serialVersionUID is used to ensure that during deserialization the same class (tha was used during serialize process) is loaded. This is a one line definition to explain why a serialVersionUID is used?
Apart from the above definition there are quite a few things to learn from this serialVersionUID. As per javadocs, following is format of serialVersionUID:

serialVersionUID Syntax
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
serialVersionUID is a static final field. You can assign any number of your choice to it. Later I will explain the significance of these two statements.

Why serialVersionUID?
Let’s start with annoying warning message you get in your IDE when you declare a class as

The Seriablizable class Lion does not declare a static final serialVersionUID field of type long
Most of us used to ignore this message as we always do for a warning. My general note is, always pay attention to the java warning messages. It will help you to learn a lot of fundamentals.

serialVersionUID is a must in serialization process. But it is optional for the developer to add it in java source file. If you are not going to add it in java source file, serialization runtime will generate a serialVersionUID and associate it with the class. The serialized object will contain this serialVersionUID along with other data.
Even though serialVersionUID is a static field, it gets serialized along with the object. This is one exception to the general serialization rule that, “static fields are not serialized”.

How serialVersionUID is generated?
serialVersionUID is a 64-bit hash of the class name, interface class names, methods and fields.

Serialization runtime generates a serialVersionUID if you do not add one in source.

It is advised to have serialVersionUID as unique as possible. Thats why the java runtime chose to have such a complex algorithm to generate it.

If you want help in generating it, jdk tools provides a tool named serialver. Use serialver -show to start the gui version of the tool as shown below.

How serialVersionUID works?
When an object is serialized, the serialVersionUID is serialized along with the other contents.
Later when that is deserialized, the serialVersionUID from the deserialized object is extracted and compared with the serialVersionUID of the loaded class.

If the numbers do not match then,InvalidClassException is thrown.

If the loaded class is not having a serialVersionUID declared, then it is automatically generated using the same algorithm as before.

Strongly recommended to declare serialVersionUID
Javadocs says, “the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization”

Now you know why we should declare a serialVersionUID.

Not only declaring a serialVersionUID is sufficient. You must do the following two things carefully. Otherwise it defeats the purpose of having the serialVersionUID.

serialVersionUID should be maintained. As and when you change anything in the class, you should upgrade the serailVersionUID.

Try your best to declare a unique serialVersionUID.

Demonstrate serialVersionUID
Initial class to be serialized has a serialVersionUID as 1L.
import java.io.Serializable;
public class Lion implements Serializable {
private static final long serialVersionUID = 1L;
private String sound;
public Lion(String sound) {
this.sound = sound;
public String getSound() {
return sound;

Test serialVersionUID:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerialVersionUIDTest {
public static void main(String args[]) throwsIOException, ClassNotFoundException {
Lion leo = new Lion(“roar”);
// serialize
System.out.println(“Serialization done.”);
FileOutputStream fos = newFileOutputStream(“serial.out”);
ObjectOutputStream oos = newObjectOutputStream(fos);
// Deserialize
FileInputStream fis = newFileInputStream(“serial.out”);
ObjectInputStream ois = newObjectInputStream(fis);
Lion deserializedObj = (Lion) ois.readObject();
System.out.println(“DeSerialization done. Lion: ” + deserializedObj.getSound());

Serialization done.
Deserialization done. Lion: roar

Now change serialVersionUID to 2L in Lion class.

private static final long serialVersionUID = 2L;
Comment the “serialize” block (4 lines of code) in SerialVersionUIDTest. Now run it and you will get the following exception.

Serialized Lion with serialVersionUID with 1L.
Changed serialVersionUID to 2L and compiled and loaded the class
Deserialize the already serialized object and load it with the latest class
We get exception as serialVersionUID is not matching.

Exception in thread “main”java.io.InvalidClassException: Lion; local classincompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at SerialVersionUIDTest.main(SerialVersionUIDTest.java:21)

How to view the SQLite database on your Android Emulator

This tutorial will show you how to open a SQLite datbase from your Android emulator.

This requires Eclipse and the Android plugin. If you don’t have them, follow this tutorial.

This also requires a SQLite viewer. I used SQLite Database Browser.

Step 1
With Eclipse open and your emulator running
select the DDMS perspective by clicking on the Window -> Open Perspective -> DDMS menu option.

Step 2
Select the emulator you have currently have running.
Click the File Explorer tab
Find the data folder.

Step 3
Follow the filepath to the application you want /data/data/your.app.namespace/dbname.db
Click the Pull a file from the device button and save the database file on your computer.

Step 4
Open the SQLite Database Viewer and click Open Database.
Open the file and you can browse the data and view the schema!

This can really help you debug some issues with your SQLite database!

With a few modifications, you should also be able to do this with your Android phone. The only problem I ran into was a permissions issue getting to the database file. I have not rooted my phone. There may also be other ways to get at it without rooting your phone.

Difference between HTTP and HTTPS?

HTTP is hypertext transfer protocol which is responsible for transmitting and receiving information across the Internet whereas HTTPS is secure http, which is used exchanging confidential information with a server, which needs to be secured in order to prevent unauthorized access

HTTP transmits normal data whereas HTTPS transmits closed or encrypted data.

HTTP is for normal applications and HTTPS is mostly for banking or secure applications.

HTTP uses port 80 whereas HTTPS uses port 443

HTTP is defined in RFC 2616 and HTTPS is defined in RFC 2817

What is Association, Aggregation,Composition, Abstraction, Generalization

What is Association?

Association is a relationship between two objects. In other words, association defines the multiplicity between objects. You may be aware of one-to-one, one-to-many, many-to-one, many-to-many all these words define an association between objects. Aggregation is a special form of association. Composition is a special form of aggregation.

Example: A Student and a Faculty are having an association.

What is Aggregation?

Aggregation is a special case of association. A directional association between objects. When an object ‘has-a’ another object, then you have got an aggregation between them. Direction between them specified which object contains the other object. 

Aggregation is also called a “Has-a” relationship.

Aggregation is a relationship between two classes that is best described as a “has-a” and “whole/part” relationship. It is a more specialized version of the association relationship. 

The aggregate class contains a reference to another class and is said to have ownership of that class. Each class referenced is considered to be part-of the aggregate class.Ownership occurs because there can be no cyclic references in an aggregation relationship. 

If Class A contains a reference to Class B and Class B contains a reference to Class A then no clear ownership can be determined and the relationship is simply one of association.

For example, imagine a Student class that stores information about individual students at a school. Now let’s say there is a Subject class that holds the details about a particular subject (e.g., history, geography). If the Student class is defined to contain a Subject object then it can be said that the Student object has-a Subject object. The Subject object also makes up part-of the Student object, after all there is no student without a subject to study. The Student object is therefore the owner of the Subject object.


There is an aggregation relationship between Student class and the Subject class:

public class Subject {
private String name;
public void setName(String name)
this.name = name;
public String getName()
return name;
public class Student {
private Subject[] studyAreas = new Subject[10];
//the rest of the Student class

What is Composition?

Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.

Example: A class contains students. A student cannot exist without a class. There exists composition between class and students.

What is Abstraction?

Abstraction is specifying the framework and hiding the implementation level information. Concreteness will be built on top of the abstraction. It gives you a blueprint to follow to while implementing the details. Abstraction reduces the complexity by hiding low level details.

Example: A wire frame model of a car.

What is Generalization?

Generalization uses a “is-a” relationship from a specialization to the generalization class. Common structure and behaviour are used from the specialization to the generalized class. At a very broader level you can understand this as inheritance. Why I take the term inheritance is, you can relate this term very well. Generalization is also called a “Is-a” relationship.

Example: Consider there exists a class named Person. A student is a person. A faculty is a person. Therefore here the relationship between student and person, similarly faculty and person is generalization.