درس مهندسی اینترنت – مهدی عمادی مهندسی اینترنت برنامه‌نویسی در اینترنت Object Oriented Programming Concepts in JAVA.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
معاونت درمان امور مامایی اردیبهشت 90. برای ثبت اطلاعات در برنامه نرم افزاری نظام مراقبت مرگ پریناتال ابتدا لازم است برنامه نرم افزار info-path وپرنیان.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation. Inheritance. Abstract Classes.
Decision Tree.
Polymorphism What is Polymorphism? Taking Advantage of Polymorphism
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Further OO Concepts (Part I) Further OO Concepts I - Polymorphism Overview l Polymorphism is Wonderful. l Usefulness of Up casting? l Method call binding?
Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
ICS 202: Data Structures Fall (081) Section 01: 10-10:50am, 24:116 Section 02: 9 - 9:50am, 24:174 Instructor: Dr. Wasfi G. Al-Khatib Office:
فایل پردازی در C File based Programming in C. انواع فایل متنی –سرعت بالا –حجم کمتر –امکان دسترسی تصادفی –حفظ امنیت داده ها دودویی (باینری) –امکان باز.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Network Address Translation (NAT). 2 Private Network شبکه خصوصی شبکه ای است که بطور مستقیم به اینترنت متصل نیست در یک شبکه خصوصی آدرس های IP به دلخواه.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS 112 Introduction to Programming Inheritance Hierarchy; Polymorphism Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Inheritance in the Java programming language J. W. Rider.
Database Laboratory: Session #4 Akram Shokri. DB-Lab 2 Lab Activity You must already created all tables You have to have inserted proper data in tables.
به نام خدا برنامه سازی سمت سرور (php)
تکنیک های پیشرفته در برنامه سازی وب ) اسلايد هفتم ) جوانمرد Website: به نام خدا.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
مظفر بگ محمدی دانشگاه ایلام Generics. کدهای عمومی 2 یکی از اهداف OOP ایجاد قابلیت نوشتن برنامه های عمومی با قابلیت استفاده ی مجدد است. کدهای چندریخت تا.
بسم الله الرحمن الرحیم برنامه سازی پیشرفته ( جاوا ) مدرس : محمد غمگسار
Inheritance ndex.html ndex.htmland “Java.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
BY:- TOPS Technologies
Data Structures and Algorithms in JAVA Chapter 2.
آشنايي با سيستم اعداد.
Modern Programming Tools And Techniques-I
Object-Oriented Concepts
Inheritance ITI1121 Nour El Kadri.
[c.
Object Oriented Analysis and Design
بنام خدا زبان برنامه نویسی C (21814( Lecture 12 Selected Topics
SY800 router mode [AD-14-TB ].
ارث بری استفاده ی مجدد از کلاس توسط وراثت
چندریختی Polymorphism
مظفر بگ محمدی دانشگاه ایلام
ارث بری استفاده ی مجدد از کلاس توسط وراثت
Review of Object-Oriented Concepts in JAVA
SSO Single Sign-on Systems
1.
آشنایی با پایگاه داده mysql
ارث بری استفاده ی مجدد از کلاس توسط وراثت
ASP.NET فرناز شریعت.
راهنمای استفاده از ابزار Mailings در Ms Word
انجمن جاواکاپ تقدیم می‌کند دوره برنامه‌نويسی جاوا
بسم الله الرحمن الرحیم هرس درخت تصمیم Dr.vahidipour Zahra bayat
مظفر بگ محمدی دانشگاه ایلام
به نام خدا Koha برنامه.
دکتر مظفر بگ محمدی دانشگاه ایلام
مظفر بگ محمدی دانشگاه ایلام
سخت افزارهای امنیتی Hardware Security تهیه و ارایه : یونس جوان.
مباني كامپيوتر و برنامه سازي Basics of Computer and Programming
مباني كامپيوتر و برنامه سازي Basics of Computer and Programming
Review of Object-Oriented Concepts in JAVA
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

درس مهندسی اینترنت – مهدی عمادی مهندسی اینترنت برنامه‌نویسی در اینترنت Object Oriented Programming Concepts in JAVA

درس مهندسی اینترنت مرور مفاهیم برنامه نویسی شئ گرا مفاهیم برنامه نویسی شئ گرا در جاوا مزایای برنامه نویسی شی گرا Inheritance Abstract Classes Interfaces مرور سوالات

درس مهندسی اینترنت جاوا اکثر مفاهيم پایه شی گرا یی را به طور مستقیم پشتیبانی می کند. برای مثال: –Classification: Grouping related things together. classes, inheritance & packages. –Encapsulation: Representing data and the set of operations on the data as a single entity classes –Information Hiding: An object should be in full control of its data, granting specific access only to whom it wishes. –Inheritance: Java allows related classes to be organized in a hierarchical manner extends –Polymorphism: Same code behaves differently at different times during execution. dynamic binding. مفاهیم برنامه نویسی شئ گرا در جاوا

درس مهندسی اینترنت مزایای شی گرایی استفاده از برنامه نویسی شی گرا مزایای زیادی دارد. برای نمونه: – استفاده مجدد از تکه کدها ( Reusability ): inheritance و Polymorphism برای کدهای مشابه با تفاوتهای اندک – قابلیت گسترش ( Extensibility ): می توان قابلیت و زیر سیستم جدید اضافه نمود بدون تغییر قسمتهای دیگر کد – قابلیت پشتیبانی ( Maintainability ): می توان پیاده‌سازی داخلی را تغییر داد بدون تغییر قسمتهای دیگر کد – امنیت ( Security ): information hiding – مجردسازی ( Abstraction ): Classification و Encapsulation این امکان می‌دهند تا از سطح بالا و بدون درنظر گرفتن جزئیات شروع کنیم

درس مهندسی اینترنت مثال می‌خواهیم برای محاسبه حقوق کارمندان یک شرکت یک برنامه بنویسیم

درس مهندسی اینترنت Review of inheritance فرض کنید ما کلاس زیر را داریم: class Employee { protected String name; protected double payRate; public Employee(String name, double payRate) { this.name = name; this.payRate = payRate; } public String getName() {return name;} public void setPayRate(double newRate) { payRate = newRate; } public double pay() {return payRate;} public void print() { System.out.println("Name: " + name); System.out.println("Pay Rate: "+payRate); }

درس مهندسی اینترنت Review of inheritance (contd.) می‌خواهیم یک کلاس دیگر داشته باشیم که نوع خاصی از کلاس Employee است که به صورت ساعتی کار می‌کند که می تواند قسمتی از کد را ارث ببرد: class HourlyEmployee extends Employee { private int hours; public HourlyEmployee(String hName, double hRate) { super(hName, hRate); hours = 0; } public void addHours(int moreHours) {hours += moreHours;} public double pay() {return payRate * hours;} public void print() { super.print(); System.out.println("Current hours: " + hours); }

درس مهندسی اینترنت مطالبی در مورد Inheritance متدها و متغیرهای کلاس والد ( super class ) به وسیله فرزند ارث برده می‌شوند کلاس فرزند می‌تواند خود متدها و متغیرهای خاص خود را اضافه کند. کلاس فرزند می‌تواند برخی از متدهای کلاس والد را باز نویسی ( override ) کند. Constructor ارث برده نمی‌شود ولی می‌توان آن را با کلمه کلیدی super فراخوانی نمود. این فراخوانی در Constructor فرزند و اولین فراخوانی باید باشد. اگر بخواهیم یک متد را که باز نویسی کردیم با همان حالت بیان شده در والد استفاده کنیم باید قبل از اسم آن کلمه super را به کار ببریم. If the constructor of the super class is not called, then the complier inserts a call to the default constructor -watch out!

درس مهندسی اینترنت Review of Abstract Classes Inheritance یک حالت سلسله مراتبی را به وجود می‌آورد که مزیت آن موارد زیر است: – reusability, type sharing and polymorphism Java uses Abstract classes & Interfaces to further strengthen the idea of inheritance. To see the role of abstract of classes, suppose that the pay method is not implemented in the HourlyEmployee subclass. Obviously, the pay method in the Employee class will be assumed, which will lead to wrong result. One solution is to remove the pay method out and put it in another extension of the Employee class, MonthlyEmployee. The problem with this solution is that it does not force subclasses of Employee class to implement the pay method.

درس مهندسی اینترنت Review of Abstract Classes (Cont'd) The solution is to declare the pay method of the Employee class as abstract, thus, making the class abstract. abstract class Employee { protected String name; protected double payRate; public Employee(String empName, double empRate) { name = empName; payRate = empRate; } public String getName() {return name;} public void setPayRate(double newRate) {payRate = newRate;} abstract public double pay(); public void print() { System.out.println("Name: " + name); System.out.println("Pay Rate: "+payRate); }

درس مهندسی اینترنت Review of Abstract Classes (Cont'd) The following extends the Employee abstract class to get MonthlyEmployee class. The next example extends the MonthlyEmployee class to get the Executive class. class MonthlyEmployee extends Employee { public MonthlyEmployee(String empName, double empRate) { super(empName, empRate); } public double pay() { return payRate; }

درس مهندسی اینترنت Review of Abstract Classes (Cont'd) class Executive extends MonthlyEmployee { private double bonus; public Executive(String exName, double exRate) { super(exName, exRate); bonus = 0; } public void awardBonus(double amount) { bonus = amount; } public double pay() { double paycheck = super.pay() + bonus; bonus = 0; return paycheck; } public void print() { super.print(); System.out.println("Current bonus: " + bonus); } Employee HourlyEmployee MonthlyEmployee Executive

درس مهندسی اینترنت Review of Abstract Classes (Cont'd) The following further illustrates the advantages of organizing classes using inheritance - same type, polymorphism, etc. The Program Output public class TestAbstractClass { public static void main(String[] args) { Employee[] list = new Employee[3]; list[0] = new Executive("Jarallah Al-Ghamdi", 50000); list[1] = new HourlyEmployee("Azmat Ansari", 120); list[2] = new MonthlyEmployee("Sahalu Junaidu", 9000); ((Executive)list[0]).awardBonus(11000); for(int i = 0; i < list.length; i++) if(list[i] instanceof HourlyEmployee) ((HourlyEmployee)list[i]).addHours(60); for(int i = 0; i < list.length; i++) { list[i].print(); System.out.println("Paid: " + list[i].pay()); System.out.println("*************************"); }

درس مهندسی اینترنت Review of Interfaces Interface کلاس نیست به کمک interface می توان تعدادی متد Abstract داشت که باید توسط یک کلاس پیاده‌سازی شود بر خلاف Abstract class هیچ یک از متدها نمی‌توانند پیاده‌سازی داشته باشند interface همانند Abstract class برای ایجاد سلسله مراتب به کار می‌رود می‌توان با Interface نوع خاصی از multiple inheritance را می‌توان داشت If both parents of a child implement a method, which one does the child inherits? - Multiple inheritance confusion. Interfaces allow a child to be both of type A and B.

درس مهندسی اینترنت Review of Interfaces (contd.) Recall that Java has the Comparable interface defined as: Recall also that java has the java.util.Arrays class, which has a sort method that can sort any array whose contents are either primitive values or Comparable objects. Thus, to sort our list of Employee objects, all we need is to modify the Employee class to implement the Comparable interface. Notice that this will work even if the Employee class is extending another class or implementing another interface. This modification is shown in the next page. interface Comparable { int compareTo(Object o); }

درس مهندسی اینترنت Review of Interfaces (contd.) abstract class Employee implements Comparable { protected String name; protected double payRate; public Employee(String empName, double empRate) { name = empName; payRate = empRate; } public String getName() {return name;} public void setPayRate(double newRate) { payRate = newRate; } abstract public double pay(); public int compareTo(Object o) { Employee e = (Employee) o; return name.compareTo( e.getName()); } Employee HourlyEmployee MonthlyEmployee Executive Comparable

درس مهندسی اینترنت Review of Interfaces (contd.) Since Employee class implements the Comparable interface, the array of employees can now be sorted as shown below: The program output import java.util.Arrays; public class TestInterface { public static void main(String[] args) { Employee[] list = new Employee[3]; list[0] = new Executive("Jarallah Al-Ghamdi", 50000); list[1] = new HourlyEmployee("Azmat Ansari", 120); list[2] = new MonthlyEmployee("Sahalu Junaidu", 9000); ((Executive)list[0]).awardBonus(11000); for(int i = 0; i < list.length; i++) if(list[i] instanceof HourlyEmployee) ((HourlyEmployee)list[i]).addHours(60); Arrays.sort(list); for(int i = 0; i < list.length; i++) { list[i].print(); System.out.println("Paid: " + list[i].pay()); System.out.println("**********************"); }

درس مهندسی اینترنت Review Questions How does an interface differ from an abstract class? Why does Java not support multiple inheritance? What feature of Java helps realize the benefits of multiple inheritance? An Abstract class must contain at least one abstract method, (true or false)? A subclass typically represents a larger number of objects than its super class, (true or false)? A subclass typically encapsulates less functionality than its super class does, (true or false)? An instance of a class can be assigned to a variable of type any of the interfaces the class implements, (true or false)?