Friday, March 9, 2012

Exception Rules in Java

It's just opposite in the case of constructors and methods. A overriding method cannot throw broader checked exceptions than the overridden method in the super class. But in case of constructors, subclass constructor can throw broader checked exception but not narrower checked exception of the super class constructor. This behavior is opposite as constructors are not overridden...

Thursday, March 8, 2012

How many ways we can create objects in Java

there are 4 ways
1) using new operator and default constructor
MyObject object = new MyObject();
2) using Class.forname
MyObject object = (MyObject) Class.forName(MyObject).newInstance();
3) using clone
MyObject object = anotherObject.clone();

4) using deserialization
ObjectInputStream inStream = new ObjectInputStream(anInputStream ); MyObject object = (MyObject) inStream.readObject();

Can we override a main method in Java

Yes, you can override a main method in Java
class Abc{
public static void main(String args[]){

System.out.println("Abc method ");
}
}
class Xyz extends Abc{
public static void main(String args[]){
System.out.println("Xyz method");
}

}
class Example{
public static void main(String args[]){
String S[]=new String[10] ;

Abc.main(S);
Xyz.main(S);
}
}

Language Basics

String default value is null and not "null"
Boolean default value is false
Float & double are same only float has a F literal in value.
Declaring int array

int myList [] = {4, 3, 7};
public abstract methods in interface
constants in interface

Wednesday, March 7, 2012

Final Variables and static

Final Variables need to be initialized when declared.

Class final variables can be initialized in static block.
Instance variables can be initialized in Constructor.

static final variables are constants.


Static methods can acess other static methods and static variables only.