MY mENU


Monday 19 March 2012

Constructor Overloading

constructor overloading
Constructor overloading is a technique in Java in which a class can have any number of constructors that differ in parameter lists.The compiler differentiates these constructors by taking into account the number of parameters in the list and their type. Examples of valid constructors for class Account are

Account(int a); Account (int a,int b); Account (String a,int b);

To understand Constructor Overloading

class Demo{
int value1;
int value2;
 /*Demo(){
  value1 = 10;
  value2 = 20;
  System.out.println("Inside 1st Constructor");
}*/
Demo(int a){
value1 = a;
System.out.println("Inside 2nd Constructor");
}
Demo(int a,int b){
value1 = a;
value2 = b;
System.out.println("Inside 3rd Constructor");
}
public void display(){
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
}
public static void main(String args[]){
Demo d1 = new Demo();
Demo d2 = new Demo(30);
Demo d3 = new Demo(30,40);
d1.display();
d2.display();
d3.display();
}
}
 Every class has a default Constructor. Default Constructor for class Demo is Demo(). In case you do not provide this constructor the compiler creates it for you and initializes the variables to default values. You may choose to override this default constructor and initialize variables to your desired values.

But if you specify a parametrized constructor like Demo(int a) ,and want to use the default constructor Demo(), it is mandatory for you to specify it.

In other words, in case your Constructor is overridden , and you want to use the default constructor , its need to be specified.

constructor chaining:
Consider a scenario where a base class is extended by a child .Whenever an object of the child class is created , the constructor of the parent class is invoked first. This is called Constructor chaining.

 To understand constructor chaining

class Demo{
int value1;
int value2;
Demo(){
value1 = 1;
value2 = 2;
System.out.println("Inside 1st Parent Constructor");
}
Demo(int a){
value1 = a;
System.out.println("Inside 2nd Parent Constructor");
}
public void display(){
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
}
public static void main(String args[]){
DemoChild d1 = new DemoChild();
d1.display();
}
}
class DemoChild extends Demo{
int value3;
int value4;
DemoChild(){
//super(5); 
value3 = 3;
value4 = 4;
System.out.println("Inside the Constructor of Child");
}
public void display(){
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
System.out.println("Value1 === "+value3);
System.out.println("Value2 === "+value4);
}
}
 Owing to constructor chaining , when object of child class DemoChild is created , constructor Demo() of the parent class is invoked first and later constructor DemoChild() of the child is created.

Expected Output =
Inside 1st Parent Constructor
Inside the Constructor of Child
Value1 === 1
Value2 === 2
Value1 === 3
Value2 === 4

 You may observe the constructor of the parent class Demo is overridden . What is you want to call the overridden constructor Demo(int a) instead of the default constructor Demo() when your child object is created ???  In such cases you can use the keyword "super" to call overridden constructors of the parent class.
Syntax:-
super(); --or-- super(parameter list);

Ex:    If your constructor is like Demo(String Name,int a)

you will specify super("Java",5)

If used ,the keyword super needs to be the first line of code in the constructor of the child class.

What is Server-Side Includes (SSI)?


Server-Side Includes allows embedding servlets within HTML pages using a special servlet tag. In many servlets that support servlets, a page can be processed by the server to include output from servlets at certain points inside the HTML page. This is accomplished using a special internal SSINCLUDE, which processes the servlet tags. SSINCLUDE servlet will be invoked whenever a file with an. shtml extension is requested. So HTML files that include server-side includes must be stored with a .shtml extension.

Platform Independent-Java

Why Java Is Platform Independent?

There are two tools which is for compliling and running java programs:

javac - it is compiler which converst java source code to byte code ie.class file. This byte code is standard for all platforms, machines or operating systems.

java - this is interpreter. this interprets the .class file based on a particular platform and excutes them.

jvm -> java virtual machine comes into play. jvm for windows will be different from jvm for solarais or linux . but all the jvm take the same byte code and executes them in that platform.

Source code -> javac ->Universal byte code

Universal byte ->jvm/java -> execute them on a particular machine.

There can be any number of jvm's but they all understand the one common language called byte code and translates them into the binary executable of a particular platform for which they are developed. There is separate development section in sun microsystems is to develop the jvm f or a particular platform. 
for each upgrade of the jdk , the jvm for each platform is updated.