Wednesday, July 13, 2011

Operator overloading in java ?

i  am going to explain operator overloading  on the basis of following context

1-language-defined  operator overloading
2-user-defined operator overloading

c++ support user defined operator overloading   only and java support language -defined  operator overloading

Java have language -defined  operator over loading properties only for + operator  .

+ operator is used to  add two primitive type variable and also used to concatenation string variable .in String concatenation it first check both operand ,if any of these String then it perform concatenation .

Example :
int i =10,j=10 k;
k=i+;j;
System.out.println(k);  //Out Put :20
String a="Hello";
String b="India";
String c=a+b;
System.out.println(c);  //Out Put:HelloIndia

Friday, July 8, 2011

What is difference between throw and throws key words in java ?

1-throws
i-throws is used by method to pass this exception to caller method and caller method should have to  handler this exception .it is used with method signature .
ii-the "throws" keyword is used in the method signature to declare that this method may throw an exception of the type specified or a sub-type of the one specified


Example :
public void passException()  throws Exception
{
     
}

2-throw
i-suppose you want to throw new type of your created Exception or already exists Exception then we used throw .it is used with statement .
ii-the "throw" keyword is used in the code of a method to perform the action of throwing an exception the value must be a proper sub-type of one of the exception types declared in the 'throws' clause of the method's signature


Example:
public void userException () throws MyException
{
    throw new MyException();
}

Tuesday, July 5, 2011

what is use of ordinal method of Enum class?

ProtoType of method is following .
public final int ordinal()


Method Name :ordinal
Return Type :int


It is final so no one override this method


Doc Specification :
/** * Returns the ordinal of this enumeration constant (its position * in its enum declaration, where the initial constant is assigned * an ordinal of zero). * * Most programmers will have no use for this method. It is * designed for use by sophisticated enum-based data structures, such * as {@link java.util.EnumSet} and {@link java.util.EnumMap}. * * @return the ordinal of this enumeration constant */


Source Code :



package model;


public class Simple {
    public Simple() {
        super();
    }
enum Hello{     H1,H2;         }     public static void main(String[] args) {        Hello aa=Hello.H2;
        int i=aa.ordinal();
        System.out.println(i);
    }
}


the out put is :1
it just return the position of any enum variable .it starting point is zero same as Array .

Monday, July 4, 2011

private and final method overriding

I am going to explain the following case .

Case1:what happen when we override private method ?
Case2:what happen when we override final method ?
Case3:what happen when we override private with final method ?

Case1:what happen when we override private method ?
Explain :
Source code of Parent class
public class Parent {
public Parent() {
super();
}
private void hello(){
System.out.println("parent class");
}
}


Source code of Child class
public class Child extends Parent{
    public Child() {
        super();
    }
    private void hello(){
      System.out.println("child class method ");
    }}
Child class has same method but this is not called method overriding because the method is make private in Parent class .this is just a new method declaration .
SO do not confuse .compiler  does not complain you it run fine without any exception .

Case2:what happen when we override final method ?
Parent Class Code :
public class Parent {
    public Parent() {
        super();
    }
     public final void hello(){
      System.out.println("parent class");
    }
}
Child class Code :
  public class Child extends Parent{
    public Child() {
        super();
    }
    public  void hello(){
      System.out.println("child class method ");
    }
   }
If you going to run this it give you following compile time exception
Exception: hello() in model.Child cannot override hello() in model.Parent; overridden method is final
this time compiler know you are going to override final method which is not possible .so compiler  do not allow you  to redefine same method in child .
This is main difference between  private method and final method overriding.

Case3:what happen when we override private with final method ?
Parent Class :
public class Parent {
    public Parent() {
        super();
    }
     private final void hello(){
      System.out.println("Parent Class");
    }
}
Child Class :
public class Child extends Parent{
    public Child() {
        super();
    }
  private final void hello(){
    System.out.println("Child Class ");
  }
}
Here private is win over final .it means it's not going to give any compile  time exception .because we make Parent method as private which not inherit in Child method .if you define the method this way it meas you just redefine method again .Case 1 rule is applicable .