مظفر بگ محمدی دانشگاه ایلام

Slides:



Advertisements
Similar presentations
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Advertisements

Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
Inheritance. Inheritance  New class (derived class) is created from another class (base class).  Ex. EmployeeEmployee  HourlyEmployee  SalariedEmployee.
Inheritance Juan Marquez 12_inheritance.ppt
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
Chapter 8 Polymorphism and Abstract Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 8: – More about Inheritance When to use inheritance Relationship between classes Rules to follow Copyright ©
CS102--Object Oriented Programming Lecture 9: – Polymorphism Copyright © 2008 Xiaoyan Li.
Chapter 7 Inheritance Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
CS102--Object Oriented Programming Lecture 10: – Abstract Classes Copyright © 2008 Xiaoyan Li.
 Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
CS 2430 Day 9. Announcements Quiz on Friday, 9/28 Prog1: see , see me as soon as possible with questions/concerns Prog2: do not add any public methods.
Chapter 8 Polymorphism and Abstract Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
JAVA Object Oriented Programming. Objectives Be able to implement inheritance using extends, super, and abstract Be able to describe differences between.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/14) Object Oriented Programming Joel Adams and Jeremy Frens Calvin.
CMSC 202 Inheritance I Class Reuse with Inheritance.
Example: O-O Payroll Program (§11.4) Object-Oriented Design Behavior. Our program should read a sequence of employees from an input file, ( managers, secretaries,
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Class Relationships A class defines a type of data Composition allows an object of another class to define an attribute of a class –Employee “has a”
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.
CS100A, 15 Sept Lecture 5 1 CS100A, 5 Sept This lecture continues the discussion of classes. The important new concept of this lecture is.
CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Abstract methods and classes. Abstract method A placeholder for a method that will be fully defined in a subclass.
Polymorphism and Abstract Classes
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Abstract Classes.
Abstract Data Types and Java Classes
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
Inheritance 2nd Lecture
ارث بری 2 استفاده ی مجدد از کلاس توسط وراثت
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Polymorphism 2nd Lecture
Inheritance I Class Reuse with Inheritance
ارث بری 2 استفاده ی مجدد از کلاس توسط وراثت
Inheritance 2nd Lecture
CMSC 202 Inheritance.
Inheritance.
Class Reuse with Inheritance
Inheritance 2nd Lecture
Abstract methods and classes
مظفر بگ محمدی دانشگاه ایلام
Object class.
Object Oriented Programming.
CMSC 202 Inheritance II.
Chapter 8 Abstract Classes.
Presentation transcript:

مظفر بگ محمدی دانشگاه ایلام کلاسهای انتزاعی مظفر بگ محمدی دانشگاه ایلام

کلاسهای انتزاعی public class Employee { private String name; private Date hireDate; // constructors, accessors, mutators, equals, toString } public class HourlyEmployee extends Employee private double wageRate; private double hours; //for the month public double getPay( ) {return wageRate * hours;} public class SalariedEmployee extends Employee private double salary; //annual public double getPay() { return salary / 12; }

متد samePay فرض کنید می خواهیم بدانیم که آیا دریافتی دو کارمند یکسان است؟ لذا متد samePay را به کلاس اضافه Employee می کنیم. این متد باید بتواند مقدار دریافتی هر دو نوع کارمندی را با هم مقایسه کند. public boolean samePay(Employee other) { return(this.getPay() == other.getPay()); }

مشکلات samePay متد samePay متد getPayرا صدا می زند. راه حل: متد getPay در کلاسهای SalariedEmployees و HourlyEmployeesتعریف شده است. اما در کلاس عمومی Employeeتعریف نشده است. چون تعریف این متد در کلاس Employee بی معنی است. راه حل: هر کلاسی که از Employee مشتق می شود، باید متد samePay را خودش پیاده کند. جاوا این کار را از طریق متدهای انتزاعی انجام می دهد.

متدهای انتزاعی متد انتزاعی مثل یک ظرف است که محتوای آن در کلاسهای مشتق شده مشخص می شود. پیاده سازی متد را به تعویق می اندازد. اما امضای آن کامل است و کلمه ی abstract به آن اضافه شده است. نمی تواند private باشد. بدنه ندارد و به جای بدنه یک ; قرار می گیرد. public abstract double getPay(); public abstract void doIt(int count); بدنه ی متد در کلاسهای مشتق شده تعریف خواهد شد. کلاسی که یک یا چند متد انتزاعی داشته باشد، یک کلاس انتزاعی است. 5

کلاس انتزاعی کلاسی که یک یا چند متد انتزاعی داشته باشد، یک کلاس انتزاعی است. کلاس انتزاعی باید در سرآیند کلاس از کلمه ی کلیدی abstract استفاده کند. public abstract class Employee { private instanceVariables; . . . public abstract double getPay(); } 6

کلاس انتزاعی یک کلاس انتزاعی می تواند هر تعداد متد انتزاعی و/یا متد کاملاً تعریف شده داشته باشد. اگر کلاس مشتق شده از یک کلاس انتزاعی، تمام متدهای انتزاعی را تعریف نکند یا یک یا چند متد انتزاعی به تعریف کلاس اضافه کند: کلاس مشتق شده نیز انتزاعی است و باید از abstract در سرآیند خود استفاده کند. کلاسی که متد انتزاعی نداشته باشد، یک کلاس concrete است. 7

کلاس انتزاعی Employee public abstract class Employee { private String name; private Date hireDate; public abstract double getPay( ); // constructors, accessors, mutators, equals, toString public boolean samePay(Employee other) return(this.getPay() == other.getPay()); }

شما نمی توانید از کلاسهای انتزاعی شی ایجاد کنید. کلاس انتزاعی برای تولید کلاسهای خصوصی تر استفاده می شود. اگر چه توصیف کلاس کارمند بصورت عمومی امکان پذیر است، اما در واقعیت هر کارمند باید حقوق ثابت یا ساعتی داشته باشد. نمی توان از جزء سازنده ی کلاس انتزاعی برای ایجاد یک شی انتزاعی استفاده کرد. اما کلاس مشتق شده می تواند جزء سازنده ی کلاس انتزاعی را (بصورت صریح یا ضمنی) از طریق super فراخوانی کند. 9

کلاس انتزاعی یک نوع است اگر چه نمی توانید از کلاسهای انتزاعی برای ایجاد شی استفاده کنید، اما می توان پارامترهایی از نوع کلاس انتزاعی داشت. بعداً می توان یک شی از نوع یکی از کلاسهای مشتق شده را به عنوان آرگومان به متد ارسال کرد. همچنین می توان یک متغییر از نوع کلاس انتزاعی تعریف نمود و از آن برای نامگذاری کلاسهای concrete مشتق شده استفاده کرد. 10