KEMBAR78
البرمجة الهدفية بلغة جافا - الوراثة | PPT
O
O
P
Inheritance
Object Oriented Programming
Prepared & Presented by:
Mahmoud Rafeek Alfarra
2012
Chapter 3
OO
OO
PP
http://mfarra.cst.ps
Contents
What is Inheritance ? Advantages1
Superclasses and Subclasses2
"is-a" and the "has-a" relationship3
Mini Example: Vehicle4
Constructors in Subclasses5
Example: Students’ types6
Single Vs Multiple inheritance7
Important Notes8
OO
OO
PP
What 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.
OO
OO
PP
Advantages 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.
OO
OO
PP
Superclasses 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.
OO
OO
PP
Superclasses 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.
OO
OO
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
OO
OO
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 vehicle
Honda is a car &
Honda is a vehicle
Object of sub is also an object of superObject of sub is also an object of super
OO
OO
PP
Mini 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
}
OO
OO
PP
Mini 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.
OO
OO
PP
Mini 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());
}
}
OO
OO
PP
Constructors 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
OO
OO
PP
Constructors 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).
OO
OO
PP
Example: Students’ types
http://mfarra.cst.ps
• Have part time hours • Have a field training
OO
OO
PP
Example: 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 methods
public void setName(String name){
this.name= name;
}
…
// get methods
public String getName(){
return name;
}
….
// Print Data
public String showData(){
return "Name: "+getName()+"nMobile: "+getMobile()+"nGPA: "+getGpa();
} }
OO
OO
PP
Example: 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 method
public void setHours(int hours){
this.hours = hours;
}
// get method
public 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$.
OO
OO
PP
Example: 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, …)
OO
OO
PP
Method 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.");
}
Overrideby
Overrideby
OO
OO
PP
Method 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);
OO
OO
PP
Method 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
OO
OO
PP
Example: Inherited members from Student Class
http://mfarra.cst.ps
Inherited method
from student class
Inherited method
from student class
OO
OO
PP
Single Vs Multiple inheritance
Class 1Class 1 Class 2Class 2
Class 3Class 3
Class 1Class 1
Class 2Class 2
Multiple Inheritance Single Inheritance
Class 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.
OO
OO
PP
Important 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.
OO
OO
PP
‫ربكم‬ ‫استغفروا‬
:‫ذكره‬ ‫تعالى‬ ‫ا‬ ‫قال‬
‫بوا‬ُ ‫تو‬ُ ‫م‬ّ ‫ث‬ُ ‫م‬ْ ‫ك‬ُ ‫ب‬ّ ‫ر‬َ ‫روا‬ُ ‫ف‬ِ ‫غ‬ْ ‫ت‬َ ‫س‬ْ ‫ا‬ ‫ن‬ِ ‫أ‬َ ‫و‬َ
‫لى‬َ ‫إ‬ِ ‫نا‬ً ‫س‬َ ‫ح‬َ ‫عا‬ً ‫تا‬َ ‫م‬َ ‫م‬ْ ‫ك‬ُ ‫ع‬ْ ‫ت‬ّ ‫م‬َ ‫ي‬ُ ‫ه‬ِ ‫ي‬ْ ‫ل‬َ ‫إ‬ِ
‫ذي‬ِ ‫ل‬ّ ‫ك‬ُ ‫ت‬ِ ‫ؤ‬ْ ‫ي‬ُ ‫و‬َ ‫مى‬ّ ‫س‬َ ‫م‬ُ ‫ل‬ٍ ‫ج‬َ ‫أ‬َ
‫ني‬ّ ‫إ‬ِ ‫ف‬َ ‫وا‬ْ ‫ل‬ّ ‫و‬َ ‫ت‬َ ‫ن‬ْ ‫إ‬ِ ‫و‬َ ‫ه‬ُ ‫ل‬َ ‫ض‬ْ ‫ف‬َ ‫ل‬ٍ ‫ض‬ْ ‫ف‬َ
‫ر‬ٍ ‫بي‬ِ ‫ك‬َ ‫م‬ٍ ‫و‬ْ ‫ي‬َ ‫ب‬َ ‫ذا‬َ ‫ع‬َ ‫م‬ْ ‫ك‬ُ ‫ي‬ْ ‫ل‬َ ‫ع‬َ ‫ف‬ُ ‫خا‬َ ‫أ‬َ
[ ]‫هــود‬
http://mfarra.cst.ps
OO
OO
PP
QUESTIONS?QUESTIONS?
http://mfarra.cst.ps
Thank You …Thank You …

