Multi-Threading by Implementing Runnable Interface

  • 0
     I have shared enough theory on Multi-Threading in my previous post. Now, it is time to share code for Multi-Threaded programs by implementing runnable interface.

Multithreading by Extending Thread Class

  • 1
Multi-threading is a topic, that I found most difficult to understand. First thing that needs to be understood is the distinction between a Thread and a Program. Thread is the smallest unit of execution. A program may contain more than one thread that executes concurrently. Let me tell you that saying concurrent execution is not correct, a computer processor can execute only one process at a time (single core processors), and if a process involves more than one Thread, only one Thread, would execute at a time, when a Thread gets paused, then and then only, any other Thread can start executing. If you ask what exactly is the advantage of multi-threading, then the advantage of multi-threading is that you can assign different tasks to Threads, for example, As in Android, Image Retrieval from database is time consuming and processor intensive task, so it is preferred to implement image retrieval mechanism on a separate thread.

Chained Exceptions in Java

  • 0
    Chained Exceptions were introduced in Java with JDK 1.4. In Chained exceptions, we can associate an exception with some other exception so that it can be inferred that occurrence of one exception is due to associated exception in reality.

Creating Your own Exceptions in Java

  • 0
     An exception is generally a run time error that leads to unwanted instant termination of the program. An exception must be handled, failing to do so leads to transfer of control of execution to Java's default exception handler. In this article, I would try to develop an exception which is generated if you use a special name "HERR".

Extended Interface in Java

  • 0
     Interfaces just like classes in Java, can be extended. A class implementing the outer interface must implement all the methods of the super class. A class implementing the sub interface must implement all the methods of sub interface as well as methods of super interface. If it fails to do so, compiler reports an error, that it should either implement the missing  method or declare itself as Abstract.

Nested Interface

  • 0
     Interfaces in Java,  just like classes  can be nested. An interface declared inside body of another interface is called nested interface. However, the two interfaces exists independently in terms of implemention. It means that one does not needs to implement the methods of both the interface if one implements only one of them. However, inner interface cant exist on its own,

Dynamic Stack in Java

  • 0
Stack is one of the basic data structure in any programming language. In this article, I would try to make you understand stack and then, I would share a Java program that implements fixed size stack. Stack is the structure that implements last in first out elimination technique. A stack has two operations that can be operated on it. They are, push and pop. In push method, we put an item on the top of the top and pop operation removes the item currently one the top of the Stack.

Stack in Java

  • 0
Stack is one of the basic data structure in any programming language. In this article, I would try to make you understand stack and then, I would share a Java program that implements fixed size stack. Stack is the structure that implements last in first out elimination technique. A stack has two operations that can be operated on it. They are, push and pop. In push method, we put an item on the top of the top and pop operation removes the item currently one the top of the Stack.

Incomplete Interface Illustration

  • 0
     Java provides classes a choice to to implement an interface completely or just partially. But, opting to implement an interface renders a class incomplete. It means that the class that implements an interface incompletely, must be declared Abstract. Any class that extends such class, must implement remaining methods of the interface or should declare themselves as Abstract too.

Interface Illustrated

  • 0
     Via interfaces, one can specify what a class must do, but not how it should do it. An interface is just like a class and is defined in almost same way.

     access interface_name
     {
          return_type method_name1(parameter_list);
          return_type method_name2(parameter_list);

          type final_var_name = value;
     }
    When no access specifier is given, access is understood to be default. Default access restricts the use of interface to the current package only.

Access Mechanism in Packages Demystified

  • 0
     Packages in java are both, a naming and a visibility control mechanism,. In this article, I will try to show the behaviour of access modifiers in different situations. We have 4 type of access in Java, they are public, private, protected and default. Out of these, only public and default can be used with classes. When no access modifier is explicitly written before a member, is is supposed to be in default access.

Dynamic Method Dispatch in Java

Dynamic Method Dispatch is the mechanism in which call to an overridden method is resolved at run time rather than at compile time. Dynamic Method Dispatch is related to a principle that states that an super class reference can store the reference of subclass object. However, it can't call any of the newly added methods by the subclass but a call to an overridden methods results in calling a method of that object whose reference is stored in the super class reference. It simply means that which method would be executed, simply depends on the object reference stored in super class object.

Abstract Class Illustrated

     An abstract class is the one having an abstract method in its definition. Abstract class puts a restriction that subclass must implement its own version of abstract methods. Programmer does not has the freedom to create object of an Abstract class. One can not declare abstract constructors or abstract static methods. One can not instantiate an Abstract class object with new operator. However, Java provides us the freedom to create an reference of Abstract class type. This reference can be used to store the reference of subclass object references.

Constructor Chaining in Java

  • 0
     In the professional language, A constructor is a subroutine which is called at the time of creation of object of a class. Well, Constructor is a very special method that does not has a return type, not even void. The job of this special method is to initialise the object with default values. If you don't create it explicitly, Java does it for you. This special method is called as soon as an instance of the class.

