Thursday, 23 January 2014

Anonymous Class

An anonymous class in Java is a class not given a name and is both declared and instantiated in a single statement. You should consider using an anonymous class whenever you need to create a class that will be instantiated only once.
Although an anonymous class can be complex, the syntax of anonymous class declarations makes them most suitable for small classes that have just a few simple methods.
An anonymous class must always implement an interface or extend an abstract class. However, you don’t use the extends or implements keyword to create an anonymous class. Instead, you use the following syntax to declare and instantiate an anonymous class:
new interface-or-class-name() { class-body }
Within the class body, you must provide an implementation for each abstract method defined by the interface or abstract class. Here’s an example that implements an interface named runnable, which defines a single method named run:
runnable r = new runnable()
    {
        public void run()
        {
            //code for the run method goes here
        }
    };
Here are a few other important facts concerning anonymous classes:
  • An anonymous class cannot have a constructor. Thus, you cannot pass parameters to an anonymous class when you instantiate it.
  • An anonymous class can access any variables visible to the block within which the anonymous class is declared, including local variables.
  • An anonymous class can also access methods of the class that contains it.

Wednesday, 22 January 2014

Command to list the file name in the directory which contains the specific word

directory > grep word .*

CUT command in Unix

Cut command in unix (or linux) is used to select sections of text from each line of files.The cut command can be used to print characters in a line by specifying the position of the characters. 

1.To print the characters in a line, use the -c option in cut command

Example:
> cat file.txt
unix or linux
is unix good
is linux good

cut command to print characters by position:

cut -c4 file.txt
x
u
l

2.cut command to print characters by range
cut -c4-7 file.txt
prints the character from 4th position to 7th position.

cut -c-6 file.txt
prints  the character from first  upto 6th.

cut -c10- fil.txt
prints the character from 10th position to till the end.

Types of polymorphism

In java language, polymorphism is essentially considered into two versions.
  1. Compile time polymorphism (static binding or method overloading)
  2. Runtime polymorphism (dynamic binding or method overriding)
Here, it is important to understand the these divisions are specific to java. In context to software engineering, there are other form of polymorphisms also applicable to different languages, but for java, these are mainly considered.

Compile time polymorphism (static binding or method overloading)

As the meaning is implicit, this is used to write the program in such a way, that flow of control is decided in compile time itself. It is achieved using method overloading.
In method overloading, an object can have two or more methods with same name, BUT, with their method parameters different. These parameters may be different on two bases:
1) Parameter type: Type of method parameters can be different. e.g. java.util.Math.max() function comes with following versions:
public static double Math.max(double a, double b){..}
public static float Math.max(float a, float b){..}
static long Math.max(long a, long b){..}
public static int Math.max(int a, int b){..}
The actual method to be called is decided on compile time based on parameters passed to function in program.
2) Parameter count: Functions accepting different number of parameters. e.g. in employee management application, a factory can have these methods:
EmployeeFactory.create(String firstName, String lastName){...}
EmployeeFactory.create(Integer id, String firstName, String lastName){...}
Both methods have same name “create” but actual method invoked will be based on parameters passed in program.

Runtime polymorphism (dynamic binding or method overriding)

Runtime polymorphism is essentially referred as method overriding. Method overriding is a feature which you get when you implement inheritance in your program.
A simple example can be from real world e.g. Animal. An application can have Animal class, and its specialized sub classes like Cat and Dog. These subclasses will override the default behavior provided by Animal class + some of its own specific behavior.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Animal {
    public void makeNoise()
    {
        System.out.println("Some sound");
    }
}
class Dog extends Animal{
    public void makeNoise()
    {
        System.out.println("Bark");
    }
}
class Cat extends Animal{
    public void makeNoise()
    {
        System.out.println("Meawoo");
    }
}
Now which makeNoise() method will be called, depends on type of actual instance created on runtime e.g.
?
1
2
3
4
5
6
7
8
9
10
public class Demo
{
    public static void main(String[] args) {
        Animal a1 = new Cat();
        a1.makeNoise(); //Prints Meowoo
         
        Animal a2 = new Dog();
        a2.makeNoise(); //Prints Bark
    }
}

Important points:

  1. Polymorphism is the ability to create a variable, a function, or an object that has more than one form.
  2. In java, polymorphism is divided into two parts : method overloading and method overriding.
  3. Some may argue that method overloading is not polymorphism. Then what does the term compile time “polymorphism” means??
  4. Another term operator overloading is also there, e.g. “+” operator can be used to add two integers as well as concat two sub-strings. Well, this is the only available support for operator overloading in java, and you can not have your own custom defined operator overloading in java.