Java, MySQL, Avaya

Differences between abstract Class and Interface

Both try to achieve abstraction in Java but there are some fundamental differences between them:

  • An interface it’s an empty shell, there are only signatures of the methods, they are not implemented. The interface is just a pattern, a ‘contract’ established about what it can do without saying how to do it.
  • Abstract classes can have members, abstract methods (not implemented) and methods, using any visibility. Interfaces can have members and method declarations and all of them must be public.
  • A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same public visibility.
  • If we add a new method to an interface, that breaks the established contracts with all classes that implement it, so the method implementation will need to be added to all those classes. Adding a new method to an abstract class it’s not such a big deal, because we can always define default behaviour.

Maybe, I have confused you now, so probably best to look at very simple examples:

As you can see from the above, for an abstract class we can have abstract and non abstract methods living together, the only requirement is that the class has the abstract attribute in front of it.

With interfaces we know that the implementation of any of the methods is not allowed. So you should never see a ‘{ }’.


June 30, 2013 Posted by | Java | 1 Comment

What it means if you don’t override equals()

If you don’t override a class’s equals() method, you won’t be able to use those objects as a key in a hashtable and you probably won’t get accurate Sets, such that there are no conceptual duplicates.

The equals() method in class Object uses only the == operator for comparisons, so unless you override equals(), two objects are considered equal only if the two references refer to the same object.

Note the there are 2 things to be done in order to make a valid equality comparison.

  • Be sure that the object being tested is the correct type. It comes in polymorphically as type Object, so you need to do an instanceof test on it. Having two objects of different class types be considered equal is usually not a good idea.
  • Compare the attributes we care about (in this case, just moofValue)

Only the developer can decide what makes two instances equal. (For best performance, you’re going to want to check the fewer number of attributes.)

equals(), hashCode() and toString() methods are all public. The following would not be a valid override of the equals() method:

Remember to check the argument types as well, the following method is an overload, but not an override of the equals() method:

June 30, 2013 Posted by | Java | Leave a comment

Handling Exceptions

Exceptions is an elegant mechanism in Java for handling errors that produces organised error-handling code: exception handling. Which it allows us to keep the error treatment cleanly separated from the exception-generating code.

Exception means ‘exceptional condition’ and is an occurrence that alters the normal program flow. When an exceptional event occurs in Java, an exception is said to be ‘thrown’. The code responsible for doing something about the exception is called an ‘exception handler’ and it ‘catches’ the thrown exception.

Exception handling works by transferring the execution of a program to an appropriate exception handler when an exception occurs. An appropriate exception handler it can be a catch or a finally block. It’s mandatory that at least one of those blocks are present, we can have no more than one finally block.

It is important to pay attention to the exception hierarchy when we are building our exception handler going from the most specific to more general exceptions. The idea is, we are trying to catch the exception as accurate as possible from the beginning.

In the previous example if we swap the catch clause for FileNotFoundException with the handler for the IOException, the program will not compile!

Recommendation, try to avoid writing a single catchall exception, programming in this way defeats the design objective:

May 25, 2013 Posted by | Java | Leave a comment

Unreachable code

A typical question that we can expect at the exam is given a code that has no errors, try simulate the execution to find out what will be the score.

In those kind of questions, you don’t expect (at least I wasn’t expecting) the compiler to complain about a condition that can never be satisfied in order to execute certain code.

In this example, the fact that we are throwing an exception at the try block means that line 10 will never be executed. The Exception will be catch but we will never return to finish the rest of the try block.

May 24, 2013 Posted by | Java | Leave a comment

Check Java Heap Size and Free Memory

A very small Java program to query the Java Heap Size value and the current Free Memory.

To display the Memory Used we could simply do Heap Size – Free Mem.

May 31, 2012 Posted by | Java | Leave a comment