Week Three Tutorial

Polymorphism
Type Casting ← Back Week Two Tutorial Goto Week Four Tutorial→
Abstract Classes
Interfaces
Packages
Exceptional Handling

Polymorphism in Java

'Poly' meaning many and morph or morphos meaning forms or shapes, many forms or Polymorphism comes from Greek word. So, Polymorphism is an aspect of having different forms and ability to perform multiple tasks. In Java Programming, same method or an object or variable can perform different tasks making programming efficient and flexible. When we talk about Polymorphism, we talk about two most popular types: a) overriding b) overloading. Dynamic method binding is also part of it. Let's take an example: We have Calculation to be make of x=0.2/5 and data type is I/D (Int/Double). The java compiler will make calculation based on what is larger ratio data type (D) and converts and result would be double. If we don't want to be converted to larger ratio data type, we can use a method called a Cast Operator. Here is an example.

Double a = 10.2D;
Int x = (int) a;
Because we have specified a variable as Int type in the second line, it will not convert to double rather it will convert into INT data type. What we've seen here is 'a' exists in two data type forms, one as double and another as Int. This is also called data type conversion. Conversion is an explicit change in the data type which is specified by the cast operator. When we call methods, we may use same method names but function may be different.
Dynamic Polymorphism
When polymorphism is performed at runtime, it is called Dynamic Polymorphism. Java compiler does not know what method is called at the compilation time. Only Java Virtual Machine (JVM) knows which method should be run at runtime. This is also called Dynamic Binding or Runtime Polymorphism. We will write a sample program called 'MyPoly' for Dynamic Polymorphism.

public class MyPoly 
{
    void add(int a, int b)
    {
     System.out.println("Sum of Two Num" + (a+b));
    }
   void add(int a, int b, int c)
    {
    System.out.println("Sum of Three Num" +(a+b+c));
    }
} 
public class DemoPoly 
{
    public static void main(String arg[])
    {
        MyPoly mp = new MyPoly(); //Creating MyPoly object
        mp.add(10, 20); //this method call goes to first method
       mp.add(10, 20, 30); //calling second method
    }
}
In the above code snippet, we have two identical method names 'add'. Even though they have same names they perform different tasks based on their bodies as first method attempts to add two numbers whereas second number attempts to add three numbers. Now, these methods are executed at runtime as object is created at runtime and methods comes after the creation of the object. JVM picks the method based on the signatures of the methods. Methods Signatures are method name and the parameters.
a) Difference in number i.e in the following example, if we call add(10, 12), it will pick up first method as it got two int data type.
void add(int x, int y) // will be called first
void add(int x, int y, int z) called second

b) Difference in data type i.e. if we call a method add(3d, 10d), it will call second method
void add(int x, float b)
void add(double x, double y)

c) difference in order (sequence) i.e. if we call add(3.9, 10), JVM will executes second method first
void add(int x, float b)
void add(float x, int b)
These are the key reasons that triggers which method is called first

Method Overloading
Two methods having the same names in a class but having different Method Signatures is called Method Overloading. Method Overloading recognizes methods separately by looking at the difference in the method signatures such as difference in no. of parameters or data types of the parameter or in sequence parameters. So, Dynamic Polymorphism is an example of Method Overloading. If programmer wants to have two same methods names and same method signatures within a same class, it is not possible. But what is possible is that we need two have two separate classes to have this kind of scenarios such that a super class and subclass can have two same method names with the same method signatures.

Method Overriding
Two methods having same method names and with the same method signatures but in different classes as in Super Class and Sub Class, is called Method Overriding. So when a method in a subclass has the same method names and type signatures as in its super class, the subclass is said to override that method.

void sum(int a)  //in super class
void sum(int a) //in sub class
//super class
public class A 
{
    void sum(int a)
    {
    System.out.println("Addition value is: "+(a+a));
    }
}
//sub class
public class B extends A{
    void sum(int a)
    {
    System.out.println("The square of x is: "+ Math.sqrt(a));
    }
}
public class ABDemo {
    public static void main(String arg[])
    {
     B b = new B();
    b.sum(100);
    }
}

Output result
The square of x is: 5
So Super Class method is overridden by the sub class method as JVM calls only subclass method if its overridden to super user method. Let's see following chart to see detailed differences between Method Overloading & Method Overriding.

Method OverloadingMethod Overriding
Having two or more same method names but with different method signaturesHaving two or more same method names and same method signatures
Method Overloading done in the same classMethod Overriding is done in super class and sub class
Method Overloading is done for Code RefinementMethod Overriding is done for Code Change/replacement
Method Overloading is to extend already existing featureMethod Overriding is to present different implementation or body
Method return type can be same or differentMethod return type should be same