Methods with Variable Number of Arguments

     In my last article, I discussed how before the introduction of VarArgs, programmers had to deal with the requirement of methods. Here, I am going to discuss the VarArgs Methods, that allow you to deal with the requirement of variable number of arguments in a method. A variable length argument is specified by three periods. For example
static void VarMethod(int...v)

Super Keyword in Java

Super keyword is one of the most confusing an exciting features of Java. In this article, I will try to demonstrate the two uses of super keyword. The two uses of super keyword are
  1. to call the constructor of immediate Superclass of subclass. Subclass is the one who has has inherited, and Superclass is the one who has been inherited.
  2. to call and access hidden members of Superclass.

Old School Variable Argument Method

     Before introduction of Varargs(which is going to be our next topic of discussion in Java Journal), two appraoches were used to create methods with variable number of arguments. They are
First approach invloved the use of overloaded method. The method simply involved creating all poosible set of input arguments and define a separate method with same name. This approach gaurantees to push you onto edge.
Second approach was to use a single method with on;y one argument array of the required type. We will discuss this method in here. However, the problems associated with this approach was that each time, preparing for a call to a method, an array was supposed to be created. This method can be extremely complex and errorprone if number of calls are large.

Nested Class Concept

  • 0
      Java language allows nesting of classes. When one class in defined in another class, then, enclosed class is called nested class. A nested class can access the methods and variables of the enclosing class, even private members, like they all are members of its own.

Static Block Demystified

  • 0
     Static keyword in Java has unusual properties. A variable flagged as static acts like a global variable, a class maintains a single copy of the static variables in the memory which is shared by all the class instances or objects of that class. A method tagged as static can be called without the creation of class object at first. Being a static method also puts some restrictions on the method, for example, it can only access static variables, can call only static methods and can not refer to "this" or "super" keyword in any way. In this article, I will try to clear some concepts regarding Static Block.

Java Program to swap values of two numbers without temporary variable

A typical value swapping progran involves use of a temporay storage to store the value of either of the two variables whose values is to be swapped. In here, I would try to make you understand the true logic of the oprogrm. This program is simply based on the fact that java assignment statement, for example,
a = 1 +3;
would add first 1 and 3, after the addition, the value is stored in the left hand value of the storage variable. Now, the program swapping vales is.

How to return Objects from Methods

  • 0
      Java enables us to return objects from the methods as well. Returning and passing objects need to consider the  parent child relationship among the classes. This relationship holds importance because, as a matter of fact, Object of parent class can hold the address of object of child class.

Java program to check whether a number is palindrome or not

  • 0
Checking whether a number is palindrome or not, is one of the earlist classic problem in programming. A number is said to be palindrome if on reversing the number, we get the same number. For example, consider the number 121, on reversing we get 121. So, it is a palindrome number. Java program to check whether a number is palindrome or not is given.

Fibonacci series program in Java

Fibonacci series is one the most series es in the world. In a Fibonacci series, each term is the sum of last two terms. For the first two terms, 1st is taken as 0 and second is taken as 1. Fibonacci series has attracted a lot of attention and research. So. let us devote a JAVA program to print the Fibonacci series.

Java program to reverse a number

Java program to reverse a number is one of the simplest programming problems. The JAVA program that reverses a number is given.

How to use Objects as Parameter

     The first thing that anyone needs to know is the difference between argument and parameters. Arguments are the variables that are passed to the functions and parameters are the variables that receive the value of the arguments. Java rich Object Oriented Features allow us to use Objects as the parameter.

Recursive Factorial Program in Java

Recursion is the process in which a function calls itself. Generally, iterative programs are slower than corresponding iterative versions of the same program but recursion has found its use due to the fact that sometimes, it is easier to develop recursive version of the program than to develop a iterative version of the same program. The general condition for the recursive program is to have a condition that should fail at some point in the program and at that point, the program needs to traverse back to generate the final answer. Without this condition, your program may fall into an infinite loop, so, the program will never stop unless the stack of method calls is overflowed.

Factorial Program in Java

In mathematics, the factorial of the number is defined as

n! = n*(n-1)*(n-2)*(n-3)*(n-4)*...1

Method Overloading Demystified

  • 0
Method Overloading is one of the best features of the Object Oriented Language. With Method Overloading, Java implements Polymorphism.

Garbage Collector Program in JAVA

  • 0
Java language dynamically allocates the memory to the objects of the classes being used in the program. Unlike C++ where a destructor is needed to free the memory, JAVA employs Garbage Collector Mechanism to reclaim the allocated memory. When no referance to the object exists in the memory, it becomes eligible to be collected by Garbage Collector. The finalize method provided by java sees to it that this method is executed just before the memory allocated to the object is being reclaimed. There is no gurantee that a finalize method would be executed. My aim was to observe the functioning of the garbage collector. So, I created a program. And here it is.