التجاوز مقابل التحميل في جافا

مقدمة

التجاوز والتحميل المكدس هما المفاهيم الأساسية في برمجة جافا. فهم الطرق المستخدمة لتنفيذ التعددية في برامجنا بجافا. التعددية هي واحدة من مفاهيم OOPS.

Screenshot of Java code with arrows pointing at instances where overloading and overriding are occurring.

عندما تكون توقيع الطريقة (الاسم والمعلمات) هو نفسه في الفئة الأساسية وفئة الابن، يُسمى ذلك التجاوز. عندما تكون لدينا طريقتان أو أكثر في نفس الفئة لهما نفس الاسم ولكن معلمات مختلفة، يُسمى ذلك التحميل المكدس.

مقارنة بين التجاوز والتحميل المكدس

Overriding Overloading
Implements “runtime polymorphism” Implements “compile time polymorphism”
The method call is determined at runtime based on the object type The method call is determined at compile time
Occurs between superclass and subclass Occurs between the methods in the same class
Have the same signature (name and method arguments) Have the same name, but the parameters are different
On error, the effect will be visible at runtime On error, it can be caught at compile time

مثال على التجاوز والتحميل المكدس

إليك مثال على التحميل المكدس والتجاوز في برنامج جافا:

package com.journaldev.examples;

import java.util.Arrays;

public class Processor {

	public void process(int i, int j) {
		System.out.printf("Processing two integers:%d, %d", i, j);
	}

	public void process(int[] ints) {
		System.out.println("Adding integer array:" + Arrays.toString(ints));
	}

	public void process(Object[] objs) {
		System.out.println("Adding integer array:" + Arrays.toString(objs));
	}
}

class MathProcessor extends Processor {

	@Override
	public void process(int i, int j) {
		System.out.println("Sum of integers is " + (i + j));
	}

	@Override
	public void process(int[] ints) {
		int sum = 0;
		for (int i : ints) {
			sum += i;
		}
		System.out.println("Sum of integer array elements is " + sum);
	}

}

التجاوز

تم تجاوز طريقة process() ومعلمات int i، int j في Processor في الفئة الفرعية MathProcessor. السطر 7 والسطر 23:

public class Processor {

    public void process(int i, int j) { /* ... */ }

}

/* ... */

class MathProcessor extends Processor {
 
    @Override
    public void process(int i, int j) {  /* ... */ }

}

وأيضًا تم تجاوز طريقة process() و int[] ints في Processor في الفئة الفرعية. السطر 11 والسطر 28:

public class Processor {

    public void process(int[] ints) { /* ... */ }

}

/* ... */

class MathProcessor extends Processor {

    @Override
    public void process(Object[] objs) { /* ... */ }

}

التحميل الزائد

تم تحميل طريقة process() في الفئة Processor. الأسطر 7 و 11 و 15:

public class Processor {

    public void process(int i, int j) { /* ... */ }

    public void process(int[] ints) { /* ... */ }

    public void process(Object[] objs) { /* ... */ }

}

الاستنتاج

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

Source:
https://www.digitalocean.com/community/tutorials/overriding-vs-overloading-in-java