Showing posts with label Java Guidance. Show all posts
Showing posts with label Java Guidance. Show all posts

Thursday, 1 January 2015

What Are Exceptions?

An exception is an divergence from  an Application's routine or normal behavior, for example, app. try to open a nonexistent file and want to connect a unavailable remote PC.As we know the normal behavior of these tasks is that file is opened successfully and remote pc is connected perfectly.

Above mentioned Examples can not prevented but a workaround is possible,Simply provide code to detect that file is exist or not before doing any other operation on it(its make no sense to work on non existent entity)

Exceptions may occur due to poorly written code,i.e, a method may try to access nonexistent element this cause occur due to poor coded to access Array. This kind of exceptions can be prevented by correcting the code.

An Exception might occur and can not prevented  and there is no workaround for such Exceptions. for example,virtual machine in Java runs out of memory or it could not find a particular class file. This is exception is kind of ERROR. This must be corrected otherwise app. have to be terminate.

Error Code

Error codes are one of the ways to represent error in source code. For example, a method can return true on successful execution or return false on failure.Many developers used error codes method to handle exception.

if(//file exist)
{
// all code
}
else
{
//show msg to user that file is not found
}

in above example else part demonstrate Error Code.

Error codes is easy to ignore and many developers ignore it,for example, A programmer may ignore false return value that file does not exist but proceed to work on it! nonsense.
Thats Why Object Code new approach to handle exceptions developed

Object Code

This approach is Object Based. an Object is created when exception is thrown, this object includes information about what error led to exceptions.This detail is latter used to provide workaround.
Error codes are less useful then Objects.For example, Error codes return false or an non-zero integer on failure but what is meant by false? or integer? but object is more useful for providing Workaround for exception thrown.Error codes can't provide detail description about exception.

Wednesday, 31 December 2014

How to Declare Interfaces in Java?

Every class in java exposes an interface which is used by other classes to communicate with exposed class.The class exposed interface includes non private methods,constructors,fields that are optionally used by other class's objects to communicate with exposed class.

Java Formalize this concept by giving reserve word "interface". which is used to give a "type" without implementation .

How to Declare Interface?

interface is declared by writing a reserved word interface followed by valid java identifier name and followed by {} open and close braces

interface Drawable
{
      int RED=2;
      int BLUE=3;
      int BLACK=4;
      int void draw(int color);

}

I declared Drawable interface.You can precede interface with public to make it visible outside of its package.

how to Implement Interface:

Java provides a reserved word  "implements" to implement interface by any class or another interface

for example 

class Shape implements Drawable
{
    // all code goes here
 }

Shape class must override all methods of implemented interface until Shape is not abstract.

Important notes about interface:

  • an interface is abstract by default
  • its fields are public static final by default
  • its methods are public and abstract by default.
  • don't make methods static because interface methods are instance methods.