Classes – Part 4

Part 4 – Unit 4: Classes (Computer Science 30S)

If you don’t feel like reading this web page, here are two alternatives:

  • Read this presentation of the same content (slides 46 – 59)
  • Watch this playlist of videos of the same content on YouTube

Content Sections:

  1. Class Association
  2. Polymorphism

Class Association

  • As previously stated, classes can have an association relationship (the “has a” relationship) without using inheritance (the “is a” relationship) – let’s expand our example by adding two more classes:



  • Let’s code it…


  • The added class, it’s property and the constructor method
  • The other methods including the over-ridden talk() method


  • The next added class, it’s property and the constructor method


  • The other methods including the over-ridden talk() method



  • The word polymorphism comes from Greek: “polys: many, much“ and “morphē: form, shape“
  • In biology, polymorphism refers to a principle in which an organism or species can have more than one form (or morph)
  • This has similar meaning in Computer Science, and the most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object
  • This concept will be expanded on in the Computer Science 40S course, but lets apply this to our example with another new class…
  • The added class, it’s properties and the constructor method


  • The other methods


  • Now let’s test all this in main() …


  • Continued…


  • And then it’s output…


  • The concept of classes, objects, inheritance, encapsulation, polymorphism, and abstraction are all expanded on in the Computer Science 40S course

The NEXT thing to work on is the “Classes assessments” on the assessment page, and when complete move to read/watch the (optional) “Graphics” content page, or (optional) “Programming Extras” content page – or you can go directly to the final assessment on the assessments page