Static Polymorphism
Static Polymorphism, also called Compile time polymorphism, is performed at compilation time. Java Compiler knows what methods to call and compile and bind together. What we used in Dynamic Polymorphism is Instance Methods. We can also use Static Methods to implement method overloading and method overriding of dynamic polymorphism. Static Method as well as Private and final methods are example of Static Methods. They are called static because they maintain only a single copy in memory that will be available and shared by all the object of a class. Static Method is also called class method as Static method is used by class rather than objects of the class. Let us see following code snippet.

//super class
public class A {
   static void sum(int a)
    {
        System.out.println("Addition value is: "+(a+a));
    }
}
//sub class
public class B extends A{
   static void sum(int a)
   {
        System.out.println("the square of x is: "+ Math.sqrt(a));
   }
}
public class ABDemo {
 public static void main(String arg[])
     {
     A a = new B();
     a.sum(100);
     }
}

Output result
Addition value is: 200
In the above sample code snippet, super class method is called. We created super class reference to sub class object, as A a = new B();. Here 'a' is super class reference variable and using this variable, we called sum() method as a.sum(100);. If we use sub class reference as B b = new B();

Private methods are also used while programming in Java. Private methods are not accessible outside the class. It can be used only within a class. It is not even available for sub class. Private method can be used in Method Overloading.

Final Methods can also be used to program as it is declared as final specifier. Final method cannot be overridden like Private Method. So Method Overloading can only be used for final methods. Programmers believe that using 'final' method will enhance in the coding. Let us see a code snippet and find out.

//super class
public class A {
    final void sum1()
    {
        System.out.println("Hello There.");
    }
}
//sub class
public class B {
    void sum2()
    {
       A.sum1();  //calling method 'sum1'
    }
}

Here, what JVM does is copies the whole thing of Method 'sum1' to method 'sum2'. Copying code like this what we called is Inline Operation. The purpose the final method is that first thing is performance will be better and secondly, other programmers cannot override this method. So, overall private method and final method are the same. They both behave the same as they both cannot be overridden, both are used in method overloading. Both cannot be accessible by other programmers.

final can also be used to declare class as final class. Final class prevents inheritance which means sub class cannot built out of final class. For example:

final class X  //final class X is declared
class Y extends X  //invalid statements
final is used to declare constant value such as
 final int PI = 3.14159;
//variable PI is constant

Type Casting in Java

When we convert one kind of data type to another data type, this is what we call Type Casting in Java. Java compiler always looks for uniformity when we assign a value to a variable. If data type is not the same, it will be converted to become the same on the both sides of. We use Cast Operator for converting. Primitive data type hold single value whereas Reference data type, also called Advance data type, can hold two or more values. Primitive data type can be converted into another primitive data type by using casting. But Primitive data type cannot be converted into referenced data type by using casting. We need to use class wrapper for this function.

Primitive data type are of two types. Higher data types and Lower data types. Higher type holds more memory than Lower type which holds lesser memory. Let us look at the following chart beginning with the lower data type to higher data type.

Byte'Short'Char'Int'Long'Double'Float
Int x = 100;
Float sal = (float) x;   //now it converts int to float as 100.0.
We can change from lower to higher and vice versa. We converted int type x into float x. When we convert from lower to higher, it is safe as there will not be loss of any data. And even though we forget to mention casting, Java compiler automatically does this job for us, this is also called Implicit casting. Such as if we type
Int x =100;
Float sal = x;
Java compiler does conversion for us in this case.

Higher data type can also be converted into lower data type as in the following examples:

double x = 100.078;
int sal = (int) x;   // converted into int value (100) 
As we discussed earlier converting from lower data type into higher is safe where as converting data from higher to lower is risky as we see in the above example that while converting from double to int data type, we have lost precision (.078) chopping to only 100 values. Converting data type from Higher to lower is called Explicit Casting

Referenced Data Type can also be converted. We can take a class, which is a referenced data type, to convert into another class type but these classes should fall into same category, meaning some relationship as in super class and sub class. Such that we can convert Tiger class to Cat class.

package org.javaSansar.tutorial;
public class UpCastA 
{
    void display1()
    {
       System.out.println("This is super class");
    }
}
public class UpCastB extends UpCastA
{
    void display2()
    {
       System.out.println("This is sub class");
    }
}
public class UpCastTest 
{
    public static void main(String arg[])
      {
        UpCastA a = (UpCastA)new UpCastB();//a is referring to subclass reference here.
       a.display1(); 
     }
}

