Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
128 views

Final Variable in Java PDF

1. Final variables, methods, and classes cannot be overridden or modified once declared. The finalize() method is called before an object is garbage collected and can be used for cleanup. 2. Method overloading allows methods to have the same name but different parameters. Overriding allows subclasses to provide their own implementation of a method declared in the parent class. 3. Wrapper classes like Integer and Double allow automatic conversion between primitives and their corresponding object types. Java uses call by value, so changes to a parameter inside a method do not affect the variable passed in.

Uploaded by

Monoj Sur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views

Final Variable in Java PDF

1. Final variables, methods, and classes cannot be overridden or modified once declared. The finalize() method is called before an object is garbage collected and can be used for cleanup. 2. Method overloading allows methods to have the same name but different parameters. Overriding allows subclasses to provide their own implementation of a method declared in the parent class. 3. Wrapper classes like Integer and Double allow automatic conversion between primitives and their corresponding object types. Java uses call by value, so changes to a parameter inside a method do not affect the variable passed in.

Uploaded by

Monoj Sur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

JAVA notes

Final variable, method, and class and finalize()

Example of final variable

There is a final variable speedlimit, we are going to change the value of this variable, but It can't
be changed because final variable once assigned a value can never be changed.

1. class Bike9{
2. final int speedlimit=90;//final variable
3. void run(){
4. speedlimit=400;
5. }
6. public static void main(String args[]){
7. Bike9 obj=new Bike9();
8. obj.run();
9. }
10. }//end of class

Output:Compile Time Error

Java final method

If you make any method as final, you cannot override it.

1. class Bike{
2. final void run(){System.out.println("running");}
3. }
4.
5. class Honda extends Bike{
6. void run(){System.out.println("running safely with 100kmph");}
7.
8. public static void main(String args[]){
9. Honda honda= new Honda();
10. honda.run();
11. }
12. }

Output:Compile Time Error

Java final class

If you make any class as final, you cannot extend it.


MKS
1. final class Bike{}
2.
3. class Honda1 extends Bike{
4. void run(){System.out.println("running safely with 100kmph");}
5.
6. public static void main(String args[]){
7. Honda1 honda= new Honda();
8. honda.run();
9. }
10. }

Output:Compile Time Error

Is final method inherited?

Ans) Yes, final method is inherited but you cannot override it. For Example:

1. class Bike{
2. final void run(){System.out.println("running...");}
3. }
4. class Honda2 extends Bike{
5. public static void main(String args[]){
6. new Honda2().run();
7. }
8. }

Can we declare a constructor final?

No, because constructor is never inherited.

finalize() method

The finalize() method is invoked each time before the object is garbage collected. This method
can be used to perform cleanup processing. This method is defined in Object class as:

protected void finalize(){}

MKS
Note: Garbage collection is performed by a daemon thread called Garbage Collector(GC). This
thread calls the finalize() method before object is garbage collected.

1. public class TestGarbage1{


2. public void finalize(){System.out.println("object is garbage collected");}
3. public static void main(String args[]){
4. TestGarbage1 s1=new TestGarbage1();
5. TestGarbage1 s2=new TestGarbage1();
6. s1=null;
7. s2=null;
8. System.gc();
9. }
10. }

Output object is garbage collected


object is garbage collected

Method Overloading

1. To call an overloaded method in Java, it is must to use the type and/or number of
arguments to determine which version of the overloaded method to actually call.
2. Overloaded methods may have different return types; the return type alone is insufficient
to distinguish two versions of a method. .
3. When Java encounters a call to an overloaded method, it simply executes the version of
the method whose parameters match the arguments used in the call.
4. It allows the user to achieve compile time polymorphism.
5. An overloaded method can throw different exceptions.
6. It can have different access modifiers.
7. class Overload
8. {
9. void demo (int a)
10. {
11. System.out.println ("a: " + a);
12. }
13. void demo (int a, int b)
14. {
15. System.out.println ("a and b: " + a + "," + b);
16. }
17. double demo(double a) {
18. System.out.println("double a: " + a);
19. return a*a;
20. }
21. }
22. class MethodOverloading
23. {

MKS
24. public static void main (String args [])
25. {
26. Overload Obj = new Overload();
27. double result;
28. Obj .demo(10);
29. Obj .demo(10, 20);
30. result = Obj .demo(5.5);
31. System.out.println("O/P : " + result);
32. }
33. }

Output:

a: 10
a and b: 10,20
double a: 5.5
O/P : 30.25

Method Overriding

Child class has the same method as of base class. In such cases child class overrides the parent
class method without even touching the source code of the base class. This feature is known as
method overriding.

public class BaseClass


{
public void methodToOverride() //Base class method
{
System.out.println ("I'm the method of BaseClass");
}
}
public class DerivedClass extends BaseClass
{
public void methodToOverride() //Derived Class method
{
System.out.println ("I'm the method of DerivedClass");
}
}

public class TestMethod


{
public static void main (String args []) {
// BaseClass reference and object
BaseClass obj1 = new BaseClass();
// BaseClass reference but DerivedClass object
BaseClass obj2 = new DerivedClass();
// Calls the method from BaseClass class
obj1.methodToOverride();
//Calls the method from DerivedClass class
obj2.methodToOverride();
}
MKS
}

Output:

I'm the method of BaseClass


I'm the method of DerivedClass

Wrapper class in Java

Wrapper class in java provides the mechanism to convert primitive into object and object into
primitive.

Since J2SE 5.0, autoboxing and unboxing feature converts primitive into object and object into
primitive automatically. The automatic conversion of primitive into object is known and
autoboxing and vice-versa unboxing. One of the eight classes of java.lang package are known as
wrapper class in java.

Primitive to Wrapper

1. public class WrapperExample1{


2. public static void main(String args[]){
3. //Converting int into Integer
4. int a=20;
5. Integer i=Integer.valueOf(a);//converting int into Integer
6. Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally
7.
8. System.out.println(a+" "+i+" "+j);
9. }}

MKS
Output: 20 20 20

Wrapper to Primitive

1. public class WrapperExample2{


2. public static void main(String args[]){
3. //Converting Integer to int
4. Integer a=new Integer(3);
5. int i=a.intValue();//converting Integer to int
6. int j=a;//unboxing, now compiler will write a.intValue() internally
7.
8. System.out.println(a+" "+i+" "+j);
9. }}

Output: 3 3 3

Call by Value and Call by Reference in Java


There is only call by value in java, not call by reference. If we call a method passing a value, it is known
as call by value. The changes being done in the called method, is not affected in the calling method.

1. class Operation{
2. int data=50;
3.
4. void change(int data){
5. data=data+100;//changes will be in the local variable only
6. }
7.
8. public static void main(String args[]){
9. Operation op=new Operation();
10.
11. System.out.println("before change "+op.data);
12. op.change(500);
13. System.out.println("after change "+op.data);
14.
15. }
16. }

Output:before change 50
after change 50

MKS
MKS

You might also like