برمجة حاسبات (1) - الباب الرابع - التحميل الزائد للدوال PDF

Summary

هذا المستند يقدم ملخصًا لموضوع التحميل الزائد للدوال في برمجة الحاسبات. يتم شرح مفهوم التحميل الزائد وكيفية استخدامه، مع أمثلة توضيحية بلغة Java.

Full Transcript

# برمجة حاسبات (1) ( الباب الرابع ## التحميل الزائد للدوال **Method Overloading** - الدوال التي لها نفس الاسم يمكن أن تعرف في نفس الكلاس مادام لها معاملات مختلفة سواء في العدد أو النوع او الترتيب - Methods of the same name can be declared in the same class, as long as they have different sets of...

# برمجة حاسبات (1) ( الباب الرابع ## التحميل الزائد للدوال **Method Overloading** - الدوال التي لها نفس الاسم يمكن أن تعرف في نفس الكلاس مادام لها معاملات مختلفة سواء في العدد أو النوع او الترتيب - Methods of the same name can be declared in the same class, as long as they have different sets of parameters (determined by the number, types and order of the parameters). ## التحميل الزائد للدوال. Method overloading - عندما نكرر دالتين بنفس الاسم العترجم تختار الدابة ذات المعاملات المناسبة - When an overloaded method is called, the compiler selects the appropriate method by examining the number, types and order of parameters in the call. - الجميلة الرائد هو انشاء العديد من الدوال لها نفس الاسم وكؤدي مهام متشابهة ولها أنواعا وأعداد من معاملات مختلفة - Method overloading is commonly used to create several methods with the same name that perform the same or similar tasks, but on different types or different numbers of parameters. ### Example ```java // Overloaded method declarations public class MethodOverload { // test overloaded square methods public static void main(String[] args) { System.out.println("Square of integer 7 is: "+ square(7)); System.out.println("Square of double 7.5 is: "+square(7.5)); } // square method with int argument public static int square(int intValue) { System.out.println("Called square with int argument: "+ intValue); return intValue * intValue; } // square method with double argument public static double square (double doubleValue) { System.out.println("Called square with double argument"+doubleValue); return doubleValue * doubleValue; } } ``` # Method Overloading ```java public class Name { String name; public void init(){ name= "No Name”; } public void init(String str){ name= str; } public void init(String first, String second){ name= first +" + second; } } ``` - كيف يعرف المترجم أي دالة ؟ - How does the compiler know which method you're invoking? - عن طريق مقارنة نوع وعدد المعاملات وهذا يعرف بالربط الثابت - compares the number and type of the parameters and uses the matched one. - This is called static binding # Method Overloading - Method Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different. - We discussed constructor overloading that allows a class to have more than one constructors having different argument lists. - Argument lists could differ in : - Number of parameters. - Data type of parameters. - order of Data type of parameters. ## Example 1: Method Overloading: changing no. of arguments ```java class Adder{ static int add(int a, int b) {return a+b;} static int add(int a, int b, int c){return a+b+c;} } class TestOverloading1{ public static void main(String[] args){ System.out.println(Adder.add(11,11)); // 22 System.out.println(Adder.add(11,11,11)); // 33 }} ``` Out put: ``` 22 33 ``` ## Example 2:Method Overloading: changing data type of arguments ```java class Adder{ static int add(int a, int b){return a+b;} static double add(double a, double b){return a+b;} } class TestOverloading2{ public static void main(String[] args){ System.out.println(Adder.add(11, 11)); // 22 System.out.println(Adder.add(12.3, 12.6)); // 24.9 }} ``` Out put: ``` 22 24.9 ``` ## Quick Check - case 1 If we have: - int mymethod(int a, int b) - float mymethod(int var1, int var2) What will happen? **Result: Compile time error.** Argument lists are exactly same. Even though return type of methods are different, it is not a valid case. Since return type of method doesn't matter while overloading a method. The compiler distinguishes overloaded methods by their signatures - a combination of the method's name and the number, types and order of its parameters, but not its return type. ## Quick Check - case 2 If we have: - float mymethod(int a, float b) - float mymethod(float var1, int var2) What will happen? **Result: Perfectly fine.** Valid case for overloading. Sequence of the data types are different, first method is having (int, float) and second is having (float, int). ## Constructor overloading - Example ```java public class Time { int hour; int minute; int second; public Time() { hour = 0; minute = 0; second = 0; } public Time(int h, int m, int s){ hour = h; minute = m; second = s; } public Time(int h){ hour = h; } } ``` ## Method Overriding - If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java. - The benefit of overriding is: ability to define a behavior that's specific to the subclass type, which means a subclass can implement a parent class method based on its requirement. ### Rules for Java Method Overriding - method must have same name as in the parent class - method must have same parameter as in the parent class - must be inheritance relationship ## Example 1 ```java class Animal { public void move() { System.out.println("Animals can move"); } } class Dog extends Animal { public void move() { System.out.println("Dogs can walk and run"); } } public class TestDog { public static void main(String args[]) { Animal a = new Animal(); Animal b = new Dog(); a.move(); // runs the method in Animal class b.move(); // runs the method in Dog class } } ``` Out put: ``` Animals can move Dogs can walk and run ``` ## Example 2: with error occurred ```java class Animal { public void move() { System.out.println("Animals can move"); } } class Dog extends Animal { public void move() { System.out.println("Dogs can walk and run"); } public void bark() { System.out.println("Dogs can bark"); } } public class TestDog { public static void main(String args[]) { Animal a = new Animal(); Animal b = new Dog(); a.move(); // runs the method in Animal class b.move(); // runs the method in Dog class b.bark(); // error } } ``` Out put: This program will throw a compile time error since b's reference type Animal doesn't have a method by the name of bark. # Method overloading vs. Method overriding in java - Java Method Overloading example ```java class OverloadingExample{ static int add(int a, int b){ return a+b;} // Static Binding static int add(int a, int b,int c){ return a+b+c;} } ``` - Java Method Overriding example ```java class Animal{ void eat(){ System.out.println("eating...");} // Dynamic Binding } class Dog extends Animal{ void eat(){ System.out.println("eating bread...");} } ``` ## Examples and Applications ### Example of Method Overriding with super keyword ```java public class Super_Class { int num = 20; public void display(){ System.out.println("This is the display method of the Super class"); } } ``` ```java public class Sub_Class extends Super_Class { int num = 10; public void display(){ System.out.println("This is the display method of the Sub class"); } public void my_method(){ display(); super.display(); System.out.println("Value of the variable named num " + "in Sub Class is "+num); System.out.println("Value of the variable named num " + "in Super Class is "+super.num); } public static void main(String[] args){ Sub_Class obj = new Sub_Class(); obj.my_method();} } ``` Output: ``` This is the display method of the Sub class This is the display method of the super class Value of the variable named num in Sub Class is 10 Value of the variable named num in Super Class is 20 ``` ## Application Give the output of the following java program ```java public class Person { String name="Name"; String dob="DOB"; Person(){} Person(String n, String dob) {name=n; this.dob=dob;} void Dispaly() {System.out.print("\n Person, name= "+name+" dob= "+dob);} } public class Student extends Person{ long id=0; int level=0; Student(){} Student(long i, int 1){ id=i; level=1;} Student(long i, int 1,String n, String d) {super(n,d); id=i; level=1;} void Dispaly(){ System.out.print(" \n student name= "+name+" dob="+dob+" id="+id+" and level= "+level);} void Dispaly (String S){ super. Dispaly(); System.out.print(" and also student id="+id+" and level= "+level);} } public static void main(String[] args){ Person p = new Person("Amani","15/09/2000"); Student s1 = new Student(); Student s2 = new Student(123456,3); Student s3 = new Student(789546,3, "Amal","24/04/1999"); p.Dispaly(); s1.Dispaly(); s2.Dispaly(); s3.Dispaly(); s3.Dispaly("ΟΚ"); } } ``` ``` Person, name= Amani dob= 15/09/2000 student name= Name dob= DOB id= 0 and level= 0 student name= Name dob= DOB id=123456 and level= 3 studentname= Amal dob=24/04/1999 id=789546 and level= 3 Person, name= Amal dob= 24/04/1999 and also student id=789546 and level= 3 ```

Use Quizgecko on...
Browser
Browser