Saturday, 18 April 2015

Q. What is the O/P of the following---

 

  if two overloaded methods are:

 

class Test

{

    public static void main(String as[])

    {

        Test t=new Test();

        t.m1(null);

    }

    public void m1(Object obj)

    {

        System.out.println("m1 with obj");

    }

    public void m1(Customer c)

    {

        System.out.println("m1 with Cust");

    }

}

which method will be called and why?


Ans: Jvm will choose the most appropriate methods.Here, we need to know which class is more specialized, Object or Customer.The Customer class is the specialization of Object class.So Customer type method will be called.

 

Thursday, 16 April 2015

Q. What is an SDK ?



Ans: A software development kit (SDK) is typically a set of software development tools whch acts as a platform to start building and developing your application.It includes mainly three things---

1) An IDE (Integrated Development Environment):
it is code editor which allows you to write and edit code eg Eclipse.

2) Performance tools: These include tools for measuring performance, bug finding and also memory leak finding.

3) Build tools:You can use the build tools to build the code into binary package, which can be turned into an app which can then be uploaded.

SDKs also includes sample code and supporting technical notes or other supporting documentation.

Example:
1) iOS xcode for apple devices, visual studios for windows are popular Software Development Kits.

2) The JDK can be called as a subset of a software development kit (SDK).It includes "tools for developing, debugging, and monitoring Java   applications".
 

Tuesday, 14 April 2015

Q. What will happen if final is used and final is removed in the following code.


public class Test

{

    public static void main(String[] args) throws Exception

    {

        final boolean x=true;

        while(x)

        {

            System.out.println("AA");

           

        }

        System.out.println(("BB"));

       

    }}


Ans:If final is used with x then will get C.E  saying Unreachable Code because the condition will always be true. If final is removed then there is a chance that the value of x may be changed (false) and so there is a chance of reaching the statement after while loop and so compilation is successful.


Monday, 13 April 2015

Q. What is the difference between Green Thread and Native Thread?

Ans: Green threads are threads that are scheduled by a virtual machine (VM) instead of natively by the underlying operating system.Green threads emulate multithreaded environments without relying on any native OS capabilities, and they are managed in user space instead of kernel space, enabling them to work in environments that do not have native thread support.


Sunday, 12 April 2015

Q. If the super class implements serializable,then no need that the sub classes would also be implementing serializable interface,then why does HttpServlet class implements Serializable interface when the superclass that it extends, GenericServlet, has already implemented Serializable interface?

Ans: It is not compulsary that if the super class implements serialiable interface then sub class should also implement the same,it is a best practice to declare in the subclass that it also implements the same interface. This way if some one just reads the subclass he will have the complete picture.

Saturday, 11 April 2015

Q. How can we allow duplicate value in set ?



Ans: In the case of Set the hashcode of the object is used to find the place/index where the object will be stored.If somehow we can change the hashcode of the object then the index accessed by using the hashcode will be changed.So same object will be stored in the Set.


Example:
package com.manish.jlc;
import java.util.HashSet;
import java.util.Set;

public class Test1
{
    public static void main(String[] args)
    {
        Set st=new HashSet();
        Student st1=new Student(34);
        st.add(st1);
        st.add(st1);
        st.add(st1);
        st.add(st1);
        System.out.println(st); // [ 34 ]
        st1.age=35;
        st.add(st1);
        System.out.println(st); //[ 35, 35 ]
    }
}
class Student
{
    int age;

    public Student(int age) {
        super();
        this.age = age;
    }
    public int hashCode()
    {
        return this.age;
    }
    public String toString()
    {
        return String.valueOf(age);
    }

 

Wednesday, 25 March 2015

Difference between Encapsulation and Abstraction

Abstraction: Abstraction means representing the essential features without showing the background details.It lets you focus on what the object does instead of how it does.For example: Mobile phone has features calling,messaging etc.A user can call or message without knowing technical details that how a call is connected,how voice is transfered over the network etc.Thus, hiding internal implementation datails and just highlighting the set of services offered is called as Abstraction.

                        Abstraction in java is achieved through interface and abstract class and methods.


                              abstract class MobilePhone

                                {

                                     public abstract call(int number);

                                     public abstract sendMsg(int number,String msg);

                            }


Encapsulation: Binding data and its functionality together into a single unit so that it cannot be accessed directly from outside is called as Encapsulation.

For Example:I have an account in SBI bank.I went to the bank and ask my balance.They will happily tell me my balance.Again,if i ask them to change my balance without withdrawing or depositing any thing,Will they..?. No because i don't have direct access to the property balance.Balance is private member of account class and we cannot directly modify it without using public methods withdraw() or deposit().This is encapsulation.


           Encapsulation in java is achieved through class and access modifier (mainly private ).Hiding data behind the methods is the central concept of encapsulation.If a class has all its member variables private then it is called as tightly encapsulated class.

                            

                           class EncapsulationTest

                                 {

                                       private int a;                                           

                                       private int b;


                                       public int getA()  {    }

                                       public int getB()  {    }

                                       public int setA()  {    }

                                       public int setB()  {    }
                               }


Advantages:

                       1) Security

                       2) Enhancement will become easy.

                       3) Improves modularity of the application.


Note: In the  case of abstraction you are hiding the implementation details from the user and showing only the set of services offered whereas in the case of encapsulation you are hiding the data members from the rest of your code in the application.