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

25
O O P Inheritance Object Oriented Programming Prepared & Presented by: Mahmoud Rafeek Alfarra 2012 Chapter 3

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

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

OO

PInheritance

Object Oriented Programming

Prepared & Presented by: Mahmoud Rafeek Alfarra

2012

Chapter 3

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

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

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

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.

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

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.

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

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.

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

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.

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

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

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

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

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

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

}

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

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.

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

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

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

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

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

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).

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

OOOO

PPExample: Students’ types

http://mfarra.cst.ps

• Have part time hours • Have a field training

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

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

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

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$.

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

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, …)

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

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

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

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

Page 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

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

OOOO

PPExample: Inherited members from Student Class

http://mfarra.cst.ps

Inherited method from student class

Inherited method from student class

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

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.

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

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.

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

OOOO

PPاستغفروا ربكم

: ذكره تعالى ا قال

بوا تو م ث م ك ب ر روا ف غ ت س ا ن أ ولى إ ا ن س ح عا ا ت م م عك ت م ي ه ي ل إ

ذي ل ك ت ؤ ي و مى س م ل ج أني إ ف وا ل و ت ن إ و ه ل ض ف ل ض ف

ر ي ب ك م و ي ب ذا ع م ك ي ل ع ف خا أ[ هــود [

http://mfarra.cst.ps

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

OOOO

PP

QUESTIONS?QUESTIONS?http://mfarra.cst.ps

Thank You …Thank You …