hi

Cards (205)

  • Inheritance
    Defining a new class based upon an existing class, where the new class inherits (almost) everything from the existing class, and can define additional fields and/or methods
  • Superclass
    The existing class that a new class is defined upon
  • Subclass
    The new class that inherits from the existing superclass
  • Inheritance
    • Makes programming easier by building upon an existing class instead of starting from scratch
    • Makes processing objects of subclasses easier via inclusion polymorphism
  • Encapsulation
    Good for 'has a' relationship between objects
  • Inheritance
    Good for 'is a' relationship between objects
  • In Processing, Java, P5js, a child can inherit from only one parent class. This is called single inheritance.
  • Inheritance hierarchy
    • A set of classes with different abstraction levels can be connected by is-a relationship
  • When to use Inheritance (Abstraction)

    • When two objects are similar to some degree, they may be of the same kind. Think conceptually what they are in real or abstracted world (program).
    • If you find that when creating a new class you're repeating a lot of fields and methods that some other classes also possess, you should abstract out those common properties and behaviors into a general class, and then make each of these classes inherit from the general class.
  • To implement Inheritance
    1. Create a general class to house the common properties and behaviors (superclass or parent class)
    2. Define some classes as more specific versions of this general class – known as subclasses or child classes
    3. The child classes would add in their own properties and methods beyond inheriting common ones from the parent class
  • super
    A keyword that refers to the superclass portion of an object
  • this
    A keyword that refers to the current object as a whole
  • Calling Superclass' Constructor
    1. The superclass constructor is called using super(arguments) as the first statement in the child class constructor
    2. The call must follow the signature of the parent class constructor, passing in arguments if the signature comes with parameters
  • When a subclass overrides a method of its superclass
    At runtime, when a child object calls the method, it's the child's version that gets executed (overriding its parent version)
  • Polymorphism
    The ability to create a variable or method that has more than one form
  • Overriding polymorphism
    A subclass can redefine its parent's method with exactly the same signature
  • Inclusion polymorphism
    Not covered in this material
  • show()
    1. console.log(this.title + ", " + this.length + " min. available:" + this.avail)
    2. console.log("director: " + this.director + ", rating: " + this.rating)
  • Optimize the code to remove redundancy
  • show()
    console.log(this.title + ", " + this.length + " min. available:" + this.avail)
  • Movie.show()
    1. super.show()
    2. console.log("director: " + this.director + ", rating: " + this.rating)
  • Instantiate objects from both classes and call their show method
  • Overriding
    Redefining a method with the same signature, done in the context of inheritance: child class vs. parent class
  • Overloading
    (NOT in Javascript) Reusing the same method name to create methods with different signatures, mostly happens within the same class
  • Overriding polymorphism
    • Child classes redefine a method of parent class, ending up with many forms for the same method signature, which all behave differently at runtime
  • Inclusion polymorphism
    A single variable can be used to reference an object of different classes that are related by inheritance
  • When a parent class variable is used to call a method, the method executed depends on the object that the variable is currently referring to
  • Although referencing a subclass object using the parent class type is totally legal, the reverse is not true
  • Pros of Inclusion Polymorphism
    • All instances of its subclasses can be treated as the parent class type, and put into an array/ArrayList typed with the parent class
    • A method parameter typed with the parent class can accept instances of any of its subclasses as argument
  • CoolParticle extends Particle, and overrides the drawMe() method to draw with its color and diameter
  • CoolParticle objects do not have accurate boundary detection because they count on Particle's update() method for wall detection, which is based on fixed diameter
  • Overriding Particle's update() method
    1. Update position
    2. Check walls and reverse velocity if hit
  • Avoiding Stuck-at-Edge issue in CoolParticle's update()
    1. Update position
    2. Check left/right edges and enforce position at edge
    3. Check top/bottom edges and enforce position at edge
  • Another Major Advantage of Inclusion Polymorphism
    • A method parameter typed with the parent class can accept instances of any of its subclasses as argument
  • A parameter of a superclass type can accept an object of any of its subclasses as argument
  • A method can deal with all subclasses of a superclass by using a parameter of the superclass type
  • Exam scheduled on April 16, 19:00-22:00, SRY 5280
  • This Week
    • Use p5.Vector to store motion parameters
    • Keyboard control: store its state using boolean variables
    • Collision detection between objects
    • Detect with enclosing circle
    • Detect using bounding box
    • Algorithm for detecting collision among a collection of objects
    • Use nested loops
    • Case study: UFO shoots enemies
  • Vector
    An entity that has a magnitude and direction
  • p5.Vector
    A class used to hold values in a collection, good for position, velocity, or acceleration