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

Inheritance in Java

الكلية كلية تكنولوجيا المعلومات     القسم قسم شبكات المعلومات     المرحلة 3
أستاذ المادة مهدي عبادي مانع الموسوي       15/12/2015 18:06:30
1. Inheritance and Overriding Methods



Single Inheritance
• When a class inherits from only one class, it is called single inheritance.
• Interfaces provide the benefits of multiple inheritance without drawbacks.
• Syntax of a Java class is as follows:

class [extends ] {
*
}




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();
}
}

The Java programming language semantics dictate that p2.method () results in the child version of the method being executed, because the method is declared private, p2 ( declared as parent ) cannot access it , this the semantics language is violated


3. Invoking Overridden Methods
A subclass method may invoke a superclass method using the
super keyword:
• The keyword super is used in a class to refer to its superclass.
• The keyword super is used to refer to the members of superclass, both data attributes and methods.
• Behavior invoked does not have to be in the superclass; it can be further up in the hierarchy.

public class Employee {
private String name;
private double salary;
private Date birthDate;

public String get Details() {
return "Name: " + name + "\nSalary: " + salary;
}
}
public class Manager extends Employee {
private String department;

public String getDetails() {
// call parent method
return super.getDetails()
“\nDepartment: " + department;
}
}

Important notes
1. Final class cannot be subclass.
2. Final method cannot be overridden.
3. Final variable such as a constant and any attempt to change the value of a final variable causes a compiler error.



More Example: Case#1 using Super and Sub Class
package Inheritance;
public class Transportation {
/**
* @param args Mehdi
*/
protected static int x=12;
private int y=19;

public static void meth1(){
System.out.println("Calling Methed in Super Class");
}
private static void meth2(){
System.out.println("Doesn t Call because of Private");


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