About me

Gladson A
Software Engineer,Chennai.

Translate

Pages

Gladson G-World : Java Reference

G-WorlD : Technical Entertainer

Trends In Information Technologies

Photo Gallery Icons

Programes and Source Code

Saturday 30 June 2012

this( ) and super( ) in constructor


this() in constructor



-The this() call in a constructor invokes the an other constructor with the corresponding parameter list within the same class.


-Java requires that any this() call must occur as the first statement in a constructor.

program:

public class Cube2 {



int length;
int breadth;
int height;
public int getVolume() {
return (length * breadth * height);
}

Cube2() {

this(10, 10);
System.out.println("Finished with Default Constructor");
}

Cube2(int l, int b) {

this(l, b, 10);
System.out.println("Finished with Parameterized Constructor having 2 params");
}

Cube2(int l, int b, int h) {

length = l;
breadth = b;
height = h;
System.out.println("Finished with Parameterized Constructor having 3 params");
}

public static void main(String[] args) {

Cube2 cubeObj1, cubeObj2;
cubeObj1 = new Cube2();
cubeObj2 = new Cube2(10, 20, 30);
System.out.println("Volume of Cube1 is : " + cubeObj1.getVolume());
System.out.println("Volume of Cube2 is : " + cubeObj2.getVolume());
}
}

Output:



Finished with Parameterized Constructor having 3 params
Finished with Parameterized Constructor having 2 params
Finished with Default Constructor
Finished with Parameterized Constructor having 3 params
Volume of Cube1 is : 1000
Volume of Cube2 is : 6000

super() in constructor:



-The super() statement invokes a constructor of the super class.


- The super statement must be the first statement of the constructor.

program 1:

(Note Carefully : Every constructor calls its superclass constructor) 

class a

{

a()

{
System.out.println("this is super class constructor");
}
}

public class b extends a

{

b(int b)

{
System.out.println("this is  main class parameterized constructor:"+b);
}

b()


this(12);

System.out.println("this is  main class constructor");
}

public static void main(String[] a)

{
b s=new b();
}


}

output:

this is super class constructor
this is  main class parameterized constructor:12
this is  main class constructor

Constructor chain:

program 2:

