ارث بری 2 استفاده ی مجدد از کلاس توسط وراثت

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

Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
Object class.
Inheritance. Inheritance  New class (derived class) is created from another class (base class).  Ex. EmployeeEmployee  HourlyEmployee  SalariedEmployee.
COP 3003 Object-Oriented Programming - Inheritance Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Lecture 10: Inheritance Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods.
CS102--Object Oriented Programming Lecture 8: – More about Inheritance When to use inheritance Relationship between classes Rules to follow Copyright ©
Chapter 7 Inheritance Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Some Quick Reviews of Java. Background Java was developed in the early 90s by Sun Microsystems Java is a high-level language Java programs are portable.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CMSC 202 Inheritance II. Version 10/102 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Chapter 8 Polymorphism and Abstract Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/14) Object Oriented Programming Joel Adams and Jeremy Frens Calvin.
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
CMSC 202 Inheritance I Class Reuse with Inheritance.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
CSC142 NN 1 CSC 142 Overriding methods from the Object class: equals, toString.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Abstract methods and classes. Abstract method A placeholder for a method that will be fully defined in a subclass.
יסודות מדעי המחשב – תרגול 6
Object-Oriented Concepts
COMP 2710 Software Construction Inheritance
Polymorphism 2nd Lecture
Lecture 10: Inheritance Subclasses and superclasses
Inheritance 2nd Lecture
ارث بری 2 استفاده ی مجدد از کلاس توسط وراثت
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Polymorphism 2nd Lecture
null, true, and false are also reserved.
Comp 249 Programming Methodology
Inheritance I Class Reuse with Inheritance
Comp 249 Programming Methodology
Inheritance 2nd Lecture
CMSC 202 Inheritance.
Inheritance.
Class Reuse with Inheritance
Inheritance 2nd Lecture
Abstract methods and classes
مظفر بگ محمدی دانشگاه ایلام
مظفر بگ محمدی دانشگاه ایلام
Overriding methods from the Object class: equals, toString
Object class.
CMSC 202 Inheritance II.
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
CS100J Final Class on Classes 10 February 2005
Presentation transcript:

ارث بری 2 استفاده ی مجدد از کلاس توسط وراثت مظفر بگ محمدی دانشگاه ایلام

آیا اجزاء سازنده به ارث برده می شوند؟ نمی توان از سازنده ی Employee برای ایجاد اشیاء HourlyEmployee استفاده کرد. ما باید برای HourlyEmployees یک سازنده ی مناسب ایجاد کنیم. اما سازنده ی HourlyEmployee چگونه می تواند متغییرهای خصوصی کلاس Employee را مقداردهی اولیه نماید در حالی که دسترسی مستقیم به آنها ندارد؟

سازنده ی super کلاس مشتق شده می تواند به شکل زیر از سازنده ی کلاس پایه برای مقداردهی متغییرهای خصوصی به ارث برده شده از کلاس پایه استفاده کند. public DerivedClass(int p1, int p2, double p3) { super(p1, p2); derivedClassInstanceVariable = p3; } در مثال فوق، دستور super(p1, p2); سازنده ی کلاس پایه را فراخوانی می کند. 3

سازنده ی super برای فراخوانی سازنده ی کلاس پایه نمی توانید از اسم کلاس پایه استفاده کنید و باید از کلمه ی کلیدی super استفاده کنید. فراخوانی super باید اولین کاری باشد که در سازنده انجام می شود. نمی توان متغییرهای کلاس را به عنوان پارامتر به super ارسال کرد. 4

سازنده ی super اگر یک کلاس مشتق شده متد super را فراخوانی نکند، سازنده ی بدون آرگومان کلاس پایه بصورت خودکار فراخوانی می گردد. اگر کلاس پایه سازنده ی بدون آرگومان نداشته باشد، منجر به تولید خطا می شود. چون متغییرهای به ارث برده شده باید مقداردهی اولیه شوند، و سازنده ی کلاس پایه برای این کار طراحی شده است، لذا باید همیشه super را صریحاً فراخوانی کرد. 5

