Final Class and Methods
Final Class and Methods
A final class is a class that is declared as a final which cannot have a subclass
Example:
final class A
{
int a;
A(int x) {a=x;}
void display()
{
System.out.println("a = "+ a);
}
}
class B extends A
{
int b;
B(int x,int y)
{
super(x); this.b=y;
}
void display()
{
System.out.println("b = "+ b);
}
}
class FinalClass
{
public static void main (String args[])
{
A objA= new A(10);
B objB= new B(100,200);
objA.display(); objB.display();
}
}
Output:
C:\>javac FinalClass.java
FinalClass.java:11: error: cannot inherit from final A class B
extends A ^
1 error
12. Passing Arguments by Value and by Reference
Arguments are the variables which are declared in the method prototype to receive the values as a
input to the Method( Function).
Example:
int add(int a, int b) // method prototype
{
//Body of the method add
return a+b;
}
Here a and b are called as arguments. ( also called as formal arguments)
Arguments are passed to the method from the method calling Ex:
int x=10,y=20; add( x , y); // method calling
Here x and y are actual arguments.
Arguments can be passed in two ways
i. Call by value
ii. Call by reference
i. Call by value
In call by value actual arguments are copied in to formal arguments. Example:
class Swap { int a,b;
void setValues(int p, int q)
{
a=p; b=q;
}
void swapping()
{ int temp; temp =a; a=b; b=temp;
}
void display()
{
System.out.println("In Swap Class: a= "+a+" b= "+b);
}
}
class CallByValue
{
public static void main (String args[])
{
int x=10,y=20;
System.out.println("Before Swap : x= "+x+ " y="+y);
Swap obj =new Swap(); obj.setValues(x,y); obj.swapping();
obj.display();
System.out.println("After Swap : x= "+x+ " y="+y);
}
}
Output:
C:\>javac CallByValue
C:\>java CallByValue
Before Swap : x= 10 y=20
In Swap Class: a= 20 b= 10
After Swap : x= 10 y=20
Suggestion: If you are beginner to java, lookup only three usages of this keyword.
This keyword can be used to refer current class instance variable. If there is ambiguity
between the instance variables and parameters, this keyword resolves the problem of
ambiguity.
Let's understand the problem if we don't use this keyword by the example given below:
1. class Student{
2. int rollno;
3. String name;
4. float fee;
7. name=name;
8. fee=fee;
9. }
10. void display(){System.out.println(rollno+" "+name+" "+fee);}
11. }
12. class TestThis1{
17. s2.display();
18. }}
0 null 0.0
0 null 0.0
In the above example, parameters (formal arguments) and instance variables are same. So, we
are using this keyword to distinguish local variable and instance variable.
1. class Student{
2. int rollno;
3. String name;
4. float fee;
7. this.name=name;
8. this.fee=fee;
9. }
10. void display(){System.out.println(rollno+" "+name+" "+fee);}
11. }
12.
17. s1.display();
18. s2.display();
19. }}
Test it Now
Output:
If local variables(formal arguments) and instance variables are different, there is no need to
use this keyword like in the following program:
1. class Student{
2. int rollno;
3. String name;
4. float fee;
7. name=n;
8. fee=f;
9. }
10. void display(){System.out.println(rollno+" "+name+" "+fee);}
11. }
12.
17. s1.display();
18. s2.display();
19. }}
Test it Now
Output:
You may invoke the method of the current class by using the this keyword. If you don't use
the this keyword, compiler automatically adds this keyword while invoking the method. Let's
see the example
1. class A{
2. void m(){System.out.println("hello m");}
3. void n(){
4. System.out.println("hello n");
5. //m();//same as this.m()
6. this.m();
7. }
8. }
9. class TestThis4{
10. public static void main(String args[]){
13. }}