Download presentation
1
Inheritance and Polymorphism
2
Inheritance Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is the mechanism whereby a new class, called a subclass. The subclass is created from an existing class called a superclass.
3
A subclass is bigger than a superclass – it contains more data and more methods. Inheritance provides an effective mechanism for code reuse.
4
A subclass can be a superclass for another subclass, leading to an inheritance hierarchy of classes.
5
Relationship between Objects
Person Student Employee GradStudent UnderGrad
6
The arrows designates the is-a relationship
The arrows designates the is-a relationship. The Employee is–a Person, a Student is-a Person, a GradStudent is-a Student; an UnderGrad is-a Student. The opposite is not necessarily true; a Person may not be a Student, nor is a Student necessarily an UnderGrad.
7
The is-a relationship is transitive: If a GradStudent is-a Student and a Student is-a Person, then a GradStudent is-a Person. Suppose the Person class has instance variables name, socialSecurityNumber, and age and instance methods getName, getSocSecNum, getAge, and printName.
8
Then every one of the derived classes shown inherits these variable and methods. The student class may have additional instance variables, like studentID and gpa, plus a method computeGrade. All of these are inherited by the subclasses GradStudent and UnderGrad.
9
Suppose GradStudent and UnderGrad use different algorithms for computing the course grade. The computeGrade implementation can be redefined in these classes. This is called method overriding.
10
The inheritance relationship between a subclass and a superclass is specified in the declaration of the subclass, using the keyword extends. public class Superclass { } public class Subclass extends Superclass
11
Inheriting Instance Methods & Variables
The UnderGrad and GradStudent subclasses inherit all of the methods and variable of the Student superclass. The student instance variables myName, myTests, and myGrade are private and not directly accessible to the methods in the UnderGrade and GradStudent subclasses.
12
The subclass can, directly invoke the public accessor and mutator methods of the superclass. Both GradStudent and UnderStudent use getTestAverage.
13
If instead of private, access specifier for the instance variable in Student were protected, then the subclasses could directly access these variable. The keyword protected is not part of the AP Java subset.
14
Classes on the same level in a hierarchy diagram do not inherit anything from each other. All they have in common is the identical code they inherit from their superclass.
15
A method in a superclass is overridden in a subclass by defining a method with the same return type and signature. Sometimes the code for overriding a method includes a call to the superclass method. This is called partial overriding. Typically this occurs when the subclass method wants to do what the superclass does, plus extra.
16
Constructors and super
Constructors are never inherited. If no constructor is written for a subclass, the superclass default constructor with no parameters is generated. If the superclass does not have a default constructor, but only a constructor with parameters, a compiler error will occur.
17
If there is a default constructor in the superclass inherited data members will be initialized for the superclass. A subclass constructor can be implemented with a call to the super method, which invokes the superclass constructor.
18
Rules for Subclasses A subclass can add new private instance variables. A subclass can add new public, private or static methods. A subclass can override inherited methods A subclass may not redefine a public method as private. A subclass should define its own constructors A subclass cannot directly access the private members of its superclass. It must use accessor or mutator methods.
19
When a variable of a superclass is declared in a client program, that reference can refer not only to an object of the superclass, but also to objects of any of its subclasses. Each of the following is legal: Student s = new Student(); Student g = new GradStudent(); Student u = new UnderGrad();
20
Student s = new Student(); Student g = new GradStudent(); Student u = new UnderGrad(); This works because a GradStudent is-a Student. GradStudent g = new Student(); UnderGrad u = new Student(); Since a Student is not necessarily a GradStudent or an UnderGrad the above is not valid.
21
Are these valid declarations: Student s = new Student(“Brian Lorenzen”, new int[] {90, 94, 99}, “none”); Student u = new UnderGrad(“Tim Broder”, new int [] {90, 90, 100}, “none”); Student g = new GradStudent(“Kevin Cristella”, new int[], {85, 70, 90}, “none”, 1234);
22
Suppose you make the method call s
Suppose you make the method call s.setGrade(“Pass”); The appropriate method in Student is found and the new grade assigned. The method calls g.setGrade(“Pass”); And u.setGrade(“Pass”); achieve the same effect on g and u since GradStudent and UnderGrad both inherit the setGrade method from Student.
23
Do the following method calls work. int studentNum = s
Do the following method calls work? int studentNum = s.getID(); int underGradNum = u.getID();
24
s.computeGrade(); g.computeGrade(); u.computeGrade();
25
Polymorphism A method that has been overridden in at least one subclass is said to be polymorphic. An example is computeGrade, which is redefined for both GradStudent and underGrad. Polymorphism is the mechanism of selecting the appropriate method for a particular object in a hierarchy.
26
The correct method is chosen because, in Java, method calls are always determined by the type of the actual object, not the type of the object reference. For example, even though s, g and u are all declared as type Student, s.computeGrade(), g.computeGrade();, and u.computerGrade() will all perform the correct operations for their particular instances.
27
Dynamic Binding Making a run-time decision about which instance method to call is known as dynamic binding. In polymorphism, the actual method that will be called is not determined by the compiler.
28
Interfaces An interface is a class with method declarations that have no implementations. An interface cannot be inherited. An interface can add behavior to a class, but it does not provide a hierarchy for the class. It may only be implemented in a class. Methods defined in an interface are by default public and abstract. The methods in an interface are only declarations followed by a semicolon.
29
The comparable interface is part of the java.lang.package.
The method is compareTo(Object obj). Returns 0 when obj is the same as the object Returns a negative integer when obj is less than the object Returns a positive integer when obj is greater than the object. Based on this if you missed it on the last test. I will add the points to your score.
30
When an interface in implemented in a class, the class must implement each method defined in the interface.
31
Critical Thinking Page 235 – Critical Thinking Question 6 and Question 7. Turn in when you finish. Move the following files: Clarinet Music Piccolo Woodwind Instrument Vocal Performance Draw out the relationship between these files Review: Music – part 1 of 2
32
Puck Create a Puck class that inherits the Disk class. The Puck class should include member variables weight, standard and youth. The standard and youth variables should be boolean variables that are set to either true or false depending on the weight of the puck. A standard puck weighs between 5 and 5.5 ounces. A youth puck weighs between 4 and 4.5 ounces. Official hockey pucks, regardless of weight, are one inch-thick with a three-inch diameter. The Puck class should also contain member methods getWeight(), getDivision(), which returns a string stating whether the puck is standard or youth, and equals() and toString(), which override the same methods in the Disk class. The Puck constructor should require an argument for weight. Be sure that the constructor initializes other variables to appropriate values as necessary. Create a Hockey application that tests the Puck class.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.