Sunday 24 July 2016

How to avoid duplicate values from ArrayList in Java?

This is one of the good interview question. I have faced many times this question in HCL & TCS interview. So frnds, let's see the following answer:

ArrayListDemo1.java

import java.util.*;
public class ArrayListDemo1 {

public static void main(String[] args)
{
ArrayList al = new ArrayList();

     al.add("Prajukta");
     al.add("Bishnupriya");
     al.add("Nitasha");
     al.add("Prajukta");
     al.add("Prajukta");      //Duplicate
     al.add("Prajukta");     //Duplicate

     ArrayList al2 = new ArrayList(new HashSet(al));

    Iterator it= al2.iterator();
    while(it.hasNext())
    {
    System.out.println(it.next());
    }

}
}

Output 
Prajukta
Bishnupriya
Nitasha

Tuesday 19 July 2016

Difference between Java and C

Difference Between Java and C
Java vs. C :
Java
C
Java is object-oriented

C is procedural-oriented
Java is both compiled and interpreted language

C is a compiled language.
Java is platform independent

C is platform dependent
Java is a high level language

C is a low-level language
Java uses bottom-up approach

C uses top-down approach
In Java, pointer is in back stage

C uses pointer explicitly
Java supports method overloading

C does not support method overloading
Java does not support pre-processor

C support pre-processor
Java handles exception

C does not support exception handling mechanism
Java collect garbage automatically
C collect garbage explicitly



Friday 8 July 2016

Why Interface in JAVA ?

We need interface due to the followings:
1.    It supports the functionality of multiple inheritance
2.    It achieves the concept of loose coupling which is the main advantage of interface

3.    It achieves 100% concept of abstraction, because interface must contain abstract method, not normal method. 

What is Dynamic Method Dispatch?

Method overriding is a disadvantageous fact, because in this concept only sub class property is coming as output, super class property is hidden. So to eliminate this disadvantage we need run-time polymorphism which is also known as dynamic method dispatch.
  • Dynamic method dispatch is a mechanism in which a call to an overriden method is reserved at runtime rather than compile time.
  • This is a mechanism that represents how java implement runtime polymorphism.
  • In this mechanism a super class reference variable can refer to a sub class object.
  • When an overriden method is called through a super class reference, java determines which method is executed and this is happened at runtime.
Example:

class Test
{
void display()
{
system.out.println("JavaRace");
}
}
class Test1 extends Test
{
void display( )
{
system.out.println("Silan Software");
}
}
class Test2 extends Test
{
void display( )
{
system.out.println("Silan Technology");
}
}
class Dispatch {
public static void main(String[] args)
{
Test obj1=new Test( );
Test1 obj2=new Test1( );
Test2 obj3=new Test2( );
Test r;
r=obj1;
r.display();
r=obj2;
r.display();
r=obj3;
r.display();
}
}

Output
JavaRace
Silan Software
Silan Technology