Thursday, December 6, 2012

Java/J2EE Job Interview

My title

Java/J2EE Job Interview
Q 01: Give a few reasons for using Java? LF DC
A 01: Java is a fun language. Let's look at some of the reasons:
  • Built-in support for multi-threading, socket communication, and memory management (automatic garbage collection).
  • Object Oriented (OO).
  • Better portability than other languages across operating systems.
  • Supports Web based applications (Applet, Servlet, and JSP), distributed applications (sockets, RMI. EJB etc)
  • and network protocols (HTTP, JRMP etc) with the help of extensive standardised APIs (Application Program Interfaces).

Q 02: What is the difference between C++ and Java? LF
A 02: Both C++ and Java use similar syntax and are Object Oriented, but:
  • Java does not support pointers. Pointers are inherently tricky to use and troublesome.
  • Java does not support multiple inheritances because it causes more problems than it solves. Instead Java supports multiple interface inheritance, which allows an object to inherit many method signatures from different interfaces with the condition that the inheriting object must implement those inherited methods. The multiple interface inheritance also allows an object to behave polymorphically on those methods. [:)]
  • Java does not support destructors but rather adds a finalize() method. Finalize methods are invoked by the
  • garbage collector prior to reclaiming the memory occupied by the object, which has the finalize() method. This means you do not know when the objects are going to be finalized. Avoid using finalize() method to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these resources through the finalize() method.
  • Java does not include structures or unions because the traditional data structures are implemented as an object oriented framework.
Q 03: What are the advantages of Object Oriented Programming Languages (OOPL)? 
A 03: The Object Oriented Programming Languages directly represent the real life objects like Car, Jeep, Account,
Customer etc. The features of the OO programming languages like polymorphism, inheritance and
encapsulation make it powerful. [Tip: remember pie which, stands for Polymorphism, Inheritance and
Encapsulation are the 3 pillars of OOPL]
Q 04: How does the Object Oriented approach improve software development? DC
A 04: The key benefits are:
􀂃 Re-use of previous work: using implementation inheritance and object composition.
􀂃 Real mapping to the problem domain: Objects map to real world and represent vehicles, customers,
products etc: with encapsulation.
􀂃 Modular Architecture: Objects, systems, frameworks etc are the building blocks of larger systems.
Java
15
The increased quality and reduced development time are the by-products of the key benefits discussed above.
If 90% of the new application consists of proven existing components then only the remaining 10% of the code
have to be tested from scratch.
Q 05: What is the difference between an abstract class and an interface and when should you use them? 
A 05: In design, you want the base class to present only an interface for its derived classes. This means, you don’t want
anyone to actually instantiate an object of the base class. You only want to upcast to it (implicit upcasting, which
gives you polymorphic behaviour), so that its interface can be used. This is accomplished by making that class
abstract using the abstract keyword. If anyone tries to make an object of an abstract class, the compiler prevents
it.
The interface keyword takes this concept of an abstract class a step further by preventing any method or function
implementation at all. You can only declare a method or function but not provide the implementation. The class,
which is implementing the interface, should provide the actual implementation. The interface is a very useful and
commonly used aspect in OO design, as it provides the separation of interface and implementation and
enables you to:
􀂃 Capture similarities among unrelated classes without artificially forcing a class relationship.
􀂃 Declare methods that one or more classes are expected to implement.
􀂃 Reveal an object's programming interface without revealing its actual implementation.
􀂃 Model multiple interface inheritance in Java, which provides some of the benefits of full on multiple
inheritances, a feature that some object-oriented languages support that allow a class to have more than one
superclass.
Shape
Circle Square
CircleOnSquare
Diamond problem & use of interface
No multiple inheritance in JAVA
Circle Square CircleOnSquare
<>
ShapeIF
<>
CircleIF
<>
SquareIF
Multiple interface inheritance in JAVA
Abstract class Interface
Have executable methods and abstract methods. Have no implementation code. All methods are abstract.
Can only subclass one abstract class.
A class can implement any number of interfaces.
Can have instance variables, constructors and any
visibility: public, private, protected, none (aka package).
Cannot have instance variables, constructors and can have
only public and none (aka package) visibility.
When to use an abstract class?: In case where you want to use implementation inheritance then it is usually
provided by an abstract base class. Abstract classes are excellent candidates inside of application frameworks.
Abstract classes let you define some default behaviour and force subclasses to provide any specific behaviour.
Care should be taken not to overuse implementation inheritance as discussed in Q8 in Java section.
Java
21
When to use an interface?: For polymorphic interface inheritance, where the client wants to only deal with a type
and does not care about the actual implementation use interfaces. If you need to change your design frequently,
you should prefer using interface to abstract. CO Coding to an interface reduces coupling and interface
inheritance can achieve code reuse with the help of object composition. Another justification for using interfaces
is that they solve the ‘diamond problem’ of traditional multiple inheritance as shown in the figure. Java does not
support multiple inheritances. Java only supports multiple interface inheritance. Interface will solve all the
ambiguities caused by this ‘diamond problem’.
Design pattern: Strategy design pattern lets you swap new algorithms and processes into your program without
altering the objects that use them. Strategy design pattern: Refer Q11 in How would you go about… section.