Saturday, October 30, 2010

Basic Java Questions(Core Java Faq) part 1


1.What are the principle concepts of OOPS?
There are four principle concepts upon which object oriented design and programming rest. They are: 
Abstraction 
Polymorphism 
Inheritance 
Encapsulation (i.e. easily remembered as A-PIE).
2.What is Abstraction?
Abstraction refers to the act of representing essential features without including the background details or explanations.
3.What is Encapsulation? 
Encapsulation is a technique used for hiding the properties and behaviors of an object and allowing outside access only as appropriate. It prevents other objects from directly altering or accessing the properties or methods of the encapsulated object.

4.What is the difference between abstraction and encapsulation? 

Abstraction focuses on the outside view of an object (i.e. the interface)Encapsulation (information hiding) prevents clients from seeing it’s inside view, where the behavior of the abstraction is implemented. 
Abstraction solves the problem in the design side while Encapsulation is the Implementation. 
Encapsulation is the deliverables of Abstraction. Encapsulation barely talks about grouping up your abstraction to suit the developer needs.
5.What is Inheritance? 
Inheritance is the process by which objects of one class acquire the properties of objects of another class. 
A class that is inherited is called a superclass. 
The class that does the inheriting is called a subclass. 
Inheritance is done by using the keyword extends. 
The two most common reasons to use inheritance are: 
To promote code reuse 
To use polymorphism
6.What is Polymorphism? 
Polymorphism is briefly described as "one interface, many implementations." Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.
7.How does Java implement polymorphism? 
(Inheritance, Overloading and Overriding are used to achieve Polymorphism in java).
Polymorphism manifests itself in Java in the form of multiple methods having the same name. 
In some cases, multiple methods have the same name, but different formal argument lists (overloaded methods). 
In other cases, multiple methods have the same name, same return type, and same formal argument list (overridden methods).
8.Explain the different forms of Polymorphism. 
There are two types of polymorphism one is Compile time polymorphism and the other is run time polymorphism. Compile time polymorphism is method overloading.Runtime time polymorphism is done using inheritance and interface.
Note: From a practical programming viewpoint, polymorphism manifests itself in three distinct forms in Java: 
Method overloading 
Method overriding through inheritance 
Method overriding through the Java interface
9.What is runtime polymorphism or dynamic method dispatch? 
In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a super class. The determination of the method to be called is based on the object being referred to by the reference variable.
10.What is Dynamic Binding? 
Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance.
11.What is method overloading? 
Method Overloading means to have two or more methods with same name in the same class with different arguments. The benefit of method overloading is that it allows you to implement methods that support the same semantic operation but differ by argument number or type.
Note: 
Overloaded methods MUST change the argument list 
Overloaded methods CAN change the return type 
Overloaded methods CAN change the access modifier 
Overloaded methods CAN declare new or broader checked exceptions 
A method can be overloaded in the same class or in a subclass

12.What is method overriding? 
Method overriding occurs when sub class declares a method that has the same type arguments as a method declared by one of its super class. The key benefit of overriding is the ability to define behavior that’s specific to a particular subclass type.
Note: 
The overriding method cannot have a more restrictive access modifier than the method being overridden (Ex: You can’t override a method marked public and make it protected). 
You cannot override a method marked final 
You cannot override a method marked static 
13.What are the differences between method overloading and method overriding?

Overloaded Method
Overridden Method
Arguments
Must change
Must not change
Return type
Can change
Can’t change except for covariant returns
Exceptions
Can change
Can reduce or eliminate. Must not throw new or broader checked exceptions
Access
Can change
Must not make more restrictive (can be less restrictive)
Invocation
Reference type determines which overloaded version is selected. Happens at compile time.
Object type determines which method is selected. Happens at runtime.

14.Can overloaded methods be override too? 
Yes, derived classes still can override the overloaded methods. Polymorphism can still happen. Compiler will not binding the method calls since it is overloaded, because it might be overridden now or in the future.
15.Is it possible to override the main method? 
NO, because main is a static method. A static method can't be overridden in Java.


