0% found this document useful (0 votes)
6 views

Final Class and Methods

The document explains the concept of final classes in Java, which cannot be subclassed, and provides examples of method argument passing by value and by reference. It also discusses the usage of the 'this' keyword in Java, highlighting its role in resolving variable ambiguity, invoking methods, and passing arguments. Additionally, it presents examples demonstrating the correct usage of 'this' to differentiate between instance variables and parameters.

Uploaded by

P.Padmini Rani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Final Class and Methods

The document explains the concept of final classes in Java, which cannot be subclassed, and provides examples of method argument passing by value and by reference. It also discusses the usage of the 'this' keyword in Java, highlighting its role in resolving variable ambiguity, invoking methods, and passing arguments. Additionally, it presents examples demonstrating the correct usage of 'this' to differentiate between instance variables and parameters.

Uploaded by

P.Padmini Rani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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

ii. Call by reference


In call by reference the object will be passed to the method as an argument. At that time the
actual and formal arguments are same.
That means any occurs in actual arguments will be reflected in the formal arguments.
Example:
class Swap
{
int a,b;
void setValues(Swap objSwap)
{
a = objSwap.a; b = objSwap.b;
}
void swapping()
{
int temp; temp =a; a=b; b=temp;
}
void display()
{
System.out.println("In Swap Class: a= "+a+" b= "+b);
}
}
class CallByReference
{
public static void main (String args[])
{
Swap obj =new Swap(); obj.a=10; obj.b=20;
System.out.println("Before Swap : obj.a = "+ obj.a+" obj.b="+
obj.b);
obj.setValues(obj); // call by reference obj.swapping();
obj.display();
System.out.println("After Swap: obj.a = "+ obj.a+ " obj.b="+
obj.b);
}
}
output:
C:\1. JAVA\PPT Programs>javac CallByReference.java
C:\1. JAVA\PPT Programs>java CallByReference
Before Swap : obj.a = 10 obj.b=20
In Swap Class: a= 20 b= 10
After Swap : obj.a = 20 obj.b=10

this keyword in Java


There can be a lot of usage of Java this keyword. In Java, this is a reference variable that
refers to the current object.

Usage of Java this keyword


Here is given the 6 usage of java this keyword.
1. this can be used to refer current class instance variable.
2. this can be used to invoke current class method (implicitly)
3. this() can be used to invoke current class constructor.
4. this can be passed as an argument in the method call.
5. this can be passed as argument in the constructor call.
6. this can be used to return the current class instance from the method.

Suggestion: If you are beginner to java, lookup only three usages of this keyword.

this: to refer current class instance variable

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.

Understanding the problem without this keyword

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;

5. Student(int rollno,String name,float fee){


6. rollno=rollno;

7. name=name;
8. fee=fee;

9. }
10. void display(){System.out.println(rollno+" "+name+" "+fee);}

11. }
12. class TestThis1{

13. public static void main(String args[]){


14. Student s1=new Student(111,"ankit",5000f);

15. Student s2=new Student(112,"sumit",6000f);


16. s1.display();

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.

Solution of the above problem by this keyword

1. class Student{
2. int rollno;

3. String name;
4. float fee;

5. Student(int rollno,String name,float fee){


6. this.rollno=rollno;

7. this.name=name;
8. this.fee=fee;

9. }
10. void display(){System.out.println(rollno+" "+name+" "+fee);}

11. }
12.

13. class TestThis2{


14. public static void main(String args[]){

15. Student s1=new Student(111,"ankit",5000f);


16. Student s2=new Student(112,"sumit",6000f);

17. s1.display();
18. s2.display();

19. }}

Test it Now

Output:

111 ankit 5000.0


112 sumit 6000.0

If local variables(formal arguments) and instance variables are different, there is no need to
use this keyword like in the following program:

Program where this keyword is not required

1. class Student{
2. int rollno;

3. String name;
4. float fee;

5. Student(int r,String n,float f){


6. rollno=r;

7. name=n;
8. fee=f;

9. }
10. void display(){System.out.println(rollno+" "+name+" "+fee);}

11. }
12.

13. class TestThis3{


14. public static void main(String args[]){

15. Student s1=new Student(111,"ankit",5000f);


16. Student s2=new Student(112,"sumit",6000f);

17. s1.display();
18. s2.display();

19. }}

Test it Now

Output:

111 ankit 5000.0


112 sumit 6000.0
It is better approach to use meaningful names for variables. So we use same
name for instance variables and parameters in real time, and always use this
keyword.
2) this: to invoke current class method

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[]){

11. A a=new A();


12. a.n();

13. }}

You might also like