Monday, 26 December 2011

Important Java FAQ's

1.What is an Abstraction?
Ans: Abstraction is a process of defining model objects which are not existing phisically, and also by defining(using access specifiers) we can hide the properties/methods of object from the other objects.

[OR]

Abstraction is a process of defining properties and methods of an Object.and also hiding the properties/methods of object from the other objects.

2.What is Encapsulation
Ans: It is a process of grouping properties and methods in the form of an Object, so that it is not possible to access the complete information of that particular object, due to this we can provide partial security using "private" access specifier. If we want to provide complete security we have to use "private" + JAAS[JAVA AUTHENTICATION AND AUTHORIZATION SERVICE].

3.What is different between encapsulation and abstraction
Ans:There is a thin line difference b/t these two things, i.e., Abstraction defines the Structure of Object and Encapsulation groups the properties and methods which leads to data hiding in encapsulation.

4.What is Polymorphism
Ans: Its general meaning is "Many Forms", it says about defining and redefining the methods in many forms. Here we define methods in many forms by Understanding Overloading concept and redefining methods in many forms by Understanding Overriding Concept.

5.what is Overloading
Ans: Overloading is a process of defining a method with same name for multiple purposes. Here we implement this by differentiating two things
 a)the type of the arguments that are passing to the method and
 b)the number of the arguments that are passing to the method.

6.what is Overriding
Ans: Overriding is a process of redefining a super-class method in the sub-class. If we make call to super class method using sub-class object, it executes redefined method in sub-class. If we want to execute super-class method then we should create super-class object.

Ex:-
class A{
 void methodA(){
    System.out.println("methodA()");
 }
}
class B extends A{
 void methodA(){
    System.out.println("methodA()");
 }
 void methodB(){
    System.out.println("methodB()");
 }
}
public class PolyTest {
 public static void main(String args[]){

  A a1=new A();
  a1.methodA();//Here A class methodA() will be executed

  A a2=new B();
  a2.methodA();//Here B class methodA() will be executed

  B b=new B();
  b.methodA();//Here also B class methodA() will be executed
 }
}



7.diff b/n Overriding and Overloading
Ans: According to me there is no comparision between them.
 Specifically to say
 In overloading there should be atleast one difference in type of the arguments or the number of arguments that are passing to method definition.
 In overriding the signatures and type of the arguments should be same in both the classes.

8.what is Interface? and what are the default access specifers and modifiers associated to properties and methods?
Ans: An Interface is a Class which is purely abstract. Here by default all the properties are Constants it means public, static and final
and the methods are abstract it means public and abstract.
So no need to explicitly specify them.

9.what is Interface?and what are the default access specifers and modifiers associated to properties and methods?
Ans:

10.how may Access Specifiers and Modifiers are there in java?
Ans:
Access Specifiers : public,private,protected or default
Access Modifiers : abstract, final, native, static, synchronized, transient, volatile and strictfp

11.Explain about Synchronization
Ans: Synchronization is a process of providing lock at method level or block level, by this we can control the concurrent access of that particular method or block. Generally In multi-threaded applications we use this technique, so that we can allow only one thread to access that method/block at a time.


12.Explain about Transient?
Ans: Transient modifier is used for variables, so that JVM does not allow these variables data to serialize or to persist.

13.Explain about Volatile
Ans: Volatile modifier is user for variable, so that JVM does not create Cache memory for these variables and it creates memory for these variables only on main memory.