Wednesday, June 16, 2010

What is Java garbage collector


The JVM's heap stores all objects created by an executing Java program. Objects are created by Java's "new" operator, and memory for new objects is allocated on the heap at run time. Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. This frees the programmer from having to keep track of when to free allocated memory, thereby preventing many potential bugs and headaches.
The name "garbage collection" implies that objects that are no longer needed by the program are "garbage" and can be thrown away. A more accurate and up-to-date metaphor might be "memory recycling." When an object is no longer referenced by the program, the heap space it occupies must be recycled so that the space is available for subsequent new objects. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. In the process of freeing unreferenced objects, the garbage collector must run any finalizers of objects being freed.
In addition to freeing unreferenced objects, a garbage collector may also combat heap fragmentation. Heap fragmentation occurs through the course of normal program execution. New objects are allocated, and unreferenced objects are freed such that free blocks of heap memory are left in between blocks occupied by live objects. Requests to allocate new objects may have to be filled by extending the size of the heap even though there is enough total unused space in the existing heap. This will happen if there is not enough contiguous free heap space available into which the new object will fit. On a virtual memory system, the extra paging required to service an ever growing heap can degrade the performance of the executing program.
This article does not describe an official Java garbage-collected heap, because none exists. The JVM specification says only that the heap of the Java virtual machine must be garbage collected. The specification does not define how the garbage collector must work. The designer of each JVM must decide how to implement the garbage-collected heap. This article describes various garbage collection techniques that have been developed and demonstrates a particular garbage collection technique in an applet.

Why garbage collection?

Garbage collection relieves programmers from the burden of freeing allocated memory. Knowing when to explicitly free allocated memory can be very tricky. Giving this job to the JVM has several advantages. First, it can make programmers more productive. When programming in non-garbage-collected languages the programmer can spend many late hours (or days or weeks) chasing down an elusive memory problem. When programming in Java the programmer can use that time more advantageously by getting ahead of schedule or simply going home to have a life.


Monday, January 4, 2010

What is DOJO and its introduction


Dojo is an Open Source DHTML toolkit written in JavaScript. It builds on several contributed code bases (nWidgets, Burstlib, f(m)), which is why we refer to it sometimes as a "unified" toolkit. Dojo aims to solve some long-standing historical problems with DHTML which prevented mass adoption of dynamic web application development.

Dojo allows you to easily build dynamic capabilities into web pages and any other environment that supports JavaScript sanely. You can use the components that Dojo provides to make your web sites more usable, responsive, and functional. With Dojo you can build degradable user interfaces more easily, prototype interactive widgets quickly, and animate transitions. You can use the lower-level APIs and compatibility layers from Dojo to write portable JavaScript and simplify complex scripts. Dojo's event system, I/O APIs, and generic language enhancement form the basis of a powerful programming environment. You can use the Dojo build tools to write command-line unit-tests for your JavaScript code. The Dojo build process helps you optimize your JavaScript for deployment by grouping sets of files together and reuse those groups through "profiles".

Dojo does all of these things by layering capabilities onto a very small core which provides the package system and little else. When you write scripts with Dojo, you can include as little or as much of the available APIs as you need to suit your needs. Dojo provides multiple points of entry, interpreter independence, forward looking APIs, and focuses on reducing barriers to adoption.


Tuesday, December 22, 2009

New Releases - GlassFish v3 and Java EE 6



Wednesday, December 16, 2009

What is JAX-RS (Java API for RESTful Web Services)?


(JAX-RS) - RESTful Web Services

Lightweight RESTful approaches have emerged as a popular alternative to SOAP-based technologies for deployment of services on the Internet. However, development of such services with the Java platform is significantly more complex than development of SOAP-based services, due to the low-level nature of the current platform APIs.

The goal of the Java API for RESTful Web Services (JAX-RS) is to provide a high-level declarative programming model for such services that is easy to use and encourages development according to REST tenets. Services built with this API are deployable with a variety of Web container technologies and benefit from built-in support for best-practice HTTP usage patterns and conventions.
For more details see the link below :