Final Variable in Java PDF
Final Variable in Java PDF
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
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. }
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. }
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:
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.
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.
Output:
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
MKS
Output: 20 20 20
Wrapper to Primitive
Output: 3 3 3
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