May 05 2012

What does a passionate developer do…

Category: English posts,TechnicalIuliana @ 22:35

… when is stuck in a hotel room because of the rain, bored out of his mind and with no mood to work? Well when that happens to me I start to reed my java feed reader and depending on what I find there I might write my own articles. So… yes, this article is today’s consequence of boredom.

1. What is the flaw with the Stack class?
Actually, there are two of them all being caused by the fact that java.util.Stack extends java.util.Vector:
I. Extending Vector methods insertElementAt and removeElementAt can be called and they actually work, so the stack definition is not respected (that part with only the last inserted element being accessible, the LIFO principle)
II. Extending Vector, Stack is also synchronized which makes it slow and when synchronization is not necessary this  is quite inefficient. This is not exactly a flaw, it’s more of a personal observation observation.

Then again in the api it is written that “It extends class Vector with five operations that allow a vector to be treated as a stack “, so I guess these are not flaws, the Stack class just works as intended. (Recommendation: use ArrayDeque)

2. Can an interface extend multiple interfaces?
There is no right answer to this question, because it depends of the point of view of the interviewer.
I. Yes, because you can define an interface like this:
public interface MultipleIntf extends List, Serializable {
II. No, extending means actually inheriting all functionality of the super-entity and  perhaps adding new functionality, in the case of interfaces there is nothing to inherit and no functionality to add. Except for the obligation to implement all abstract methods that will be enforced on the implementing class.

3. What is lazy loading?
Lazy loading is a name to describe the process of not loading something (object/class) until needing it. This question will surely take you to a ClassLoader discussion, so it is better to know and understand the Java Class Loading mechanism.
– the java source files are compiled into executable code for the JVM, called bytecode, stored into *.class files.
– at start-up JVM has no loaded classes. When the first class is loaded, the classes on which its execution depends are searched and loaded too. So if I have a class which has imports statement for ArrayList and Serializable, the JVM will load my class, then it will search and load ArrayList.class and Serializable.class. Let’s imagine we have a big application with a lot of class files and one of them is missing. The application will run just fine, until we try to access a functionality implemented by that class, when the JVM will let us know that the class was not found by throwing a java.lang.ClassNotFoundException.
And this my darlings is lazy loading. A class is not loaded until used and there would be no point in doing that for efficiency reasons. Right? Anyway, if you want to have a deep understanding of the Java Class Loader, I recommend this article.

Tags: , , ,

Jan 19 2012

Java stuff that you should know: difference between abstract classes and interfaces

Category: English posts,Miscellaneous,TechnicalIuliana @ 17:14

Many times after a Java Interview I have asked myself if my answers were correct, if the person interviewing me realized that I understand a certain concept and if I was able to express my knowledge in order to achieve that. So I will start writing on my blog how I would answer some common Java Interview questions.

So, in Java what are the differences between an abstract class and an interface?
This is a question with one big answer, not only because there are that many differences between abstract classes and interfaces, but because there are so many ways to say it. So, here is what I would say about interfaces:

  1. Interfaces are equivalent to protocols. They basically represent an agreed-upon behavior (a contract) to facilitate interaction between unrelated objects.
  2. Interfaces are a capsule for a group of methods with empty bodies or prototypes (abstract methods ) and implicitly final static members (constants).
  3. Interfaces have all members public impilicitly.
  4. Interfaces are declared like this:
    public interface TestIntf {}
  5. Interfaces can only extend other interfaces and keyword implements does not apply to them.
  6. Interfaces can extend more interfaces.
  7. If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.
  8. Interfaces are considered to be slower because they require extra indirection to find the corresponding method in the class implementing them.
  9. Interfaces support only public and default access modifiers.
  10. Interfaces can be declared abstract, so the Java compiler would not complain if your declaration would look like below, but the abstract modifier is redundant as interfaces are implicitly abstract.
    public abstract interface TestIntf {}

Here is what I would say about abstract classes:

  1. Abstract Classes are the middle ground between interfaces and classes. They can have both abstract and non-abstract methods and constant and non-constant fields.
  2. Abstract Classes can have constructor, but it can never be called directly. It can only be called using super in the extending class or is automatically called by the jvm.
  3. Abstract Classes support all access modifiers.
  4. Abstract classes can be declared like this:
    [access_modifier] abstract class TestAC {  [ implementation]    }
  5. Abstract Classes can implement any number of interfaces, can extend only one class (which can be abstract or not – the condition is not to be final) and implicitly extend class Object.
  6. Abstract Classes can have only non-abstract methods (but what would be the point?). Abstract Classes which have only abstract methods and constant fields are called fully abstract.

And what would I say about both of them:

  1. Java supports multiple inheritance only through interfaces.(Some say it is limited. I say it is practical.) Why? Because extending multiple classes may lead to problems when they have methods with identical signatures. (Remember C?)
  2. The use of interfaces or abstract classes is determined by the project requirements and design.
  3. Using abstract classes affects the flexibility of your code, because class extension rips away class individuality.

If you have anything else to add, or something here needs correction, your effort will be kindly appreciated.

Tags: , ,