Friday, March 18, 2011

Java Trivia - Take 1

True or false?
It is preferable to write
List dogs = new ArrayList();
instead of
List<Dog> dogs = new ArrayList<Dog>();
because raw types enable developers to avoid needless verbiage.

Answer: False.
While this may seem like needless verbiage, it's not, as Joshua Bloch argues: "By telling the compiler what kind of elements the list contains, you enable it to find many errors at compile time that would otherwise cause a ClassCastException at runtime. You also eliminate ugly casts from your program."

What is the difference between a Vector and ArrayList?

Vectors are synchronized. Any method that touches the Vector's contents is thread safe
ArrayList, on the other hand, is unsynchronized, making them, therefore, not thread safe.

What is a static member in Java? - OOD 101

The keyword 'static' preceding a variable(field) or a method makes its static.

static means, one per class and not one for each instance of the class (or object). This means that you can use them without creating an instance of a class.

Static variables (class variables) VS Instance variables:
When a number of objects are created from a class, each object gets gets its own copy of the instance variables.
Static variables are however associated with the class, rather than with any object of the class. Every instance of the class shares the same class variable, which is in one fixed location in memory. The class variable can be accessed without creating an instance of the class and any object can change the value of a class variable.

Static  methods (class methods) VS Instance methods:
Instance methods are associated with an object and use the instance variables of that object.
Static methods use no instance variables of any object of the class they are defined in. A Static method can only access static variables and cannot access the instance variables. Static methods typically take all they data from parameters and compute something from those parameters, with no reference to variables. This is typical of methods which do some kind of generic calculation. A good example of this are the many utility methods in the predefined Math class.

How do you create a thread in java?

(1) Extend the thread class and and override the run() method within the subclass to to define the code executed by the thread.

(2) Implement the Runnable interface and define the run() method.

Implement the Runnable Interface and then instantiate an object of the class.

We need to override the run() method into our class which is the only method that needs to be implemented.

The run() method contains the logic of the thread.

When creating threads, there are two reasons why implementing the Runnable interface may be preferable to extending the Thread class:

  • Extending the Thread class means that the subclass cannot extend any other class, whereas a class implementing the Runnable interface has this option.
  • A class might only be interested in being runnable, and therefore, inheriting the full overhead of the Thread class would be excessive. Further the class can implement multiple interfaces, in addition to Runnable.

What is the difference between an Interface and an Abstract Class?

Interface:
An interface is a collection of method prototypes, i.e. method signatures only, no body.
The class that implements the interface must provide an implementation to the methods in the interface.
So a interface is used to enforce a contract.

Abstract class:
An abstract class is one that has one or more abstract methods. An abstract method is one that has only a signature and no implementation. An abstract class can have implemented methods. An abstract class cannot be instantiated, but can be sub-classed/extended. The class that extends the abstract class must provide a definition for the abstract methods.

So, an abstract class is used to provide an initial implementation, that needs to be extended by a sub class. It is used to propagate 'a kind of' relationship.

What are the three pillars of OOD?

(1) Encapsulation:
The ability to combine attributes (data) and functionality (methods) into a single entity (class).

(2) Inheritance:
The ability to extend (subclass) a base class, i.e. the capability of a class to use the properties and methods of another class while adding its own functionality.

(3) Polymorphism:
From the biological term 'many forms'

The capability of an action or method to do different things based on the object that it is acting upon.

Example:

(i) Overloaded methods - methods with the same name but different signatures i.e. either a different number or type of parameters or a different return type.

(ii) Overridden methods - methods that exist in a base class and then redefined within an inherited or subclass. They have the same signature and the subclass definition is used i.e. the subclass definition overrides the definition in the base class.