البرمجة الهدفية بلغة جافا - الوراثة

  • 1.
    O O P Inheritance Object Oriented Programming Prepared& Presented by: Mahmoud Rafeek Alfarra 2012 Chapter 3
  • 2.
    OO OO PP http://mfarra.cst.ps Contents What is Inheritance? Advantages1 Superclasses and Subclasses2 "is-a" and the "has-a" relationship3 Mini Example: Vehicle4 Constructors in Subclasses5 Example: Students’ types6 Single Vs Multiple inheritance7 Important Notes8
  • 3.
    OO OO PP What 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.
  • 4.
    OO OO PP Advantages of Inheritance http://mfarra.cst.ps Usinginheritance:  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.
  • 5.
    OO OO PP Superclasses 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.
  • 6.
    OO OO PP Superclasses and Subclasses http://mfarra.cst.ps Asubclass 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.
  • 7.
    OO OO 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
  • 8.
    OO OO 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 vehicle Honda is a car & Honda is a vehicle Object of sub is also an object of superObject of sub is also an object of super
  • 9.
    OO OO PP Mini Example: VehicleProject http://mfarra.cst.ps classVehicle { 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 }
  • 10.
    OO OO PP Mini Example: Carclass 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.
  • 11.
    OO OO PP Mini Example: usingsub & 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()); } }
  • 12.
    OO OO PP Constructors 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
  • 13.
    OO OO PP Constructors in Subclasses http://mfarra.cst.ps PublicTest1(){ } 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).
  • 14.
    OO OO PP Example: Students’ types http://mfarra.cst.ps •Have part time hours • Have a field training
  • 15.
    OO OO PP Example: Student Class http://mfarra.cst.ps classStudent { 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 methods public void setName(String name){ this.name= name; } … // get methods public String getName(){ return name; } …. // Print Data public String showData(){ return "Name: "+getName()+"nMobile: "+getMobile()+"nGPA: "+getGpa(); } }
  • 16.
    OO OO PP Example: PostGraduated Class http://mfarra.cst.ps classPostGraduated extends Student { private int hours; public PostGraduated(String name, String mobile, float gpa,int hours){ super(name, mobile, gpa); this.hours = hours; } // set method public void setHours(int hours){ this.hours = hours; } // get method public 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$.
  • 17.
    OO OO PP Example: Graduate Class http://mfarra.cst.ps classGraduate 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, …)
  • 18.
    OO OO PP Method Overridden http://mfarra.cst.ps An instancemethod 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."); } Overrideby Overrideby
  • 19.
    OO OO PP Method Overridden http://mfarra.cst.ps public intcalcsal(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);
  • 20.
    OO OO PP Method Overridden VsMethod 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
  • 21.
    OO OO PP Example: Inherited membersfrom Student Class http://mfarra.cst.ps Inherited method from student class Inherited method from student class
  • 22.
    OO OO PP Single Vs Multipleinheritance Class 1Class 1 Class 2Class 2 Class 3Class 3 Class 1Class 1 Class 2Class 2 Multiple Inheritance Single Inheritance Class 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.
  • 23.
    OO OO PP Important Notes Methods ofa 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.
  • 24.
    OO OO PP ‫ربكم‬ ‫استغفروا‬ :‫ذكره‬ ‫تعالى‬‫ا‬ ‫قال‬ ‫بوا‬ُ ‫تو‬ُ ‫م‬ّ ‫ث‬ُ ‫م‬ْ ‫ك‬ُ ‫ب‬ّ ‫ر‬َ ‫روا‬ُ ‫ف‬ِ ‫غ‬ْ ‫ت‬َ ‫س‬ْ ‫ا‬ ‫ن‬ِ ‫أ‬َ ‫و‬َ ‫لى‬َ ‫إ‬ِ ‫نا‬ً ‫س‬َ ‫ح‬َ ‫عا‬ً ‫تا‬َ ‫م‬َ ‫م‬ْ ‫ك‬ُ ‫ع‬ْ ‫ت‬ّ ‫م‬َ ‫ي‬ُ ‫ه‬ِ ‫ي‬ْ ‫ل‬َ ‫إ‬ِ ‫ذي‬ِ ‫ل‬ّ ‫ك‬ُ ‫ت‬ِ ‫ؤ‬ْ ‫ي‬ُ ‫و‬َ ‫مى‬ّ ‫س‬َ ‫م‬ُ ‫ل‬ٍ ‫ج‬َ ‫أ‬َ ‫ني‬ّ ‫إ‬ِ ‫ف‬َ ‫وا‬ْ ‫ل‬ّ ‫و‬َ ‫ت‬َ ‫ن‬ْ ‫إ‬ِ ‫و‬َ ‫ه‬ُ ‫ل‬َ ‫ض‬ْ ‫ف‬َ ‫ل‬ٍ ‫ض‬ْ ‫ف‬َ ‫ر‬ٍ ‫بي‬ِ ‫ك‬َ ‫م‬ٍ ‫و‬ْ ‫ي‬َ ‫ب‬َ ‫ذا‬َ ‫ع‬َ ‫م‬ْ ‫ك‬ُ ‫ي‬ْ ‫ل‬َ ‫ع‬َ ‫ف‬ُ ‫خا‬َ ‫أ‬َ [ ]‫هــود‬ http://mfarra.cst.ps
  • 25.