سازنده ی HourlyEmployee public class HourlyEmployee extends Employee { private double wageRate; private double hours; // for the month // the no-argument constructor invokes // the Employee (super) no-argument constructor // to initialize the Employee instance variables // then initializes the HourlyEmployee instance variables public HourlyEmployee( ) super( ); wageRate = 0; hours = 0; }

سازنده ی HourlyEmployee // the alternative HourlyEmployee constructor invokes an // appropriate Employee (super) constructor to initialize // the Employee instance variables (name and date), and then // initializes the HourlyEmployee rate and hours public HourlyEmployee(String theName, Date theDate, double theWageRate, double theHours) { super(theName, theDate); if ((theWageRate >= 0) && (theHours >= 0)) wageRate = theWageRate; hours = theHours; } else System.exit(0);

دسترسی به متد کلاس پایه که بازنویسی شده است. می توان با استفاده از super در هنگام تعریف متدها در کلاس مشتق شده، متدهای بازنویسی شده ی کلاس پایه را نیز فراخوانی کرد. // HourlyEmployee’s toString( ) might be public String toString( ) { return (super.toString() + "$" + getRate( )); } به هر حال، بجز در داخل تعریف کلاس، در جای دیگری نمی توان نسخه ی کلاس پایه ی یک متد بازنویسی شده را فراخوانی کرد. 8

نمی توان از super به صورت تودرتو استفاده کرد به عنوان مثال، کد زیر غلط است. super.super.toString() // ILLEGAL! 9

هر شی از کلاس مشتق شده بیش از یک نوع دارد هر شی که از نوع کلاس مشتق شده باشد، از نوع کلاس پایه ی مربوطه هم هست. بطور کلی، هر شی کلاس مشتق شده از نوع تمام اجداد خود نیز هست. بنابراین، این شی را می توان به هر متغییری از نوع اجداد نیز منتسب کرد. 10

هر شی از کلاس مشتق شده بیش از یک نوع دارد یعنی می توان شی کلاس مشتق شده را بعنوان پارامتر به جای کلاسهای اجدادش به متدها ارسال کرد. در حقیقت، شی کلاس مشتق شده را می توان در هر جایی که یک شی از نوع اجدادش استفاده شده است، استفاده نمود. دقت کنید در این رابطه حالت برعکس صحیح نیست. یعنی نمی توان یک شی از نوع اجداد را به جای یک شی کلاس مشتق شده استفاده کرد. 11

خلاصه ی کلاس پایه/مشتق شده فرض کنید که کلاس D از کلاس B مشتق شده است. هر شی از نوع D از نوع B نیز هست ولی عکس آن صحیح نیست. D نسخه ی خصوصی تر B است. هر جایی که از B استفاده شده است، از D نیز می توان استفاده کرد.

دسترسی Protected می توان هنگام تعریف یک متد یا متغییر از protected به جای public یا private استفاده کرد. در این صورت، می توان در جاهای زیر آنرا با اسم فراخوانی کرد: داخل تعریف کلاس خودش داخل هر کلاسی که از آن مشتق می شود. داخل هر کلاسی که بسته ی آن با کلاس ما یکسان است. حفاظت protected نسبت به private ضعیف است. می توان با تعریف کلاسهای مشتق شده متغییرهای protected را فراخوانی کرد. بنابراین، متغییرهای خصوصی را معمولاً با protected علامت نمی زنند. 13

راهنمایی: متغییرهای استاتیک به ارث برده می شوند. متغییرهای استاتیک کلاس پایه توسط همه ی کلاسهای مشتق شده به ارث برده می شوند. معنای public, private و protected برای متغییرهای استاتیک شبیه متغییرهای معمولی است. 14

کلاس Object در جاوا کلاس Object جد تمام کلاسها است. کلاس Object در بسته ی java.lang قرار دارد که همیشه بصورت اتوماتیک فراخوانی می شود. لذا می توان متدهای نوشت که آرگومان آنها از نوع Object باشد. این پارامتر را بعداً می توان با هر شی از هر کلاسی جایگزین کرد. 15

کلاس Object کلاس Object متدهایی دارد که توسط تمام کلاسهای دیگر به ارث برده می شود. مثل متدهای equals و toString هر شی این متدها را از اجداد خود به ارث می برد. می توان این متدها را در کلاسهای مشتق شده بازنویسی کرد. بعضی کتابخانه های جاوا فرض می کنند که این متدها حتماً بازنویسی می گردند. 16

روش درست تعریف متد equals چون متد equals همیشه از Object به ارث برده می شود، متدهایی شبیه این متد آنرا بارگذاری می کنند. public boolean equals(Employee otherEmployee) { . . . } اما این متد به جای بارگذاری، باید بازنویسی شود public boolean equals(Object otherObject) 17

روش درست تعریف کلاس equals پارامتر otherObject از نوع Objectرا باید با تبدیل نوع تغییر دهد. به هر حال، متد جدید این کار را فقط در صورتی باید انجام دهد که otherObject یک شی از همین کلاس باشد و مقدار آن null نباشد. باید تمام متغییرهای هر دو شی را با هم مقایسه نماید. 18

یک متد equals بهتر برای کلاس Employee public boolean equals(Object otherObject) { if(otherObject == null) return false; else if(getClass( ) != otherObject.getClass( )) else Employee otherEmployee = (Employee)otherObject; return (name.equals(otherEmployee.name) && hireDate.equals(otherEmployee.hireDate)); } 19

متد getClass() هر شی متد getClass()را از کلاس Object به ارث می برد. این متد بصورت finalتعریف شده است، یعنی قابل بازنویسی نیست. فراخوانی getClass() باعث می شود که کلاس مورد استفاده در هنگام ایجاد شی (توسط دستور new) برگردد. خروجی این متد را می توان با عملگرهای == و != مقایسه نمود تا ببینیم که کلاس دو شی برابر هستند یا نه؟ (Object1.getClass() == Object2.getClass()) 20