Output result
This is super class

Abstract Classes in Java

Abstract Class contains one or more abstract methods. An abstract class cannot be instantiated. If we create this code: AbsClassA a = new AbsClassA();, this will generate a compile error. A concrete class extends all the abstract methods. If we write only a method NOT method body i.e. add();, it becomes an abstract method and the class this method belongs to also becomes an abstract class. Abstract methods are called by sub classes/objects. The purpose of this is to create a super method/class and call them and avoid time and memories. Now, we can create a sub class and call the abstract class/method and give the body according to its need. So abstract methods are declared when two or more subclasses are needed similar role in different ways via different implementation. Abstract method is a method that does not have body whereas an Abstract Class is a class that contains these incomplete abstract methods. Let us take a look following code snippet code to be clearer on this.

package org.javaSansar.tutorial;
public abstract class MyAbstractClass  //abstract class declared with abstract keyword
{
    abstract void calculate(int x);//abstract method
}
public class SubClassA extends MyAbstractClass{
    void calculate(int x)
    {
       System.out.println("Addition= "+ (x+x));
    }
}
public class SubClassB {
    void calculate(int x)
    {
       System.out.println("Multiplication= "+ (x*x));
    }
}
public class SubClassC {
    void calculate(int x)
    {
       System.out.println("Subtraction= "+ (x-x));
    }
}
public class AbstractClassTest {
    public static void main(String args[])
    {
     //creating three objects
    SubClassA a = new SubClassA();
    SubClassB b = new SubClassB();
    SubClassC c = new SubClassC();
    //calling methods
    a.calculate(5); //Addition
    b.calculate(10); //Multiplication
    c.calculate(15);//Subtraction
    }
}

The output is:
Addition= 10
Multiplication= 100
Subtraction= 0

Interfaces in Java

An Interface is sort of contract which defines a standard way of specifying the behavior of classes. Methods that come into an Interface are all abstract and obviously these methods don't have body in them. A sub class or concrete class is needed to implement Interfaces. Interface is a kind of class that contains only abstract methods as opposed to abstract class that may contain abstract method and concrete method. Only method prototypes are written in Interfaces. Object cannot be created out of Interfaces. So, we have to create separate classes where we can implement all the methods of the interfaces. We call these, implementation classes. Interface is important because it is used as an encapsulation, such that to reveal the functionalities of an object without revealing its implementation. Implementation can change without affecting the caller of the interfaces.

An example of an interface is a credit card. You have got a visa or master card. You can make purchases by swiping it in USA (dollar) or in England (pound) or in Canada (Canadian dollar). No matter what currencies have been used, it makes possible to make transaction and does all the currency conversion for you. So a credit card can be good example of an Interface or contract which fulfills the need of purchases/transactions regardless of locations or currencies. An Interface in Java sort of works as multiple inheritances. So a class can implement multiple interfaces but it can extend only one class. Interfaces have no direct relationship to any particular class. They are independently defined.

public interface CreditCardInterface
{
  void purchaseDollar();//buy in dollar
  void purchasePound(); //Buy in pound 
}
public class BankA implements CreditCardInterface
{
    public void purchaseDollar()
    {
        System.out.println("Buy in dollor");//
    }
    public void purchasePound()
    {
        System.out.println("Buy in pound");
    } 
}
public class BankB implements CreditCardInterface 
{
    public void purchaseDollar()
    {
        System.out.println("Buy in dollor.");//
    }
    public void purchasePound()
    {
        System.out.println("Buy in pound.");
    }
}
public class InterfaceTest {
    public static void main(String arg[])
     {
     //More logics/statement can be used to adjust the buying
     //power of each currency, such as if location is USA, call purchaseDollar() method.
     BankA a = new BankA();
     a.purchaseDollar();
     BankB b = new BankB();
     b.purchasePound();
     }
}

Multiple Inheritance advantage can be achieved by using interfaces such as in below code

interface A
{
     double x = 10;
     void method1();
}
interface B
{
     double x =15;
     void method2();
}
class TestClass implements A, B
{
     //statement/logics
} 

Packages in Java

