Showing posts with label Demystified. Show all posts
Showing posts with label Demystified. Show all posts

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.

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".

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,

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.

Methods with Variable Number of Arguments

  • 0
     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)

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.

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.