0% found this document useful (0 votes)
19 views19 pages

CSC111 2023 Sem1 Chapter5 3-AccessorsAndConstructors2

Uploaded by

zyadking1800
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)
19 views19 pages

CSC111 2023 Sem1 Chapter5 3-AccessorsAndConstructors2

Uploaded by

zyadking1800
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/ 19

Chapter 5: Classes and Objects

in Depth

Getters, Setters and Constructors


How Private Attributes could be
Accessed
• Private attributes are not accessible
from outside.
• Except from objects of the same class.
• They are accessible:
• From inside: from the object containing the
data itself.
• From objects of the same class.
• They are accessible from outside using
accessor operations:
• Getters
• Setters
Page 2 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
class Course {
// Data Member
private String studentName;
private String courseCode ;
}

public class CourseRegistration {


public static void main(String[] args) {
Course course1, course2;
//Create and assign values to course1
course1 = new Course( );
course1.courseCode= “CSC112“;
course1.studentName= “Majed AlKebir“;

//Create and assign values to course2


course2 = new Course( );
course2.courseCode= “CSC107“;
course2.studentName= “Fahd AlAmri“;

System.out.println(course1.studentName + " has the course “+


course1.courseCode);
System.out.println(course2.studentName + " has the course “+
course2.courseCode);

}
}

Page 3 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Getters
The object point of view The user point of view
• Are operations • Are services called
performed by the from outside allowing
object returning to to retrieve data from
outsiders data the object state.
retrieved from the
object state.
:Y (Client) object:X
Getters are:
public
•Public
private •With no parameters
Data •With return value
Data
Getters

Page 4 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Template for Getters
public class ClassName {
private dataType1 attribute1;
. . .
private dataTypen attributen;
. . .

public dataType1 getAttribute1() {


return attribute1;
}
. . .

public dataTypen getAttributen() {

return attributen;
}
. . .
}

Page 5 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Setters
The object point of view The user point of view
• Are operations • Are services used by
performed by the outsiders allowing to
object allowing to provide to the object
receive and store in the the data that should
object state the data be stored in the
provided by outsiders. object state.
:Y (Client) object:X
Setters are:
public •Public
private •With 1 parameter
Data •With no return value
Data
Setters
Page 6 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Template for Setters
public class ClassName {
private dataType1 attribute1;
. . .
private dataTypen attributen;
. . .

public void setAttribute1(dataType1 param){


attribute1 = param;
}
. . .

public void setAttributen(dataTypen param) {

attributen = param;
}
. . .
}

Page 7 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


public class Course {
// Attributes
private String studentName;
private String courseCode ;
...
public String getStudentName() {
return studentName;
}
public String getCourseCode() {
return courseCode;
}
...
public void setStudentName(String val) {
studentName = val;
}
public void setCourseCode(String val) {
courseCode = val;
}
}

Page 8 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


public class CourseRegistration {
public static void main(String[] args) {
Course course1, course2;
//Create and assign values to course1
course1 = new Course( );
course1.setCourseCode(“CSC112“);
course1.setStudentName(“Majed AlKebir“);

//Create and assign values to course2


course2 = new Course( );
course2.setCourseCode(“CSC107“);
course2.setStudentName(“Fahd AlAmri“);

System.out.println(course1.getStudentName() +
" has the course “ + course1.getCourseCode());
System.out.println(course2.getStudentName() +
" has the course “ + course2.getCourseCode());

}
}

Page 9 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Passing an Object
to a Setter

Page 10 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Setters and Sharing Objects

• The same Student


object reference is
passed to card1 and
card2 using setters

• Since we are actually passing


the same object reference, it
results in the owner of two
LibraryCard objects referring
to the same Student object

Page 11 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Class Constructors

• A class is a blueprint or prototype from


which objects of the same type are
created.
• Constructors define the initial states of
objects at birth.
• ClassName x = new ClassName();
• A class contains at least one constructor.
• A class may contain more than one
constructor.
Page 12 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
The Default Class Constructor

• If no constructors are defined in the


class, the default constructor is added by
the compiler at compile time.

• The default constructor does not accept


parameters and creates objects with
empty states.
• ClassName x = new ClassName();

Page 13 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Class Constructors Declaration
public <constructor name> ( <parameters> ){
<constructor body>
}

• The constructor name: a constructor has the same


names as the class .

• The parameters represent values that will be passed to


the constructor for initialize the object state.

• Constructor declarations look like method declarations


except that:
• they use the name of the class
• and have no return type.

Page 14 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Example of
a Constructor with No-Parameter
public class Kasree { A.
A.The
Theinstance
instance x
variable
variable isisallocated
allocated
private int bast;
ininmemory.
memory.
private int maquam; Object: Kasree

public Kasree() { B.
B.The
Theobject
object isis
bast 0
created
createdwith
withinitial
initialstate
state maquam 1
bast = 0; maquam =1;
}
. . .
C.
C.The
Thereference
referenceofofthe
the
} object
objectcreated
createdininBB isis x
A
A assigned
assignedtotothe
thevariable.
variable.

Kasree x; B
B Object: Kasree

bast 0
x = new Kasree ( ) ;
maquam 1
C

State of Memory
Code
Page 15 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Class with Multiple Constructors
public class Kasree { A.
private int bast; A.The
Theconstructor
constructor
private int maquam; declared
declaredwith
withno-parameter
no-parameter x
isisused
used to createthe
to create theobject
object
public Kasree() { Object: Kasree
bast = 0; maquam =1;
} bast 0
public Kasree(int a, int b) { maquam 1
bast = a;
if (b != 0) maquam = b;
else maquam = 1; B.
B.The
Theconstructor
constructor
declared y
} declaredwith
withparameters
parametersisis
. . . used
usedtotocreate
createthe
theobject
object
}

Object: Kasree

Kasree x , y; A
A bast 4
B
B maquam 3
x = new Kasree()

State of Memory
y = new Kasree(4, 3);
Code
Page 16 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP
Overloading
• Two of the components of a method
declaration comprise the method signature:
• the method's name
• and the parameter types.
• The signature of the constructors declared
above are:
• Kasree()
• Kasree(int, int)
• Overloading methods allows implementing
different versions of the same method with
different method signatures.
• This means that methods within a class can have the
same name if they have different parameter lists.

Page 17 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Overloading (cont.)
• Overloaded methods are differentiated by:
• the number,
• and the type of the arguments passed into the method.
• You cannot declare more than one method
with:
• the same name,
• and the same number and type of parameters.
• The compiler does not consider return type
when differentiating methods.
• No declaration of two methods having the same
signature even if they have a different return type.

Page 18 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP


Intra-Constructors Calls
• A constructor of a class may use an
other constructor of the same class.
public class Kasree {
private int bast;
private int maquam;

public Kasree(int a, int b) {


bast = a;
Kasree x , y;
if (b != 0) maquam = b;
else maquam = 1;
x = new Kasree()
}
y = new Kasree(4, 3);
public Kasree() {
}
Kasree(0, 1); Client Side
. . .
}

Page 19 Dr S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

You might also like