– public: accessible anywhere by anyone
– private: Only accessible within this class
– Protect: accessible only to the class itself and to it’s subclasses or other classes in the same package.
– Default: default access if no access modifier is provided. Accessible to all classes in the same package.
Q: What is the difference between Abstract and Interface? When should we use any of these two?
Ans: An abstract class is a class which may have the usual flavors of class members (private, protected, etc.) with some abstract methods. An abstract class can have instance methods with default behavior. A class can extend only one abstract class. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. Methods are implicitly public and variables are implictly public static Final. Which one to choose depends on the design requirement.
Ans: HashMap is unsynchronised while HashTable is synchronised but we can synchronise the HashMap with use of Collections.synchronizedMap(new HashMap()); HashMap permits null values in it, while Hashtable doesn’t. HashTable has method which Returns an enumeration of the values in hashtable which is not fail safe. But there is no such method in HashMap.
Ans: Checked exceptions are subclass’s of Exception excluding class RuntimeException and its subclasses. Checked Exceptions forces programmers to deal with the exception that may be thrown. Example: Arithmetic exception. When a checked exception occurs in a method, the method must either catch the exception and take the appropriate action, or pass the exception on to its caller.
Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are Unchecked exceptions, compiler doesn’t force the programmers to either catch the exception or declare it in a throws clause. In fact, the programmers may not even know that the exception could be thrown. Example: ArrayIndexOutOfBounds Exception. They are either irrecoverable (Errors) and the program should not attempt to deal with them, or they are logical programming errors. (Runtime Exceptions). Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be.
Q: What is use of static for Main method and what will happen if we remove static keyword from signature?
Ans: Static method can be accessed without creating any object. In this way main method provides the entry point of the program. If we remove static keyword it will not be access by JVM and program can not be started.
Ans: In Java everything except primitives are object. These primitives don’t get the benefit of being object. Wrapper classes are created to provide the same to primitives. Integer class is wrapper for int primitive.
What is the Java Virtual Machine? What is its role?
Java was designed with a concept of ‘write once and run everywhere’. Java Virtual Machine plays the central role in this concept. The JVM is the environment in which Java programs execute. It is software that is implemented on top of real hardware and operating system. When the source code (.java files) is compiled, it is translated into byte codes and then placed into (.class) files. The JVM executes these byte codes. So Java byte codes can be thought of as the machine language of the JVM. A JVM can either interpret the byte code one instruction at a time or the byte code can be compiled further for the real microprocessor using what is called a just-in-time compiler. The JVM must be implemented on a particular platform before compiled programs can run on that platform.
Java has powerful features. The following are some of them:-
Portable (Platform Independent)
Abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.
Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation.
* Hides the implementation details of a class.
* Forces the user to use an interface to access data
* Makes the code more maintainable.
Inheritance is the process by which one object acquires the properties of another object.
Polymorphism is the existence of the classes or methods in different forms or single name denoting different implementations.
With extensive set of routines to handle TCP/IP protocols like HTTP and FTP java can open and access the objects across net via URLs.
One of the powerful aspects of the Java language is that it allows multiple threads of execution to run concurrently within the same program A single Java program can have many different threads executing independently and continuously. Multiple Java applets can run on the browser at the same time sharing the CPU time.
Java was designed to allow secure execution of code across network. To make Java secure many of the features of C and C++ were eliminated. Java does not use Pointers. Java programs cannot access arbitrary addresses in memory.
Automatic garbage collection is another great feature of Java with which it prevents inadvertent corruption of memory. Similar to C++, Java has a new operator to allocate memory on the heap for a new object. But it does not use delete operator to free the memory as it is done in C++ to free the memory if the object is no longer needed. It is done automatically with garbage collector.