poly means many, morphs means forms
one interface with multiple implementations
Polymorphism in java:
We will also see polymorphism through generics in the near future
In java: Only static or final methods (and constructors) have static binding:
A sub-class is free to redefine (most) methods
@Override
Animal.java
public class Animal {
private String species;
private String genus;
private String family;
public Animal (String s, String g, String f) {
species = s;
genus = g;
family = f;
}
@Override
public String toString() {
String message;
message = "The " + species;
message += " of genus " + genus;
message += " and family " + family;
return message;
}
}Broadly Shadowing is when two things share a name.
super.<name> syntax for disambiguationMethods whose signatures differ are different methods. Using the same name for two methods whose signatures differ is known as overloading.
Cat.java
CreateAnimals.java
equalsCompare two objects
Let’s take a look at .equals(Object obj) from the Object class:
What would we need to make sure is the same for Animal?
We can also take a look at how String implements the equals method
Use the keyword instanceof to check if an object is an instance of a class.
Use (type) variableName to type cast.
Let’s say we want to make the following classes:
Note, these have a clear is-a relationship
What attributes and methods would we need?
Download tester files.
Overload: two Polygon constructors, one with array of side measures (double[]), another with no arguments
Override: toString() and equals(Object o)
Submit Polygon.java, Rectangle.java, Square.java and Triangle.java to gradescope
Polygon.java
public class Polygon {
protected String name = "Polygon";
protected int sides;
private double[] measures = new double[100];
public Polygon(double[] measures) {
sides = measures.length;
for (int i = 0; i < measures.length; i++) {
this.measures[i] = measures[i];
}
}
public Polygon() {}
protected void setMeasures(double[] measures) {
this.measures = measures;
}
public double[] getMeasures() {
return measures;
}
public int getSides() {
return sides;
}
public String getName() {
return name;
}
@Override
public String toString() {
String message = name + " has " + sides + " sides";
if (sides > 0) {
message += "\nMeasures are:";
}
for (int i = 0; i < sides; i++) {
message += " " + measures[i];
}
return message;
}
@Override
public boolean equals(Object o) {
// check if same object
if (this == o) {
return true;
}
// check if same type of object
if (!(o instanceof Polygon)) {
return false;
}
// check sides, must be same number of sides
if (((Polygon) o).getSides() != sides){
return false;
}
// check individual measures
for (int i = 0; i < sides; i++) {
if (((Polygon) o).getMeasures()[i] != measures[i]) {
return false;
}
}
// check name
if (!((Polygon) o).getName().equals(name)) {
return false;
}
return true;
}
}equals(Object o) solution @Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o instanceof Polygon aPolygon) {
if (aPolygon.getSides() == sides && aPolygon.getName().equals(name)) {
for (int i = 0; i < sides; i++) {
if (aPolygon.getMeasures()[i] != measures[i]) {
return false;
}
}
return true;
}
}
return false;
}Rectangle.java
Square.java
Triangle.java