0% found this document useful (0 votes)
2 views21 pages

Lecture 14A B Copy Final

This lecture discusses object copying in Java, highlighting that there is no direct operator for copying objects, and methods such as constructors, assignment, and the clone() method can be used instead. It also explains the final keyword's application to variables, methods, and classes, emphasizing that final variables cannot be reassigned and final classes cannot be inherited. Additionally, the lecture covers the differences between normal and final variables, and the implications of using final for methods and classes.

Uploaded by

Amaan Sadri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views21 pages

Lecture 14A B Copy Final

This lecture discusses object copying in Java, highlighting that there is no direct operator for copying objects, and methods such as constructors, assignment, and the clone() method can be used instead. It also explains the final keyword's application to variables, methods, and classes, emphasizing that final variables cannot be reassigned and final classes cannot be inherited. Additionally, the lecture covers the differences between normal and final variables, and the implications of using final for methods and classes.

Uploaded by

Amaan Sadri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

LECTURE 14A

Java copy

Prof. Anita Agrawal,


BITS, Pilani-K.K.Birla Goa campus
2/15/2023 4:15 PM Anita Agrawal CS F213 2

Java Copy ….
◼ In Java, there is no operator to create a
copy of an object.

◼ Assignment operator will create a copy of


variables and not of objects.

……..cao
2/15/2023 4:15 PM Anita Agrawal CS F213 3

Methods of Copying objects in Java

◼ By using constructor

◼ By assigning the value of one object to


another

◼ By clone() method of Object class


2/15/2023 4:15 PM Anita Agrawal CS F213 4

Copying values by using constructor

….cbc
2/15/2023 4:15 PM Anita Agrawal CS F213 5

Copying by assigning values of one


object to another

• The values of one object can be copied into another


object by assigning the object’s values to another object.

…………..cwc
2/15/2023 4:15 PM Anita Agrawal CS F213 6

Copying by clone method of object class


2/15/2023 4:15 PM Anita Agrawal CS F213 7

Difference between constructor and


method in Java
Constructor Method
Used to initialize the state Used to expose behavior
of an object of an object

Must not have a return Must have return type


type
Is invoked implicitly Is invoked explicitly

The Java compiler Method is not provided by


provides a default compiler in any case
constructor if you do not
have any constructor
Name must be same as Name may or may not be
class name same as class name
2/15/2023 4:15 PM Anita Agrawal CS F213 8

final keyword in java


• final is a non-access modifier applicable to:
• Variable
• Method
• Class

Final variable: Used to create constant variables


Final methods: Used to prevent method overriding
Final classes: Used to prevent extending classes
2/15/2023 4:15 PM Anita Agrawal CS F213 9

• final variable:
• When a variable is declared with final
keyword, it’s value cannot be modified.
• It is essentially a constant.

• final reference:
• cannot be re-bound to reference another
object
• Internal state of the object pointed can be
changed
2/15/2023 4:15 PM Anita Agrawal CS F213 10

 The final variable has to be initialized,


otherwise compile-time error.

 Can be initialized only once.


2/15/2023 4:15 PM Anita Agrawal CS F213 11

Ways to initialize the final variable


• Initialization during declaration:
Example:
• final int num = 7;
• static final double length = 52.5
• If not initialized during declaration: Blank variable

• Blank variable initialization:


• inside the instance initialization block
• inside the constructor
• inside all the constructors if more than one constructor
• inside the static block, if a static variable
2/15/2023 4:15 PM Anita Agrawal CS F213 12

//Java program to demonstrate different


// ways of initializing a final variable
class A
{
// a final variable direct initialize
final double width = 10.5;

// a blank final variable


final int sum;
// instance initializer block for initializing sum
{
sum = 25;
}
2/15/2023 4:15 PM Anita Agrawal CS F213 13

// a final static variable PI direct initialize


static final double PI = 3.14;

// a blank final static variable


static final double grav_const;

// Constructor for initializing height. Note that if there are more


// than one constructors, you must initialize height in them also

OOP( )

{
height = 20;
}
2/15/2023 4:15 PM Anita Agrawal CS F213 14

• A final variable cannot be re-assigned, but in case of a


reference final variable, internal state of the object
pointed by that reference variable can be changed.
Note: This is not re-assigning. This property of final is
called non-transitivity.

• The non-transitivity property also applies to arrays,


final arrays.
2/15/2023 4:15 PM Anita Agrawal CS F213 15

// Java program to demonstrate reference final


variable

finref……
2/15/2023 4:15 PM Anita Agrawal CS F213 16

• A final variable cannot be reassigned, doing it,


will throw compile-time error.
2/15/2023 4:15 PM Anita Agrawal CS F213 17

Java program to demonstrate not assigning/ re-assigning


final variable will throw compile-time error.

……finvar
2/15/2023 4:15 PM Anita Agrawal CS F213 18

When a final variable is created inside a method /


constructor/block, it is called local final variable, and it
must be initialized once where it is created.

fincons.java
2/15/2023 4:15 PM Anita Agrawal CS F213 19

Difference between a normal variable and


a final variable

• Values can be reassigned to normal variables, but final


variable values cannot be changed once assigned.

• Use final variables only for the values that are


required to remain constant throughout the execution of
program.
2/15/2023 4:15 PM Anita Agrawal CS F213 20

Final classes
• Declared with final keyword.

• A final class cannot be extended(inherited).

• Uses of a final class :


• To prevent inheritance, as final classes cannot be extended.

• To create an immutable class like the predefined String class.


• You can not make a class immutable without making it final.
2/15/2023 4:15 PM Anita Agrawal CS F213 21

final method
• Declared with a final keyword.

• A final method cannot be overridden

• Declare methods with final keyword to follow the same


implementation throughout all the derived classes.

You might also like