ch13 - Chapter 13 Inheritance and Polymorphism Topics...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 13 Inheritance and Polymorphism
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Topics Inheritance Access Modifiers Abstract Classes
Background image of page 2
Defining Classes with Inheritance To explain the concept of inheritance, we will consider an example of a class roster. The class roster should contain both undergraduate and graduate students. Each 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.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Defining Classes with Inheritance There 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.
Background image of page 4
Defining Classes with Inheritance If two object types 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.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Student Classes For this example, we will design three classes: Student UndergraduateStudent GraduateStudent The Student class will incorporate behavior and data common to both UndergraduateStudent and GraduateStudent objects. The UndergraduateStudent class and the GraduateStudent class will each contain behaviors and data specific to their respective objects.
Background image of page 6
Access Modifiers The 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.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Student Classes The UndergraduateStudent and GraduateStudent classes are descendants of the Student class. A subclass extends its superclass.
Background image of page 8
UML Diagram A superclass Student and its subclasses GraduateStudent and UndergraduateStudent .
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Polymorphism Polymorphism allows a single variable to refer to objects from different classes. For example, if we declare Student student; We can say student = new Student(); student = new GraduateStudent(); and student = new UndergraduateStudent();
Background image of page 10
Polymorphism A 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.
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
We can maintain our class roster using an array, combining objects from the Student , UndergraduateStudent , and GraduateStudent classes. Student roster [40];
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 48

ch13 - Chapter 13 Inheritance and Polymorphism Topics...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online