CSC111 2023 Sem1 Chapter5 3-AccessorsAndConstructors2
CSC111 2023 Sem1 Chapter5 3-AccessorsAndConstructors2
in Depth
}
}
return attributen;
}
. . .
}
attributen = param;
}
. . .
}
System.out.println(course1.getStudentName() +
" has the course “ + course1.getCourseCode());
System.out.println(course2.getStudentName() +
" has the course “ + course2.getCourseCode());
}
}
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.