Friday, 2 February 2018

Abstract class Vs Interface in java

Abstract class and interface differ based on following parameters:


Methods

An abstract class can have both abstract methods and concrete methods.
An interface has all the methods as abstract. Methods in interface is 100% abstract.

Keyword used to declare

To declare abstract class use the keyword ‘abstract’. 
Example abstract class dummyClass{//abstract methods, methods}

To declare interface use the word ‘interface’. 
Example interface dummyInterface{//abstract method (s)}

Method declaration

In abstract class, to declare any method as abstract use of abstract keyword is mandatory.
In interface, to declare methods as abstract use of abstract keyword is optional. Methods in interface is by default abstract.

Inheritance Supported

Being a class, an abstract class cannot inherit multiple classes. It can only inherit one class.
An Interface can extend (or inherit) multiple interfaces.

What can be extended?

An abstract class can extend either normal class or abstract class.
Interface can extend only interface but not a class.

Variable type & Access modifier

An abstract class can have static, non-static, final or non-final variable with any access modifier.
An interface can only have public static final variable. In interface variable is constant.

Saturday, 13 January 2018

What are classes implementing Set interface?

We have three classes implementing set interface in java Collection framework hierarchy. Look at the diagram given below:

set interface java radar


From the diagram its clear that classes implementing set interface are:

1. Hashset

HashSet implements Set interface and extends AbstractSet class. It is part of java.util package.
HashSet contain unique elements only. If you add duplicate in it, previous value will be overwritten.
HashSet allows one null value only.
HashSet doesn’t maintain insertion order so when you retrieve elements from it they may be returned in random order. If you want to get element in order of insertion, use LinkedHashSet.

For more details about Treeset follow link Hashset in java


2. LinkedHashset

LinkedHashSet implements Set interface and extends HashSet class. It is part of java.util package.

LinkedHashSet is almost similar to HashSet except that it maintains the insertion order of the elements.

For more details about Treeset follow link LinkedHashset in java


3. Treeset

Treeset class implements set interface and extends AbstractSet class.
When storing element in treeset, it stores them in ascending order by default.

For more details about Treeset follow link Treeset in java

Tuesday, 9 January 2018

What are the classes implementing List interface?

We have three classes implementing list interface in java Collection framework hierarchy. Look at the diagram given below:

list interface java radar



From the diagram its clear that classes implementing list interface are:


1. ArrayList
  • Arraylist was added in Java 1.2 version.
  • Arraylist uses grow able array and thus can grow automatically.
  • Arraylist in java is not synchronized or thread safe.
  • As arraylist is non-synchronized so it is better performance wise as multiple threads can access the resources of arraylist at the same time simultaneously.
  • Arraylist's  iterator and list iterator are fail fast in nature.
  • Follow the link to read in detail about arraylist

Read: Why iterator on arraylist is fail fast in java?


2. LinkedList
  • Linkedlist was added in Java 1.2 version
  • Linkedlist in java is not synchronized
  • Linkedlist stores element in nodes which has capability to store element and its address.
  • Follow the link to read in detail about linkedlist

Read: Difference Arraylist vs Linkedlist


3. Vector
  • Vector is synchronized in nature. This is the major difference between array list and vector.
  • Vector is a legacy class and is not recommended to be used at present.
  • Follow the link to read in detail about vector


You may also like to read about:

Friday, 15 December 2017

Difference: Array vs Vector in java

Array and Vector differs based on the following parameters:

Basic:

Array in java is an object that contains data of homogeneous (similar) type.
example, int a[]={5,6,4,55}; //this array can only store int data type

Vector can store heterogeneous data types, if restriction is not put using generics concept. See the example below:

package string;

import java.util.Iterator;
import java.util.Vector;

public class splitDemo {

      public static void main(String[] args) {

            Vector v = new Vector();
            v.add(1);    //integer
            v.addElement("xyz");  //string
            v.add('s');  //char

            Iterator itr = v.iterator();
            while(itr.hasNext()){
                  System.out.println(itr.next());
            }
      }
}

OUTPUT:
1
xyz
s 

In above example, you can see we have stored int, String and char objects in vector. 

Note: To make vector store homogeneous data type do the following:

Vector<String> v = new Vector<String>();

This will only allow storage of String object in vector.

Family:

Array does not belongs to Collection family. It is a primitive data type.

Vector implements List interface which is a part of collection hierarchy. Thus vector is a part of collection family.

Size:

Array needs to be assigned with the fixed size during initialization thus making it static in nature.

Vector resizes itself dynamically as per the need. Thus it is dynamic in nature.

Synchronized:

Array is non-synchronized in java.

Vector is synchronized in java.

Traversal:

Array can be traversed using for loop or enhanced for loop. It cannot use iterator or enumeration interface like vector or arraylist.

Vector can be traversed using iterator or enumeration interface

Declare/Instantiate/Initialization:

Array
Integer array
int a[]={5,6,4,55};

String array
String a[]={"Ajay","vijay","Rajesh","Mukesh"};

Similarly you can do for other data types like double, float, char, etc

Vector
Vector<String> vec=new Vector<String>();  //create vector
vec.add("Ajay");      //add element to vector
vec.add("vijay");




You may also like to read:

Wednesday, 13 December 2017

Difference: ArrayList vs CopyOnWriteArrayList in java

ArrayList and CopyOnWriteArrayList in java differ based on following parameters:

1. Inclusion in java

Arraylist was added in Java 1.2 version.

CopyOnWriteArrayList was added in Java 1.5 or Java 5.


2. Thread-safety

Arraylist in java is not synchronized thus is not at all thread safe.

CopyOnWriteArrayList is synchronized in nature. Thus it is thread safe and for the same reason only one thread can access the resources in this class at a time.


3. Performance

As arraylist is non-synchronized so it is better performance wise as multiple threads can access the resources of arraylist at the same time simultaneously.

Being synchronized means only one thread can have access to CopyOnWriteArrayList resources thus impacting its performance speed.


4. ConcurrentModificationException

Arraylist throws ConcurrentModificationException.

CopyOnWriteArrayList doesnot throw ConcurrentModificationException.


5. Fail fast or fail safe

 Arraylist's  iterator and list iterator are fail fast in nature.

CopyOnWriteArrayList iterator are fail safe in nature.



You may also like to read:


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.