Saturday, October 20, 2012

Why do we have only public static final variables in interfaces?

Why do we have only public static final variables in interfaces?

Interfaces are protocol, templates. Classes implementing the interfaces, decide about the implementation
public: So that all the classes implementing the interface have access to everything inside class
static:So that the variables, methods inside interface belong to the interface
final:so that implementing classes don't modify the constants

Java passes all the values...as cally by value....not call by reference


Composition vs Inheritance in java


Sunday, October 14, 2012

Core Java Faq

1)  Difference between path and classPath in java 
2)  Dynamic Loading vs Static Class Loading in java 
3)  Overloading in java
4) Overriding in java
5) static variables in java
6) static methods in java
7) can static methods be overloaded
8) can static methods be overrided
9) can static methods be overloaded by non-static methods
10) can static methods be overrided by non-static methods
11)  static class

Saturday, October 13, 2012

Dynamic Loading vs Static Class Loading in java

Class in java can be loaded in two ways

Static means at compile time means using new operator.

Dynamic means using class.forname() a class is loaded dynamically during run time

so a decision to load a class dynamically at run time can be taken based on certain logic using property file etc.
getClass(); getName(); getDeclaredFields();
Instance can also be created using forName() method. It loads the class into the current class memory.  
it is similar like Reflection.

Difference between path and classPath in java

We always set a path and classpath while coding using java.

Path is for the OS to know where the java exe's are. by setting path you can execute java program from anywhere if not set then u need to go to bin directory of java and only then u can execute a java program.
Classpath is for the java program to know where the .class file are.

Classpath location points to where the .class file of your java programs are and /or the jar files (they contain the .class file used to code a specific functionality/usage)