انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

1. Overriding Methods

الكلية كلية تكنولوجيا المعلومات     القسم قسم شبكات المعلومات     المرحلة 3
أستاذ المادة مهدي عبادي مانع الموسوي       10/11/2013 20:29:03
1. Overriding Methods
A subclass can modify behavior inherited from a parent class.
• A subclass can create a method with different functionality than the parent’s method but with the same:

• Name
• Return type
• Argument list

Then the new method is said to override the old one.

So, what is the objective of subclass?

Example

Public class Employee {

protected String name;
protected double salary;
protected Date birthDate;

public String getDetails() {
return “Name: “ + name + “\n” +
“Salary: “ + salary;
}
}
public class Manager extends Employee {

protected String department;

Public String getDetails() {

return “Name: “ + name + “\n” +
“Salary: “ + salary + "\n" +
“Manager of: “ + department;
}
}
The Manager class has a getDetails method by definition because it inherits one from the “Employee class”. However, the original has been replaced, or overridden by the child class ‘version.

2. Overridden Methods Cannot Be Less Accessible
Public class Parent {
Public void doSomething() { }
}

Public class Child extends Parent {
Private void doSomething() {} // illegal
}

public class UseBoth {
public void doOtherThing() {
Parent p1 = new Parent();
Parent p2 = new Child();
p1.doSomething();
p2.doSomething();
}
}


المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .