Thursday, October 3, 2013

Difference between Abstract class and Interface

One more common interview question from the Java interview bucket.

Both conceptually doing the same work but with few differences. Its up to the programmer how he / she wants to use it and when. Here I have mentioned few of their major differences with example.

Declaration
Abstract class is declared by using abstract keyword along with the class signature.
public abstract class AbstractTest {
}

Interface is declared by using interface keyword.
public interface InterfaceTest {
}

Class Members
Abstract class members can be public, private, protected or default. But Interface members are public by default.
public interface InterfaceTest {
private String testString
}

The above code will result in compilation error with message: Illegal modifier for Interface. Because it only allows public, static and final access specifiers.

Variable declaration
Interface can have only constant variables which are by default public static final where as Abstract class can have non-final variables as a normal class.

public interface InterfaceTest {
String testString = "ABCTEST";

}

The above code will work fine without any compilation error. The variable declaration is equal to
public static final String testString = "ABCTEST" by default.

Methods
Only abstract methods are allowed inside an interface. Abstract class can contain concrete methods with abstract methods.
Abstract method: Methods without any body, only method declaration.
Concrete method: Methods with body / implementation inside it.

Multiple Inheritance
A class can implement more than one Interface but a class can only extend one abstract class at a time. After extending one class you can't extend any other class.

Absolutely abstract
Why Interface are called absolutely abstract ? Because, Interface can not have main method  (public static void main(String[] args)) declared inside it. It can't be instantiated by any chance.

An Abstract class can have a main method where you can use it as a normal Java class to execute. But it can't instantiated outside the abstract class.

Hope this article help you to understand the difference between abstract class and interface. You can post your questions or suggestions in the comment section.

Wednesday, September 25, 2013

Swapping of two numbers in Java

One more common interview questions you might have faced in Java interviews.Swapping of numbers is possible in Java by using two methodologies. Either you can do it through a third variable or without third or temporary variable.

Lets have a look on those two types of swapping through Java code.

Swapping two numbers without using third variable

Suppose you have two variables 'a' and 'b' and you need to swap the value of 'b' to 'a' and the 'a' to 'b'.
We cant use any third variable so we have to swap it between these two variables only. Here is the code for doing so.


There are other ways too for doing the above apart from addition and subtraction.
Now lets look at the code for using a temporary or third variable to swap two numbers. No trick in this.

Swapping two numbers by using a third variable

Now lets look at the sample code for swapping two numbers by using a third or temporary variable.


















Tuesday, September 24, 2013

Setup JAVA on windows

When you start developing java / j2ee applications you need to setup JAVA on your system. You should setup / configure the JAVA_HOME and path as system properties to compile and execute a Java file.
This setup information is only for Windows OS.

Install JDK (Java Development Kit)
Before starting your configuration you should have the JDK installer downloaded from Oracle website. Earlier it was with Sun Microsystems. You may need to sign-in to get the JDK installer from the website.
Follow some basic steps to log-in and get your installer file. Its pretty simple. After the JDK installer will be downloaded double click on the exe file which will take you through installation process step by step.
Once your JDK got installed follow the steps described below to setup JAVA system property on your machine.

Configure JAVA_HOME

Right click on your 'Computer' icon present in desktop screen of your system. Select 'properties' from the menu list. Then click on 'Advanced system settings'. A System property window will appear as its shown in the following image. Check out for Advanced tab if you don't find the above settings on your OS. 




At the bottom of the above window you can see a button labeled as 'Environment Variables'. Click that button. Now you have the setup window where you will add the Java system property. 

To add JAVA_HOME as a part of user variables, click on New... button listed under User Variable section. Set JAVA_HOME as variable name. Make sure you are putting the correct variable name. Its a standard variable name used across Java supported applications. 


Then browse through your Java installation directory and copy the directory path from address bar of windows explorer. For me its installed under C:/Program Files (x86)/Java directory. Make sure you are selecting the correct path for JAVA_HOME. The home or root directory will have bin, lib, jre and few other subfolders. Copy and paste the directory path into Variable value field. Then select OK button to confirm your settings.

Configure path variable  

After setting up the JAVA_HOME property you should set the path variable too in your advance system property. Repeat the above steps to add a new user variable. 


In Variable name field add path and in Variable value field add %JAVA_HOME%/bin; value. Then click OK and the settings will be saved. 

Test your setup

Now you are ready to test your java setup. To test it open the command line window. 
And type java -version in the command window. The installed version will be displayed on your command screen. See the screen below for your reference.


Type java command for more available options. Post your comments if you find any issues in the post. 




Thursday, September 22, 2011

OOPS Concept In Java

Java basically works on four basic OOPS concept and they are

1. Abstraction
2. Encapsulation
3. Polymorphism
4. Inheritance

Abstraction means incomplete. Abstraction in java can be achieved by use of interface or abstract class. Abstraction declares the functions, it doesn't define or provide the details how it will be used.
It can't be instantiated. The concrete class who will implement the interface or extend the abstract class has to implement the functionality of the function / method.

Encapsulations hides the data from external use. You can't access the data which are declared as private inside the class. By using encapsulation you are keeping data safe in Java.

Polymorphism stands for many forms one name. Method overriding and method overloading are considered to be part of polymorphism concept.

Acquiring parent's properties within child class is called Inheritance in java. Its like parent-child relationship. Extending a class / interface is inheritance in Java.

More detailed posts regarding oops concept can be found in my blogs. I have explained it with real time examples in some of them.

Saturday, September 25, 2010

Abstraction in Java

Hiding unnecessary data from the user is called abstraction. Abstraction is the process of recognizing and focusing on important characteristics of an object and filtering out un-wanted characteristics of an object.
Real time example: A person can be abstracted differently in different situations.
A doctor sees a person as a patient. The doctor is interested in person’s name, height, weight, age, blood group and diseases. Where as an employer is interested in the person’s name, degree, percentage, work experience etc.
This is a beautiful example for abstraction.
In java Interface follows the concept of abstraction.


One of my friend Rajesh Hegde gives this concept about data abstraction.
Abstraction is a way to remove the association of the behavior of an object with the actual details behind the scenes which implement that object's behavior.This 'abstraction' is usually accomplished through the use of base classes with virtual functions; each derived function provides the details that implement the behavior behind that abstraction.A simple example is using a base class "Animal", with a virtual function "Walk". In the case two-legged versus four-legged animals, both of them walk, but the actual mechanics are different. The "Walk" method abstracts the actual mechanics behind the walking that each "Animal" does.