Tài liệu Bài giảng Chapter 13: Inheritance and Polymorphism: Chapter 13Inheritance and Polymorphism©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Chapter 13 ObjectivesAfter you have read and studied this chapter, you should be able toWrite programs that are easily extensible and modifiable by applying polymorphism in program design.Define reusable classes based on inheritance and abstract classes and abstract methods.Define methods, using the protected modifier.Parse strings, using a String Tokenizer object.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.1 Defining Classes with InheritanceTo explain the concept of inheritance, we will consider an example of a class roster.The class roster should contain both undergraduate and graduate students.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.1 Defining Classes with InheritanceEach student’s record will contain his or her name, three test scores, and the final course grade.The formula for determi...
59 trang |
Chia sẻ: honghanh66 | Lượt xem: 784 | Lượt tải: 0
Bạn đang xem trước 20 trang mẫu tài liệu Bài giảng Chapter 13: Inheritance and Polymorphism, để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
Chapter 13Inheritance and Polymorphism©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Chapter 13 ObjectivesAfter you have read and studied this chapter, you should be able toWrite programs that are easily extensible and modifiable by applying polymorphism in program design.Define reusable classes based on inheritance and abstract classes and abstract methods.Define methods, using the protected modifier.Parse strings, using a String Tokenizer object.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.1 Defining Classes with InheritanceTo explain the concept of inheritance, we will consider an example of a class roster.The class roster should contain both undergraduate and graduate students.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.1 Defining Classes with InheritanceEach student’s record will contain his or her name, three test scores, and the final course grade.The formula for determining the course grade is different for graduate students than for undergraduate students.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.1 Defining Classes with InheritanceThere are two broad ways to design the classes to model undergraduate and graduate students.We can define two unrelated classes, one for undergraduates and one for graduates.We can model the two kinds of students by using classes that are related in an inheritance hierarchy.Two classes are unrelated if they are not connected in an inheritance relationship.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.1 Defining Classes with InheritanceIf two objects are expected to share common behaviors and data, it is better to design their classes using inheritance.Using unrelated classes in such an instance will result in duplicating code common to both classes.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.1 Defining Classes with InheritanceFor this example, we will design three classes:StudentUndergraduateStudentGraduateStudentThe Student class will incorporate behavior and data common to both UndergraduateStudent and GraduateStudent objects.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.1 Defining Classes with InheritanceThe UndergraduateStudent class and the GraduateStudent class will each contain behaviors and data specific to their respective objects.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.1 Defining Classes with Inheritance/*File: Student.java*/class Student {protected final static int NUM_OF_TESTS = 3;protected String name;protected int[] test;protected String courseGrade;public Student( ) { this("No Name");}public Student(String studentName) { name = studentName; test = new int[NUM_OF_TESTS]; courseGrade = "****"; }©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.1 Defining Classes with Inheritancepublic String getCourseGrade( ) { return courseGrade;}public String getName( ) { return name;}public int getTestScore(int testNumber) { return test[testNumber-1];}public void setName(String newName) { name = newName;}public void setTestScore(int testNumber, int testScore) { test[testNumber-1] = testScore; }}©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.1 Defining Classes with InheritanceThe modifier protected makes a data member or method visible and accessible to the instances of the class and the descendant classes. Public data members and methods are accessible to everyone.Private data members and methods are accessible only to instances of the class.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.1 Defining Classes with InheritanceThe UndergraduateStudent and GraduateStudent classes are descendants of the Student class.A subclass extends its superclass.Following are the class definitions for the UndergraduateStudent and GraduateStudent classes.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.1 Defining Classes with Inheritance/*File: UndergraduateStudent.java*/class UndergraduateStudent extends Student {public void computeCourseGrade() { int total = 0; for (int i = 0; i = 70) { courseGrade = "Pass"; } else { courseGrade = "No Pass"; } }©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.1 Defining Classes with Inheritance/*File: GraduateStudent.java*/class GraduateStudent extends Student {public void computeCourseGrade() { int total = 0; for (int i = 0; i = 80) { courseGrade = "Pass"; } else { courseGrade = "No Pass"; } }}©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 13.1A superclass Student and its subclasses GraduateStudent and UndergraduateStudent.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.2 Using Classes Effectively with PolymorphismPolymorphism allows a single variable to refer to objects from different classes.For example, if we declareStudent student;We can saystudent = new Student();student = new GraduateStudent();and student = new UndergraduateStudent();©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.2 Using Classes Effectively with PolymorphismA variable of class X may not refer to an object from the superclass or sibling classes of X.Sibling classes are those that share the common ancestor class.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.2 Using Classes Effectively with PolymorphismWe can maintain our class roster using an array, combining objects from the Student, UndergraduateStudent, and GraduateStudent classes.Student roster [40];roster[0] = new GraduateStudent();roster[1] = new UndergraduateStudent();roster[2] = new UndergraduateStudent();and so on.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 13.2The roster array with elements referring to instances of GraduateStudent or UndergraduateStudent classes.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.2 Using Classes Effectively with PolymorphismTo compute the course grade using the roster array, we executefor (int i=0; i=50){ courseGrade = “Pass”; }else{ courseGrade = “No Pass”; } }...}This design allows us to create an instance of Student to represent a nonregular student.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.5 Abstract Superclasses and Abstract MethodsIn the second approach, we leave the Student class abstract. We define a third subclass, OtherStudent:class OtherStudent extends Student { public void computeCourseGrade(){ int total = 0; for (int i=0; i=50){ courseGrade = “Pass”; }else{ courseGrade = “No Pass”; } }}©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 13.7A program diagram of the abstract superclass Student and its three subclasses.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.5 Abstract Superclasses and Abstract MethodsThe best approach depends on the particular situation. When considering design options, we can ask ourselves which approach allows easier modification and extension. Finally, private methods and static methods may not be declared abstract.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.6 Inheritance versus InterfaceJava interface and inheritance both model an IS-A relationship:class ButtonHandler implements ActionListenerClass SavingsAccount extends AccountWe say “ButtonHandler is an ActionListener” and “SavingsAccount is an Account.”©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.6 Inheritance versus InterfaceHowever, their uses are very different.The Java interface is used to share common behavior (only method headers) among the instances of different classes.Inheritance is used to share common code (including both data members and methods) among the instances of related classes. ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.13.6 Inheritance versus InterfaceIn your program designs, remember to use the Java interface to share common behavior. Use inheritance to share common code.If an entity A is a specialized form of another entity B, then model them by using inheritance. Declare A as a subclass of B.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Các file đính kèm theo tài liệu này:
- chapter_13_0222.ppt