Skip to content

Latest commit

 

History

History
103 lines (85 loc) · 6.57 KB

T103-23102018.md

File metadata and controls

103 lines (85 loc) · 6.57 KB

LESSON 03 - TOPIC 1: Inheritance & Polymorphism

Accompanying code files:

Jump to Homework

Inheritance - Superclasses + Subclasses

  • as one would inherit certain attributes from a parent, we might want to establish a similar relationship between similar objects e.g. objects that are a subset or type/kind of another object
  • inheritance refers to a relationship between objects that share characteristics, specifically where a new class (the subclass) is created from an existing class (the superclass)
  • the subclass inherits the state and behaviour of the superclass, but alters them with features unique to the new class
  • inheritance is an effective way to reuse and refactor code that is similar, especially if the code for the superclass has been tested and debugged, so the only new code required for the subclass are any of its additional characteristics

Inheritance Hierarchy

  • a subclass can be a superclass for another subclass, creating an inheritance hierarchy
  • inheritance relationships are also called is-a relationships, where a GreatBall is-a PokeBall, and an UltraBall is-a Pokeball etc.
  • these relationships are transitive i.e. if a GradStudent is-a Student and a Student is-a Person, then a GradStudent is-a Person

image

Rules for Subclasses

  1. a subclass inherits the public variables and methods of its superclass, but it cannot redefine them as private, and does not inherit the private instance variables and methods of is superclass
  2. a subclass this cannot directly access the private members of its superclass; only by using accessor or mutator methods
  3. a subclass may have additional private/public/static methods and instance variables that are not in the superclass
  4. a subclass should define its own constructors. A subclass constructor can be implemented with a call to the super method, which invokes the superclass constructor.
  5. a subclass may override a method it inherits (method-overriding, or partial-overriding if part of the original code is retained), UNLESS it is a static method. The super method can also be used to invoke the corresponding method in the superclass.
public class GradStudent extends Student { // the keyword extends tells the compiler that GradStudent is-a Student

  public GradStudent() { // default constructor
    super(); // uses the default constructor of the Student class, must be used in first line if used
    gradID = 0; // initializes the gradID field that is unique to the GradStudent class
  }

  public GradStudent(String name, int[] tests, String grade, int gradID) { // parameterized constructor
    super(name, tests, grade); // uses the parameterized constructor of the Student Class, must be used in first line if used
    this.gradID = gradID; // initializes the gradID field that is unique to the GradStudent class
  }

  public void computeGrade() {
    super.computeGrade(); // invokes computeGrade in the Student class
    if (getTestAverage() >= 90) // additional code not in the computeGrade method of the Student class
      setGrade("Pass with distinction");
  }

}

Declaring Subclass Objects

  • since a GradStudent is-a Student, the following declarations are legal:
Student s = new Student(); // VALID
Student g = new GradStudent(); // VALID
  • and the following declaration is NOT legal:
GradStudent g = new Student(); // INVALID
  • when you call a method e.g. setGrade that is present in both the Student and GradStudent classes, the following are legal:
s.setGrade("Pass"); // VALID
g.setGrade("Pass"); // VALID
  • however, if you call a method e.g. getID that is present in the GradStudent class but not in the Student class, it will NOT work
s.getID(); // INVALID

Polymorphism

  • a method that has been overridden in at least one subclass is said to be polymorphic
  • polymorphism is the mechanism of selecting the appropriate method for a particular object in a class hierarchy
  • 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
  • in Java, the selection of the correct method occurs during the run-time execution of the program

Dynamic Binding (Late Binding) vs. Static Binding (Early Binding)

  • static binding involves selecting the correct overloaded instance method to use at compile time, where the compiler determines the correct method to use by comparing their signatures
  • dynamic-bindng is when the decision about which overridden instance method to call takes place during run-time
  • basically, the compiler determines if a method can be called (i.e. is it legal?), while the run-time environment determines how it will be called (i.e. which overridden form should be used?)

Type Compatibility (Downcasting)

Student s = new GradStudent();
GradStudent g = new GradStudent();
int x = s.getID();  // causes a compile-time error
int y = g.getID();  // legal
  • in the code above, although both s and g are GradStudent objects, s is of the type Student, and the Student class does not have the getID() method i.e. it is not polymorphic
  • this means that in compile time, only the nonprivate methods of the Student class can be invoked on s
  • the error above can be fixed by casting s to the correct type:
int x = ((GradStudent) s).getID(); // this is possible since s is actually a GradStudent object
  • casting a superclass to a subclass is called downcasting

HOMEWORK

Assignments:

  • read over Activity 2 of the Elevens Lab
  • based on what we did for the Card.java(solution) class in our first lab last lesson, complete the isEmpty and size accessor methods. HINT: You will need to use one of the methods from the AP Exam Java Quick Reference

Prep for Next Class:

  • review the lecture notes for lessons 1-3 as we will be working on some questions in class