Tuesday 24 November 2015

Exception Handling in Java 7 : The Pictorial Way

(Download One Minute Java Mobile app for latest updates)
In this post I am going to introduce you with java 7 modified try-catch block. I am not going to start from scratch, but if some one wants to understand basics of try catch block, do let me know, I will write a separate article. As there are multiple changes to try-catch block I will cover them in series of posts with one change per post.

Change 1 : 
try with multi catch block
As you can see in below image now you can reduce duplicate code by using multi-catch.




















Some rules of try with multi catch block
#Rule 1 :
Multi catch is only for exception with different inheritance hierarchy.






















#Rule 2:
You cannot re-assign value to catch parameter in multi-catch.









#Rule 3:
In Java 7 Exception will not handle all the exceptions, to understand this refer below image



Tuesday 10 November 2015

Garbage collection with example : The Pictorial Way

(Download One Minute Java Mobile app for latest updates)
Garbage Collector :
As the name suggest garbage collector helps to perform cleaning activity of discarded objects. Discarded objects are those objects which are not reachable by any live thread.

Below image will help you understand this better :


When is object eligible for garbage collection?
To answer this in one line, as soon as life of last variable referring to object ends, object become eligible for garbage collection.

Very obvious question will come to your mind, and when dose the life of variable ends?  to understand this check following example.


However in some cases, if method returns a reference of local variable, than even if the life of local variable ends, object will still not be eligible for garbage collection.

So to conclude, if we declare all our variables judiciously we need not want to worry about garbage collection, except in certain scenarios which we will discuss latter.

But however in some cases, if you wish to explicitly make object eligible for garbage collection you can take following steps.

How to explicitly make object eligible for garbage collection?
Below are some ways to explicitly make object eligible for garbage collection.

1. By making reference null

2. By setting a reference variable, refer to another object


What is "island of isolation" in java?

Please refer below image.


When does garbage collection starts cleaning process ?
Answer to this question is, there is no assured way when this cleaning process will start it varies from JVM to JVM and the underlying platform.

Can we force garbage collection?
We cannot force garbage collection, but we can request with the help of below methods. Once again we cannot invoke garbage collection by these methods, we are just requesting it whether to fulfil your request depends solely on JVM.

1. System.gc();
2. System.runFinalization();

The only method that claim to guarantee force garbage collection is  System.runFinalizersOnExit() but this method has many limitations and due to this it is deprecated.

Friday 6 November 2015

What is overloading method in java ? : The Pictorial Way

Overloading Method :

A method with same name but with different argument list is known as method overloading.
Below image will explain you in detail about method Overloading in java.

Method Overloading in Java
Rules for method Overloading in Java?
  1. Overloaded method name must be same.
  2. Overloaded method must have different argument list.
  3. Overloaded method can have different access specifier.
  4. Overloaded method can have different return type.
  5. Overloaded method can be in same class or different class.
  6. Overloaded method can declare a broader checked exception.
Why reference type determines which method to invoke in overloading ?

Have a look at below image and than read the explanation explanation following it.

Accessing Overloaded Method in Java

In overloading reference type determines, which method to invoke and not the object type, this is because method overloading is a compile time polymorphism and so method binding is done at compile time. Therefore at run time reference is pointing to which object dose not matter for overloaded method.


Why changing only a return type is not legal in overloading?
Why we cannot only change return type and keep the argument list same in overloading?

This is because in overloading compiler binds a method to instance based on the argument list, and if the argument list of two methods is same it will cause a ambiguity about which method to invoke.

When to use method overloading?
You can use method overloading when you don't want user to restrict on datatype of data he is passing as argument, or when you want to take different action depending on data passed by user.

For example:

Class int overloadedExample {
    public int add(int a, int b)
      {
              return a + b;
       }

  public String add(String a, String b)
      {
              return a + b;
       }
}

In the above example, when user will invoke add(10,20) he will get the addition of provided numbers, on the other-hand if user invokes add("Hello","World") he will get the concatenated string. 

Can we overload main method? 
Yes we can overloaded main method, but JVM will always invoke public static void main(String[] args).

Wednesday 4 November 2015

Rules of method overriding in java : The Pictorial Way

In the previous post we have seen what is method overriding and how we can access overridden methods. In this post we will cover what are the rules to override a particular method from superclass to subclass.

#Rule 1 : Argument list must exactly match


#Rule 2 : Return type should be same as of supper class method or it can be a covariant return type.

What is covariant return type?
Subtype of original return type is known as covariant return type

#Rule 3 : Access level cannot be more restrictive


Why derived class overriding method should not be more restrictive than base class method in java?
Suppose someone access Animal class and thinks eat() is a public method and so it is safe to use this method with Animal class reference, and if at runtime JVM binds Animal reference with Horse object and Horse object have eat() as private method about which the person who is accessing Animal class eat() is completely unaware, than program will crash so derived class overriding method should not be more restrictive than base class.


#Rule 4 : Overriding method cannot throw new or broader checked exceptions


Why derived class overriding method should not throw broader exception than base class?
The reason behind this is same as above explanation, if derived class throws broader exception and JVM binds the base class reference with derived class object, than the code, which is accessing base class method, will fail. 


#Rule 5 : You cannot override final and static method
Why static method cannot be overridden in subclass in java ?
This is because at run time while accessing the static method compiler will replace the instance with class name, and so it will always access the method to which reference is pointing and not to which object is pointing.
However though you cannot override a static method in subclass, you can always redline static method in subclass.







Sunday 1 November 2015

What is method overriding in java with example : The Pictorial Way

Overriding Method 

I have explained how to override a method in below image.

As I said, I like to explain things in pictorial way, which is compact and easy way to understand, so you will find very less text in my posts, but if at some point you don't understand any concept you can always ask question and I will be happy to help you.

In below diagram you will get answer to following questions.

  • What is Overriding in java with example? 
  • How to access overridden methods in java?

What is overridden method in java ? 
A method in subclass is called overridden method if it satisfies below conditions
  • Same method name as of in Super class
  • Same or broader access specifier
  • Same return type or covariant return type (I will explain covariant return type in next post)
  • Exactly same argument list.
Accessing overridden method in java :
As you can observe in image, no matter what is the reference type, Java will always consider the object to which the reference is pointing.

Why object type and not reference type determines which overridden method to invoke ?
This is because overriding method is a type of run time polymorphism, by this I mean the method binding is done at run time depending upon the object being referred  at that point of time.


How to invoke super class version of overridden method?
To invoke super class version you can use "super" keyword.
Suppose in our above example if you want to invoke first eat method of Animal class and than you want to continue with eat method of horse class; in this scenario you can change eat method of Horse class in following way.

public void eat()
{
super.eat();
//followed by Horse specific functionality

}

When to use overriding method?
Overriding is useful in following scenarios
  • If you want to add some functionality to the method existing in super class.
  • If you want to suppress the functionality of the method present in super class.


In the next post I will explain all the rules to override a method in java.