Wednesday, August 17, 2011

Java Interview Notes Lesson 1

These notes below are aimed to be a quick reference to any one gearing for a Java Interview. I have broken the series in parts, and this is part one of the tutorial lesson.

Classes
  • A source code file can have only one public class.
  • If the source file contains a public class, the filename must match the public class name.
  • There can be more than one nonpublic class in a file.
  • Files with no public classes have no naming restrictions.
  • Classes can also be final or abstract, but not both at the same time.
  • Classes can have only public or default access.
    • A class with default access can be seen only by classes within the same package.
    • A class with public access can be seen by all classes from all packages

Local Variables
  •  Local variables can not have access modifiers, and final is the only modifier applicable for them.

Interface
  • Interface methods are by default public and abstract. Explicit declaration of these modifiers is optional. 
  • Interfaces can have variables declared which are by implicitly constants as they are always implicitly  public, static, and final.
Polymorphism
  • The reference variable's type (not the object's type), determines which methods can be called.
  • Object type (not the reference variable's type), determines which overridden method is used at runtime.

Some Pointer Notes:
  • Abstract class cannot be instantiated.
  • Final class cannot be subclassed.
  • Private instance variables and methods are not visible to subclass, so they cannot be inherited.
  • Final methods cannot be overridden in a subclass. 
  • Abstract classes have constructors that are called when a concrete subclass is instantiated.
  • Interfaces do not have constructors.

Thursday, August 11, 2011

Enabling Java in Ubuntu and Mozilla, Jar execution

Hi folks.

Today's Aim
  1. Enable Java in Ubuntu 11.
  2. Execute Applets in Mozilla in Ubuntu, adding the Java plug-in to Mozilla Firefox in Ubuntu.
  3. Execute Jar files directly in Ubuntu for which we do not have execute access.

I have been working on Java for a long time on Windows systems. Yesterday I was giving a try on Ubuntu and really felt the pain of getting Java up in Ubuntu. So I decided to dot my learning in this article.

Task 1: Enable Java in Ubuntu 11
Ubuntu comes with an open jdk for running Java. We need to install it, or else we may get it from Sun Microsystems (Now Oracle) also. I preferred the open-jdk as it was much easier to install and get running.

Steps:
  • Open Terminal
  • Type: java -version. This will give the list of packages containg java.I got the below listing on my machine.
                      $ java -version
                       The program 'java' can be found in the following packages:
                       * gcj-4.4-jre-headless
                       * gcj-4.5-jre-headless
                       * openjdk-6-jre-headless
                        Try: sudo apt-get install
  • Select the openjdk to be installed. My machine's screen text is below:
               $ sudo apt-get install openjdk-6-jre-headless
  • This will install Java in your Ubuntu machine.

Task 2: Execute Applets in Mozilla in Ubuntu, adding the Java plug-in to Mozilla Firefox in Ubuntu.
For enabling Java in Mozilla Firefox in Ubuntu so that it can run Java Applets, follow the below steps.
  • Open Terminal
  • Install icedtea6-plugin. The screen-text from my machine is:
                      $ sudo apt-get install icedtea6-plugin
  • This will install the icedtea6-plugin on your Ubuntu Mozilla Firefox and will enable you to run Java Applets.
 
Task 3: Execute Jar files directly in Ubuntu for which we do not have execute access.
This was the most tricky of all. I had a jar file which i made through my Windows login for which i do not have execute access on Ubuntu machine. I tried chmod, chown, manually changing the permissions, etc, but all failed. The trick that finally worked was with Terminal again.

Use the following command to execute jar files in Ubuntu from Terminal.
$ java -jar filename


The screentext from my machine is:-
$ java -jar HelloWorld.jar

the above jar is a Java Applet and it executed just perfectly fine on Ubuntu. I also heard about some software called Wine for the same purpose, but did not tried it, as my job got done with the Terminal.

I hope the article will be useful to my readers. Will keep posting on more updates from my-side on this Java blog.