class Cube {



int length;
int breadth;
int height;

public int getVolume() {

return (length * breadth * height);
}

Cube() {

this(10, 10);
System.out.println("Finished with Default Constructor of Cube");
}

Cube(int l, int b) {

this(l, b, 10);
System.out.println("Finished with Parameterized Constructor having
2 params of Cube");
}

Cube(int l, int b, int h) {

length = l;
breadth = b;
height = h;
System.out.println("Finished with Parameterized Constructor having
3 params of Cube");
}
}

public class SpecialCube extends Cube {



int weight;

SpecialCube() {

super();
weight = 10;
}

SpecialCube(int l, int b) {

this(l, b, 10);

System.out.println("Finished with Parameterized Constructor having
2 params of SpecialCube");
}

SpecialCube(int l, int b, int h) {

super(l, b, h);

weight = 20;
System.out.println("Finished with Parameterized Constructor having
3 params of SpecialCube");
}

public static void main(String[] args) {

SpecialCube specialObj1 = new SpecialCube();
SpecialCube specialObj2 = new SpecialCube(10, 20);
System.out.println("Volume of SpecialCube1 is : "
+ specialObj1.getVolume());
System.out.println("Weight of SpecialCube1 is : "
+ specialObj1.weight);
System.out.println("Volume of SpecialCube2 is : "
+ specialObj2.getVolume());
System.out.println("Weight of SpecialCube2 is : "
+ specialObj2.weight);
}
}

Output:


Finished with Parameterized Constructor having 3 params of SpecialCube
Finished with Parameterized Constructor having 2 params of SpecialCube
Volume of SpecialCube1 is : 1000
Weight of SpecialCube1 is : 10
Volume of SpecialCube2 is : 2000
Weight of SpecialCube2 is : 20



-This implies that this() and super() calls cannot both occur in the same constructor. Just as the this() construct leads to chaining of constructors in the same class, the super() construct leads to chaining of subclass constructors to superclass constructors.

                                       Ask your Forums and Questions in the Comment box (mention your main Id).

Constructor in JAVA


Constructor in JAVA


-Constructor initialize an object Of a class immediately upon creation.


-JAVA creates constructor call for every object that is created.


-When you create a new instance (a new object) of a class using the new keyword, a constructor for that class is called.

program 1:

public class contest {

contest()
{

System.out.print("this is constructor");
}

public static void main(String[] a)

{
constortest c=new constortest();

}


}

output:

this is constructor

-Constructors are similar to methods, but with some important differences.



1.Constructor name is class name. A constructors must have the same name as the class its in.


2.Default constructor is created only if there are no constructors. If you define any constructor for your class, no default constructor is automatically created.


3.If you don't define a constructor for a class, a default parameterless constructor is automatically created by the compiler. The default constructor calls the default parent constructor (super()) and initializes all instance variables to default value (zero for numeric types, null for object references, and false for booleans).


4.Constructor cannot return values.


5.cannot specify return type (not even void).

program 2:

public class constructor {

static int t=9;

public constructor(double y,int e)

{
System.out.println("these are two parameterized constructor:"+y+"   "+e);
}

public constructor(constructor e)

{
t=3;
System.out.println("this is parameterizer constructor passing object:"+t);
}

public constructor(int dd)

{
t=2;

System.out.println("this is parameterized constructor:"+t);

}

public constructor()

{
t=1;
System.out.println("this is parameterless constructor:"+t);
}

public static void main(String[] b)

{

constructor c1=new constructor();
constructor c2=new constructor(t);
constructor c3=new constructor(c2);
constructor c4=new constructor(12.7,13);
System.out.println("main function test:"+t);
                constructor.fuc();
       constructor c5=new constructor();
       constructor.fuc();
}

public static void fuc()

{
System.out.println("sub function test:"+t);
}
}

output:

this is parameterless constructor:1
this is parameterized constructor:2
this is parameterizer constructor passing object:3
these are two parameterized constructor:12.7   13
main function test:3
sub function test:3
this is parameterless constructor:1
sub function test:1

(NOTE: Next is about calling constructor from a constructor using keywords this() and super())

                       Ask your Forums and Questions in the Comment box (mention your main Id).




Method Overloading in JAVA


Method Overloading in JAVA

-Methods of same name can be declared in java in the same class,they have different set of parameters(number ,types,order of parameters).


-When an overloaded method method is called,JAVA compiler identify the appropiate method by using the parameters and it types,numbers and order of the arguments. 

program:

public class methodoverload { public static void main(String[] a)

{
int i=123; int j=13;
double d=13.9;

func();
func(i);
func(i,d);
func(d,i);
func(59,4,"gkads");

}

public static void func()

{
System.out.print("func\n");

}

public static void func(int i)

{
System.out.printf("func with one parameter:%d\n",i);

}

public static void func(int i,double d)

{
System.out.printf("func with two parameters order1:%d%10f\n",i,d);

}

public static double func(double d,int i)

{
System.out.printf("func with two parametrs order2:%f%10d\n",d,i);

return 0;

}

public static void func(double d,int i,String s)

{
System.out.printf("func with three parameters order1:%f%10d%10s\n",d,i,s);

}

public static void func(int j,int i,String s)

{
System.out.printf("func with three parameters order2:%d%10d%10s\n",j,i,s);

}




}

output:

func
func with one parameter:123
func with two parameters order1:123 13.900000
func with two parametrs order2:13.900000       123
func with three parameters order2:59         4     gkads


Ask your forums and question in the comment box(mention your mail ID).


Pass by Reference in java


Pass by Reference in java


-Passing the address or sending the copy of reference.


-If any changes in copied method ,it affect the original method.


-Pass By Reference is based on the object of a class or a object like

            int[] a=new int[10];           

program:

public class passarray {

public static void main(String[] a)
{
int[] array={1,2,3,4,5};

System.out.println("effect of passing reference to entire array:\n"+"the value of the original array are:");

for(int b:array)
{
System.out.printf("%5d",b);
}

modifyarray(array);
System.out.println("\nthe value of the modified array are:");

for(int b:array)
{
System.out.printf("%5d",b);
}

}

public static void modifyarray(int[] array2)
{
for(int count=0;count<array2.length;count++)
{
array2[count]*=2;

}

}


}

output:

effect of passing reference to entire array:
the value of the original array are:
    1    2    3    4    5
the value of the modified array are:
    2    4    6    8   10


Ask your Forums and Questions in the Comment box (mention your main Id).

public static void main(String[] a) in JAVA

public static void main(String[] a)

Why we are declaring "public static void main(String[] a)" in JAVA programs.What is the purpose of declaring like this.

program:

public class test1 
{
public static void main(String[] a)
{
System.out.print("java main function");
}

}

output:

java main function

public        

       -(access control:public) allows everyone can access this method.
       
       -Mainly function main(String[] a) contains "public" because,It be Accessible to the JVM(JAVA VIRTUAL MACHINE) to begin execution of the program.

static

      - you may know that you need an object instance to invoke any method.

      -But,this method is Static because it be available for execution without an object instance.

void     

      -Once the main method execution is over that means that program end or terminates.So this method should not return any value.

      -void will not return any value.Hence "void" is declaring in the function main(String[] a)

main(String[] a)

      -The parameter "String[] a" is used to signify that the user may like to enter parameters to the java program at command line. We can use both String[] a or String a[]. The Java compiler would accept both forms

Deep Explaination about main(String[] a):

          public class test1 
{
   
  public static void main(String[] a) 
{
     
    if ( a.length <= 1 ) 
   {
      System.out.println("type arguments in Command line");
    } 
   else if ( a.length == 2 ) 
    {
      System.out.println( "your commands, " + a[0] + " " + a[1] );
    }
     
  }
   
}


In the console, command prompt or terminal add arguments in command line like this

java test1 sun moon

output:

your commands,sun moon

  Ask your Forums and Questions in the Comment box (mention your main Id).






Gladson,Gladson G-WorlD,Java.










GLADSON A
M.C.A Student
INDIA.
gladsinfo@gmail.com