sabaTEL.net

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 ‘{ }’.

Advertisements

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

Switch your domain to SIP

Onsip is offering a nice feature for free that will allow you to use their SIP infrastructure masked under your domain.

The guide to setting this up can be found here and below is the check I carried out to test that the SIP service for my domain is now being resolved by onsip.com

It’s all free as long as you are phoning a SIP number, but they will charge per minute rate for all PSTN calls (both inbound and outbound) and they charge $2 per month per phone number.

February 1, 2012 Posted by | Linux, SIP | Leave a comment

AES health checker script

I’ve just updated my first version of a health check script for an Avaya Application Enablement Services server.

And this is the current output for the server where I’m testing it:

September 15, 2011 Posted by | Avaya, Python | Leave a comment

User Management

It is very important to maintain and improve the performance of your database when possible, but you should never forget about the security aspect either.

MySQL bases all its security access on the ‘mysql’ database itself. Below you can see the users that I have created and from which box they can access the database. And afterwards I check what the account ‘reader’ is allowed to do:

  • Able to connect to MySQL as long as the ‘reader’ logs in from the same machine ‘localhost’ with the password stored.
  • Execute any SELECT statement to any table of the ‘world’ database.
The list of privileges available at the moment for MySQL is:

August 20, 2011 Posted by | MySQL | Leave a comment

EXPLAIN

The EXPLAIN statement is a valuable tool in terms of tuning your database, but before you get too excited, it is important to pay attention to all it can offer you and its limitations:

  • Shows MySQL’s estimated query plan.
  • Only works for SELECT queries.
The EXPLAIN output columns are:

field : (possible values)  description
id : (number) which SELECT the row belongs to.
select type : (SIMPLE or COMPLEX : primary, subquery, derived, union, union result)
  • SIMPLE, No subqueries or unions.
  • PRIMARY, The outer query
  • SUBQUERY, Numbered according to the position in SQL text
  • DERIVED, Subquery in the FROM clause executed as a temp table
  • UNION, Rows are spooled into a table, then read out with a NULL id in a row that says UNION RESULT

table : (table name) the table accessed or its alias.
type : (ALL, index, range, ref, eq_ref, const, system, NULL) ordered from worst to best by the ‘join type’. How MySQL will access the rows to find results.
possible_keys : (table columns) which indexes looked useful to the optimizer, the indexes that can help make row lookups efficient.
key : (table columns) which indexes the optimizer chose, but that does NOT mean they will be the most efficient indexes, different criteria.
key_len : (number) the number of bytes of the index MySQL will use
ref : (const, column name) which columns/constants from preceding tables are used for lookups in the index named in the key column
rows : (number) estimated number of rows to read, NOT the number of rows in the result set!
extra : (text) Any additional information that help us to understand the execution plan used.
  • Using index: covering index
  • Using where: server post-filters rows from storage engine
  • Using temporary: an implicit temporary table (for sorting or grouping rows, DISTINCT)
  • Using filesort: external sort to order results

August 20, 2011 Posted by | MySQL | Leave a comment

Obtain the table storage format

As per usual, on MySQL, we have several ways to find this out.

Requesting it directly via the SHOW TABLE STATUS command:

The Row_format value is the one we are interested on, but we could obtain that information from the INFORMATION_SCHEMA also:

August 3, 2011 Posted by | MySQL | Leave a comment