Home | finalize( ) and Overloading Method

Chapter: Web or internet Programming : JAVA

finalize( ) and Overloading Method

Sometimes an object will need to perform some action when it is destroyed.

The finalize( ) Method:

 

Sometimes an object will need to perform some action when it is destroyed. The finalize( ) method has this general form:

 

protected void finalize( )

 

{

 

// finalization code here  }

 

Here, the keyword protected is a specifier that prevents access to finalize( ) by code defined outside its class.

 

Overloading Methods :

 

In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. Method overloading is one of the ways that Java supports polymorphism.

 

Overloading Constructors: In addition to overloading normal methods, you can also overload constructor methods. In fact, for most real-world classes that you create, overloaded constructors will be the norm, not the exception.

 

class box2{

 

double height; double depth; double width;

 

// this operator & constructor when 3 dimensions are known box2(double w, double d, double h){

 

this.width=w; this.height=h; this.depth=d;

 

}

 

//default constructor box2(){

 

height=width=depth=2;

 

}

 

double fun_volume_box(){

 

return width*height*depth;

}

 

}

 

 

public class consoverloading {

public static void main(String args[]){

 

double volume;

 

box2 mybox1=new box2(10,10,10);//calls parametrized constructor

 

box2 mybox2=new mybox2();//calls default constructor

 

volume=mybox1.fun_volume_box();

 

System.out.println("volume of mybox1 object is " + volume);

 

volume=mybox2.fun_volume_box(); System.out.println("Volume of mybox2 obj is " + volume);

 

}

}

 

 

We can pass the value to the member variable through the object. And also we can call the member function with the help of object. Here is an example for the above said statement.

 

class ree{ int x;

 

int square2(){

return x*x;

 

}

}

 

public class metho {

public static void main(String args[])   

{    

int number,squaredvalue;     

Scanner sr= new Scanner(System.in);

ree op=new ree();

System.out.println("enter the number");  

number=sr.nextInt();   

op.x=number;     

squaredvalue=op.square2();   

System.out.println("squared value is" + squaredvalue);

 

}

}


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : JAVA : finalize( ) and Overloading Method |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.