3-Lecture3
3-Lecture3
• This line is written into the main function of the program and
then values are given for all properties. By writing the name-
object, then the dot, then name-property, then the assignment
sign "=", then the value of the property according to data type.
• Object_name. name-property = value;
Example: Create an object of a class “Student” in the main
function of another class called “Test”.
class Student{
String name;
int ID;
String Dep;
double avg;
char group;
}
public class Test{
public static void main(String args[]){
Student s1 = new Student();
s1.name="Ali";
s1.ID=52362;
s1.Dep =“IS";
s1.avg=80;
s1.group='c';
}}
public class Test
2. {
3. public static void main(String args[])
4. {
5. Student s1 = new Student(); //Student اشتقاق كان من الفئة
: شرح البرنامج
6. s1.name=“Ahmed” تم إنشاء فئة تحت14 في السطر
7. s1.ID=52362; تحتوي على مجموعةStudent اسم
قيم تخصيص
8. s1.Dep=“CS” من الخصائص والتي
للخصائص
9. s1.avg=80;
10. s1.group='c';
ودالة واحدة20 إلى16 تبدأ من السطر
11. s1.print(s1.name,s1.ID,s1.Dep,s1.avg,s1.group);// استدعاء دالة الطباعة21 تبدأ من السطرprint تحت اسم
12. } 28 إلى
13. } تم اشتقاق كائن5 في السطر رقم
14. class Student //Student انشاء الفئة
15. {
Student . من الفئةs1 تحت اسم
16. String name; تم فيها تخصيص10 إلى6 السطر من
.قيم لكافة خصائص الكائن
خصائص
17. int ID;
18. String Dep; تم استدعاء الدالة11 في السطر رقم
الكالس
19. double avg;
الخاصة بالطباعة وارسال كافة
20. char group;
21. void print(String p1,int p2,String p3,double p4,char p5) المعامالت المطلوب طباعة
22. { .قيمها
23. System.out.println("Student name is :" + p1 );
24. System.out.println("Student id is :" + p2 );
25. System.out.println("Student department is :" + p3 );
26. System.out.println("Student average is :" + p4 );
دالة الطباعة
27. System.out.println("Student group is :" + p5 );
28. }
29. }
Constructor
• constructor is a function statement that has a beginning and an end that is
similar in structure to the function and is distinguished from the rest of the
functions through its name, which is similar to the name of the class in which it
is located.
• The constructor function also differs from other functions in the way it is
implemented, as the code inside it is executed every time a new object is
derived from the class in which the constructor function is located.
• This is done using the keyword "new", which is used when creating a new
object from the class. Also, the constructor function cannot return a value as is
available in some other functions.
• Constructor functions are used to create variables that need to be revalued or
given initial values every time a new object of the class is derived. There are
two types of constructor functions:
public class Main_Class // الفئة الرئيسية
1. Non Parameterized {
Constructor: public static void main(String[]
args)// الدالة الرئيسة في البرنامج
• constructor {
functions that do not Student Std= new Student(); //
Student اشتقاق كائن من الفئة
contain parameters Std.print(); // استدعاء دالة الطباعة
and therefore do not }
}
need to send class Student // الفئة
values or variables {
public Student()
when executed. {
There is only one name="Salem"; //name تهيئة المتغير
}
function in each public void print()// دالة الطباعة
class and it is called {
System.out.println("welcome Mr "+name);
Default Constructor }
}
Output
Sum of two integers: 30
Sum of three integers: 60
Sum of two doubles: 31.0
Sum of three doubles: 61.5
Explanation of the previous example
• Method Signatures: In the MathUtils class, the add method is
overloaded with different signatures.
• add(int a, int b)
• add(int a, int b, int c)
• add(double a, double b)
• add(double a, double b, double c)
• Usage: In the Main class, the correct add method is called based on
the number and type of arguments passed.