مساق الخوارزميات والبرمجة بلغة جافا (1) مفاهيم الخوارزميات ج1
البرمجة الهدفية بلغة جافا - الوراثة
-
Upload
mahmoud-alfarra -
Category
Education
-
view
167 -
download
8
Transcript of البرمجة الهدفية بلغة جافا - الوراثة
OO
PInheritance
Object Oriented Programming
Prepared & Presented by: Mahmoud Rafeek Alfarra
2012
Chapter 3
OOOO
PP
http://mfarra.cst.ps
Contents
What is Inheritance ? Advantages1
Superclasses and Subclasses2
"is-a" and the "has-a" relationship3
Mini Example: Vehicle 4
Constructors in Subclasses5
Example: Students’ types6
Single Vs Multiple inheritance 7
Important Notes8
OOOO
PPWhat is Inheritance ?
Inheritance is a form of software reuse in which a new class is created by absorbing
an existing class's members and embellishing them with new or modified
capabilities.
http://mfarra.cst.ps
P_Properties P_ methods
P_Properties P_ methods Parent Class
C_Properties C_methods
C_Properties C_methods Child Class
This class has its properties, methods and that of its parent.
This class has its properties, methods and that of its parent.
OOOO
PPAdvantages of Inheritance
http://mfarra.cst.ps
Using inheritance: to minimize the amount of duplicate
code. A better organization of code and smaller, simpler compilation units.
make application code more flexible to change because classes that inherit from a common superclass can be used interchangeably.
OOOO
PPSuperclasses and Subclasses
http://mfarra.cst.ps
P_Properties P_ methods
P_Properties P_ methods SuperClass
C_Properties C_methods
C_Properties C_methods SubClass
When creating a class, rather than
declaring completely new members, the
programmer can designate that the new
class should inherit the members of an
existing class.
The existing class is called the
superclass, and the new class is the
subclass.
Each subclass can become the
superclass for future subclasses.
OOOO
PPSuperclasses and Subclasses
http://mfarra.cst.ps
A subclass normally adds its own fields and methods.
Therefore, a subclass is more specific than its superclass.
Typically, the subclass exhibits the behaviors of its superclass and
additional behaviors that are specific to the subclass.
In Java, the class hierarchy begins with class Object (in package java.lang), which every class
in Java directly or indirectly extends.
OOOO
PP
properties3 methods3
properties3 methods3 SubClass
Direct & Indirect Superclasses
http://mfarra.cst.ps
The direct superclass is the superclass from which the subclass
explicitly inherits. An indirect superclass
is any class above the direct superclass in the
class hierarchy.
properties1 methods1
properties1 methods1 Indirect
SuperClass
properties2 methods2
properties2 methods2 Direct
SuperClass
OOOO
PP"is-a" and the "has-a" relationship
http://mfarra.cst.ps
"Is-a" represents inheritance.In an "is-a" relationship, an object of a
subclass can also be treated as an object of its superclass.
For example, a car is a vehicle.
Vehicle Class
Vehicle Class SuperClass
Car ClassCar Class SubClass
Honda is a car & Honda is a vehicleHonda is a car &
Honda is a vehicle
Object of sub is also an object of superObject of sub is also an object of super
OOOO
PPMini Example: VehicleProject
http://mfarra.cst.ps
class Vehicle {protected String model;protected float price;
public Vehicle(String model, float price){this.model = model;this.price = price;
}
public String print(){return "Model: "+model+"\t Price: "+price;
}
public void setModel(String model){this.model = model;}public String getModel(){
return model;} // set and get of price
}
OOOO
PPMini Example: Car class
http://mfarra.cst.ps
class Car extends Vehicle{private int passengers;
public Car(String model, float price,int passengers){super( model, price);this.passengers = passengers;
}
public String print(){return "Data is:\n"+super.print()+
" # of passengers: "+passengers;}
}
A compilation error occurs if a subclass constructor calls one of its superclass constructors with arguments that do not
match the superclass constructor declarations.
OOOO
PPMini Example: using sub & super class
http://mfarra.cst.ps
public class VehicleProjectInheritance { public static void main(String[] args) { Car c = new Car("Honda", 455.0f, 4); System.out.println(c.print()); }}
OOOO
PPConstructors in Subclasses
When a program creates a subclass object, the
subclass constructor immediately calls the
superclass constructor.
The superclass constructor's body executes to
initialize the superclass's instance variables that are
part of the subclass object, then the subclass
constructor's body executes to initialize the
subclass-only instance variables.
http://mfarra.cst.ps
OOOO
PPConstructors in Subclasses
http://mfarra.cst.ps
Public Test1(){}
Public Test1(){}
SuperClass
Public Test2(){}
Public Test2(){}
SubClass
calls
Return values
Subclass constructor invokes its direct superclass's constructor either explicitly (via the super reference) or
implicitly (calling the superclass's default constructor or no-argument constructor).
OOOO
PPExample: Students’ types
http://mfarra.cst.ps
• Have part time hours • Have a field training
OOOO
PPExample: Student Class
http://mfarra.cst.ps
class Student {protected String name;protected String mobile;protected float gpa;public Student(String name, String mobile, float gpa){
this.name = name;this.mobile = mobile;this.gpa = gpa;
}// set methodspublic void setName(String name){
this.name= name;}…// get methodspublic String getName(){
return name;}….// Print Data
public String showData(){return "Name: "+getName()+"\nMobile: "+getMobile()+"\nGPA: "+getGpa(); } }
OOOO
PPExample: PostGraduated Class
http://mfarra.cst.ps
class PostGraduated extends Student {private int hours;
public PostGraduated(String name, String mobile, float gpa,int hours){super(name, mobile, gpa);this.hours = hours;
}// set methodpublic void setHours(int hours){
this.hours = hours;}// get methodpublic int getHours(){
return hours;} public String PrintData (){
return showData() + " Hours: "+getHours();}
}
Declare another method to calculate
the earn of post graduate if the cost of
each hour is 20$.
OOOO
PPExample: Graduate Class
http://mfarra.cst.ps
class Graduate extends Student {private String trainingField;
public Graduate(String name, String mobile, float gpa, String trainingField){super(name, mobile, gpa);this.trainingField = trainingField;
}
public void setTrainingField(String trainingField){this.trainingField= trainingField;
}
public String setTrainingField(){return trainingField;
}
public String Printinfo(){return showData() + " TrainingField: "+getTrainingField();
}
}
Declare another method to calculate
the grade of the graduate student
(Excellent, V.Good, …)
OOOO
PPMethod Overridden
http://mfarra.cst.ps
An instance method in a subclasssubclass with the same signature (name, plus the number and the
type of its parameters) and return type as an instance method in the superclasssuperclass overrides
the superclass's method.
public class Animal { public static void testClassMethod() { System.out.println("The class" + " method in Animal"); } public void testInstanceMethod() { System.out.println("The instance " + " method in Animal."); } }
public class Cat extends Animal { public static void testClassMethod() { System.out.println("The class method" + " in Cat."); }
public void testInstanceMethod() { System.out.println("The instance method" + " in Cat."); }
Overrid e by
Overrid e by
OOOO
PPMethod Overridden
http://mfarra.cst.ps
public int calcsal(int x){sal = days*x;return sal; }
public int calcsal(int x){sal = days*x;return sal; }
public int calcsal(int x){sal = (days*x)- absent;return sal; }
public int calcsal(int x){sal = (days*x)- absent;return sal; }
Super obj1 = new Super ();int x = obj.
Super obj1 = new Super ();int x = obj. Calcsal(20);Calcsal(20);
Super class
Sub class
Sub obj2 = new Super ();int x = obj.
Sub obj2 = new Super ();int x = obj. Calcsal(20);Calcsal(20);
OOOO
PPMethod Overridden Vs Method Overloading
http://mfarra.cst.ps
Using code, distinguish between overridden and overloading method.
Implement the concept of method overridden on the methods of print in example of Students’ types
OOOO
PPExample: Inherited members from Student Class
http://mfarra.cst.ps
Inherited method from student class
Inherited method from student class
OOOO
PPSingle Vs Multiple inheritance
Class 1Class 1 Class 2Class 2
Class 3Class 3
Class 1Class 1
Class 2Class 2
Multiple Inheritance Single InheritanceClass can inherit members from more than
one class (more than one super class) Class can inherit members from only
one class (one super class)
Java was designed without multiple inheritance.
Java was designed without multiple inheritance.
Java was designed with single inheritance.
Java was designed with single inheritance.
OOOO
PPImportant Notes
Methods of a subclass cannot directly access private members of their superclass.
With inheritance, the common instance variables and methods of all the classes in the hierarchy
are declared in a superclass.
Use the protected access modifier when a superclass should provide a method only to its subclasses and other classes in the same package, but not to other
clients.
A subclass is more specific than its superclass and represents a smaller group of objects.
A superclass's protected members have an intermediate level of protection between public and
private access. They can be accessed by members of the superclass, by members of its subclasses and by
members of other classes in the same package.
OOOO
PPاستغفروا ربكم
: ذكره تعالى ا قال
بوا تو م ث م ك ب ر روا ف غ ت س ا ن أ ولى إ ا ن س ح عا ا ت م م عك ت م ي ه ي ل إ
ذي ل ك ت ؤ ي و مى س م ل ج أني إ ف وا ل و ت ن إ و ه ل ض ف ل ض ف
ر ي ب ك م و ي ب ذا ع م ك ي ل ع ف خا أ[ هــود [
http://mfarra.cst.ps
OOOO
PP
QUESTIONS?QUESTIONS?http://mfarra.cst.ps
Thank You …Thank You …