pog: PolymOrphismisGreat
Disclaimer: This document is meant to supplement other studying materials, not replace them
- Everything covered last week has a high likelihood of being on an exam
- References and Polymorphism
You can use a code playground on the CS 125 website to follow along and test the code! https://cs125.cs.illinois.edu/
What is the difference between reference equality and object equality. How are they called?
When a function takes in an object what is it really taking in?
void one(FillerObject o) {
o.addOne();
}
void one(int i) {
i++;
}
What do arrays of objects store? Do you understand the behavioral implications when compared to numbers? See the example below
class Basic() {
int i = 0;
void incrementI() {
i++;
}
}
Basic[] example = new Basic[4];
//At this point we have created an array of what? Have we created ANY person objects.
for(int i = 0; i < example.length; i++) {
example[i] = new Basic();
}
//How many objects are in example?
//what if i did:
Basic holder = new Basic();
for(int i = 0; i < example.length; i++) {
example[i] = holder;
}
Do you understand the idea of making shallow and deep copies of the array shown above? Quiz Prep: Design a class that uses both static methods and variables
- Here is a book class:
public class Book {
//make sure these are private on the exam
public String author;
public String title;
public int pageNumbers;
//constructor is obviously public
public Book(String setAuthor, String setTitle, int setPageNumbers) {
author = setAuthor;
title = setTitle;
pageNumbers = setPageNumbers;
}
}
Create a class that extends the book class in any way you see fit. Have a bookshelf that stores an array of Book objects. Downcast the book object to unlock the unique properties of the class you created.
public class Book {
//make sure these are private on the exam
public String author;
public String title;
public int pageNumbers;
//constructor is obviously public
public Book(String setAuthor, String setTitle, int setPageNumbers) {
author = setAuthor;
title = setTitle;
pageNumbers = setPageNumbers;
}
public static Book copy(Book o) {
Book toReturn = new Book(o.title, o.author, o.pageNumbers);
return toReturn;
}
}
public class Fiction extends Book {
public int fictionLevel;
Fiction(String setAuthor, String setTitle, int setPageNumbers, int setFictionLevel) {
super(setAuthor, setTitle, setPageNumbers);
fictionLevel = setFictionLevel;
}
}
public class Book {
//make sure these are private on the exam
public String author;
public String title;
public int pageNumbers;
//constructor is obviously public
public Book(String setAuthor, String setTitle, int setPageNumbers) {
author = setAuthor;
title = setTitle;
pageNumbers = setPageNumbers;
}
public static Book copy(Book o) {
Book toReturn = new Book(o.title, o.author, o.pageNumbers);
return toReturn;
}
}
public class Fiction extends Book {
public int fictionLevel;
Fiction(String setAuthor, String setTitle, int setPageNumbers, int setFictionLevel) {
super(setAuthor, setTitle, setPageNumbers);
fictionLevel = setFictionLevel;
}
}
Then try out:
Pet j = new Dog();
j.getType();
j.noise();
public class Pet {
public String noise() {
return null;
}
public String getType() {
return "Pet";
}
}
public class Dog extends Pet {
public String getType() {
return "Dog";
}
public String bark() {
return "Bark";
}
}