Think of climbing a ladder. The top of the ladder is the Object class.
Upcasting = going up the ladder
super easy to do and always possible, but you lose the methods and variables in the original class
Downcasting = going down the ladder
more complicated because you must check if instance of using instanceof
Upcasting and Downcasting Example
classMovie{
privateint generalRating;
publicintgetGeneralRating(){
return generalRating;
}
}
classRomanceextendsMovie{
privateint cheesyRating;
publicintgetcheesyRating(){
return cheesyRating;
}
}
Movie twilight = new Romance(); // upcasting example
twilight.getcheesyRating(); // will this run?
twilight.getGeneralRating(); // will this run?
Practice with Upcasting and Downcasting (5 minutes)
Use the Movie class from the previous slide.
(1) What's another class that could descend from Movie? What's a unique method it would have?
(2) Create an Movie object that is an instance of the child class you came up with in (1).
(3) Properly downcast the Movie object from (2) into an instance of your child class and have it call the unique function you came up with in (1).
Practice with Upcasting and Downcasting (10 minutes)
Create a method called vetVisit() that accepts a single Pet parameter and returns a String that is the type of visit. Depending on what kind of pet is passed in, the vet visit will change. You can assume that all Pets have a method called isRegular() which returns a boolean value indicating whether the pet is a regular. If the Pet is null or not one of the described ones, you should return null.
If the pet is a Dog and a regular, the visit will be a "Regular Grooming". If the pet is a Dog but not a regular, the visit will be a "Parasite Prevention Session".
If the pet is a Cat, the visit will be a "Cat Wellness Exam".
If the pet is a Hamster, the visit will be a "Dental Care Visit".
A new object is created with the new keyword. Variables store references to that object. References can point to objects or nothing at all (null).
String jet = new String("My Private Jet");
String plane = jet;
String anotherPlane = jet;
//How many objects do we have here?// How many references do we have here?
More on References in Java
Changes are visible to all references
publicclassStudent{
String name = "akhila";
}
Student one = new Student();
Student two = one; // what is two.name?
two.name = "jackie"; // what is one.name?
Pop Quiz: Does copying a reference copy the object?
Reference vs Instance Equality
publicclassStudent{
private String value;
publicStudent(String setValue){
value = setValue;
}
@Overridepublicbooleanequals(Object o){
if (!(o instanceof Student)) {
returnfalse;
}
Student other = (Student) o;
return value == other.value;
}
}
Student a = new Student("jackie");
Student b = new Student("jackie");
Student c = a;
System.out.println(a == b); //`==` tests reference equality.
System.out.println(a.equals(b)); //`.equals()` tests instance equality.
System.out.println(a == c);
System.out.println(a.equals(c));
Practice with References (5 minutes)
Create a method called compareMovie() that takes in two Movies. If the references to the two movie objects are the same, it should return a String "passedTest1". If the instances of the two objects match, return a String "passedTest2". Otherwise, return "failed".
Starter Code
publicclassMovie{
private String value;
publicMovie(String setValue){
value = setValue;
}
@Overridepublicbooleanequals(Object o){
if (!(o instanceof Movie)) {
returnfalse;
}
Movie other = (Movie) o;
return value == other.value;
}
publicstatic String compareMovie(Movie first, Movie second){
// write your code here
}
}
Java and Copying
Problem: Java has no built-in way to create a copy of a object (side note: clone() does exist, but it's not really recommended)
Solution: Use copy constructors!
Copy Constructors - conventions or a pattern adopted by Java programmers over time to copy objects
Shallow Copy - copy over references to the original object, but no new object is created...what does this mean???
Deep Copy - creates a whole new object that is a replica of the original
The Copy Constructor
publicclassMovie{
int rating;
publicMovie(int setRating){
rating = setRating;
}
// copy constructor publicMovie(Movie o){
rating = o.rating;
}
}
Movie one = new Movie(2);
Movie two = new Movie(one);
System.out.println(two.rating);
Shallow Copy
Deep Copy
Practice with Deep Copy (10 minutes)
Create a deep copy of the class below.
publicclassPlaylist{
private String[] playlist;
publicPlaylist(String[] songs){
playlist = songs;
}
//write your copy constructor here
}
String[] songs = {"Dear John", "22", "cardigan"};
Playlist one = new Playlist(songs);
Playlist second = new Playlist(one);
Yay! You made it to the end!
Recap:
Upcasting is easy. Downcasting is a little more complex.
References point to the object. Like your address points to your house.
With shallow copies, no new object is created. With deep copies, a new object is created.
Have a good week. See you on Friday. Peace
Solutions Section
Practice with Upcasting and Downcasting
classMovie{
privateint generalRating;
publicintgetGeneralRating(){
return generalRating;
}
}
// new child class classInternationalextendsMovie{
privateint language;
publicintgetLanguage(){
return language;
}
}
Movie parasite = new International();
if (parasite instanceof International) {
International parasiteMovie = (International) parasite;
parasiteMovie.getLanguage();
}
Practice with Upcasting and Downcasting
publicclassPet{
booleanisRegular(){
returntrue;
}
}
publicclassDogextendsPet{ }
publicclassCatextendsPet{ }
publicclassHamsterextendsPet{ }
public String vetVisit(Pet p){
if (p instanceof Dog) {
if (p.isRegular()) {
return"Regular Grooming";
} else {
return"Parasite Prevention Session";
}
}
if (p instanceof Cat) {
return"Cat Wellness Exam";
}
if (p instanceof Hamster) {
return"Dental Care Visit";
}
returnnull;
}
Practice with References
publicclassMovie{
private String value;
publicMovie(String setValue){
value = setValue;
}
@Overridepublicbooleanequals(Object o){
if (!(o instanceof Movie)) {
returnfalse;
}
Movie other = (Movie) o;
return value == other.value;
}
publicstatic String compareMovie(Movie first, Movie second){
if (first == second) { // reference equality checkreturn"passedTest1";
} elseif (first == null || second == null) {
return"failed";
} elseif (first.equals(second)) { // instance equality check return"passedTest2";
} else {
return"failed";
}
}
}
Movie a = new Movie("jackie");
Movie b = new Movie("jackie");
Movie c = a;
Movie d = null;
Movie e = new Movie("akhila");
System.out.println(Movie.compareMovie(a, b)); // returns passedTest2
System.out.println(Movie.compareMovie(a, c)); // returns passedTest1
System.out.println(Movie.compareMovie(d, c)); // returns failed
System.out.println(Movie.compareMovie(a, e)); // returns failed
Practice with Deep Copy
publicPlaylist(Playlist o){
playlist = new String[o.playlist.length];
for (int i = 0; i < o.playlist.length; i++) {
playlist[i] = o.playlist[i];
}
}