In Java, we can organize group of classes and interface in one place for efficiency and flexibilities. These files are organized in one directory or folder called a package. Java contains lot of built in classes organized in packages. java.io.*; is one of the example of package in java which contains number of very useful classes within this package.
In the above example, 'java' is a directory and 'io' is sub directory within it. '.*' brings all the available classes and sub classes and interface of the package. Similar other classes within java folder are java.lang, java.awt. In java, there are already available classes to be used making it easy and saving time for users and put together in one folder. These sort of packages/classes are called Built-In-Classes/packages. And the one user creates are called User-Defined-Packages/classes. 'java.io', 'java.lang', 'java.awt', 'java.util' are built-in-classes for example.

To import packages from java, we have to use following import commands.

import java.io.*;
import java.awt.event.*;

Java programmer can create user defined packages and these can be imported just like Built-in-packages. We will discuss how to create a package and use them in the following tutorials.

package pack.nettools; //creating a package  and storing MyClass class in it.
public class MyClass
{ 
    void nameDisplay()
    {
       System.out.println("My Name Is David MacCain);
    }  
}

Now, we can compile above code using following command:

C:\> javac -d . MyClass.java
'-d' creates a package called 'pack' and sub package called 'nettools' and places MyClass.class file in it.
'.' dot tells compiler that package should be created in the current directory
Now, if we want to use MyClass class, we tag this class with a membership operator(.) as
pack.nettools.MyClass
Let us import above package class in another program:

import pack.nettools.MyClass; //importing a package  for another class.
public class MyClassDemo
{
     Public static void main(String args[])
        {
            MyClass mc=new MyClass(); 
           mc.nameDispaly();
       }
  
}

Exceptional Handling in Java

Usually there are two types of error in java we mostly face
A) Compile-time errors: Compile error occurs when there are some syntax errors. Such that missing semicolon (;), missing parenthesis'()', etc.


public class CompError 
{
    public static void main(String args[])
       {
          System.out.println("My Compile Errors")
      }
}

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
 Syntax error, insert ";" to complete BlockStatements

B) Run-time errors:

When there is some system error such that out of memory, then run-time errors will occur.


public class CompError {
    public static void main()
       {
       System.out.println("My Run-Time Errors");
      }
}

Exception in thread "main" java.lang.Error: java.lang.NoSuchMethodError: main

C) Logical Error:

Logical errors occur when programmer or designers use wrong logic to programming. This sort of errors some time are undetectable which causes defect or bugs in the application/systems.

D) Exception Error:

Exceptions errors are generated in the run-time errors. If error occurs in compile-time by java compiler, they are sometime also called 'checked exception' but they occur only in few times. And if these errors are detected in run-time by JVM, they are sometime referred to 'unchecked exception'. How to handle Exception? Let us talk about it in below tutorial.

public static void main(String args[]) throws IOException
'IOException' is an example of checked exception. What above code does is throws out exception from main() method without handling it. We are not handling but throwing or bypassing exception here. To handle Exception by writing try block. It looks as follows:
try{
 statement;
     }
What above Exception handling code does is that even if some exception occurs, the program will not be terminated or corrupted. But JVM stores all the exception details in stack/log and bypasses it and move to catch block. catch block looks as follows:
catch (Exceptionclass ref)
 {
  statements;
 }
'ref' indicates reference which helps to generate exception stack. By using
System.out.println(ref);
, detail exceptions can be generated.

Finally, a programmer should clean up by using following command:

finally{
 statements;
 }
'finally' block will clean up all the unnecessary files and running threads. This is required to include in the snippet code. So, try, catch, finally blocks is what we used to handle Exception Handling.Let us write a code snippet to understand Exception Handling.
public class ExceptionClass 
{
 public static void main(String args[])
     {
     try
     {
       //opening the files
      double n = args.length;
      System.out.println("n= "+ n);
      double x = 100/n;
      System.out.println("x= "+ x);
     }
     catch(ArithmeticException ae)
     {
       //displaying exception stack details
      System.out.println(ae);
      //User message
      System.out.println("Please enter value i.e. 5 to avoid errors.");
     }
     finally
     {
       //cleaning of files and threads
      System.out.println("Cleaning up files/threads.");
     }
 }

}

Exception can be of two types. Built-in exception and User-defined exception. Some of the Built-in-exceptions are: ArithmeticException, ArryIndexOutOfBoundsException, FileNotFoundException, NoSuchFieldException, IOException etc.

No comments:

OTN Discussion Forums: Message List - Database - General

Ask Tom HOT ARTICLES

 
© Copyright 2008 - 2011. All rights reserved to dba-sansar.blogspot.com.
All other blogs, posts and entries are the property of their respective authors.
Please email us for your comments and suggessions Click Here