Skip to the content.

Polymorphism

-

Liskov Substitution Principle

An instance of type T can be replaced by an instance of type S
if
S is a subtype of T.


public interface GasFueled{}
public interface Electric{}
public class Vehicle{}
  // subtype of Vehicle
public class FordTaurus extends Vehicle implements GasFueled{}
  // subtype of Vehicle
public class TeslaS extends Vehicle implements Electric{}

FordTaurus taurus = new FordTaurus();
Vehicle rental = taurus;
TeslaS snazzy = new TeslaS();
rental = snazzy;              // All of these are legal;

-

Polymorphic Program design

-

Things that don’t behave polymorphically

-

Things that don’t behave polymorphically

//: polymorphism/FieldAccess.java
// Direct field access is determined at compile time.
class Soup {
  public int field = 0;
  public int getField() { return field; }
}
class Stew extends Soup {
  public int field = 1;
  public int getField() { return field; }
  public int getSuperField() { return super.field; }
}

-

Field Access

public class FieldAccess {
  public static void main(String[] args) {
    Soup soup = new Stew(); // Upcast
    System.out.println("soup.field = " + soup.field +
      ", soup.getField() = " + soup.getField());
    Stew sub = new Stew();
    System.out.println("sub.field = " +
      sub.field + ", sub.getField() = " +
      sub.getField() +
      ", sub.getSuperField() = " +
      sub.getSuperField());
  }
} /* Output:
soup.field = 0, soup.getField() = 1
sub.field = 1, sub.getField() = 1, sub.getSuperField() = 0
*///:~ wha?

-

Generics

-

Generics

-

Generics - a FIFO Queue

-

Hmm.

-

Queue

this is an imaginary example…

// Pretend Queue has a add() method and a next() method.
Queue personQueue = new Queue<Person>(); // this queue is for persons

personQueue.add(joe); // joe starts standing in line
currentPerson = personQueue.next();  // "Next!" person being serviced

Queue carWashQueue = new Queue<Vehicle>(); // this queue tracks cars to be washed
carWashQueue.add(teslaS);

-

Terms

-

Examples of generic Types

-

Primitive types cannot be type parameters

Example:

// This will not compile!!!!

public class Main{
  public static void main(String[] args){
    ArrayList<int> primitiveArrayList = new ArrayList<>();
  }
}

-

The solution? Wrapper classes!


public class Main{
  public static void main(String[] args){
    ArrayList<Integer> pg = new ArrayList<>();
  }
}

-

Primitives and autoboxing

public static void main(String[] args){
  int x, y;
  x = 5;
  ArrayList<Integer> box = new ArrayList<>();
  box.add(x);
  y = box.get(0);
  System.out.println(y);

-

Enumeration Classes

public enum Size { SMALL, MEDIUM, LARGE, EXTRA_LARGE };

-

Enumeration Classes

public enum Size
{
	SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");
	private String abbreviation;
	private Size(String abbreviation) { this.abbreviation = abbreviation; }
	public String getAbbreviation() { return abbreviation; }
}

-

Enumeration Classes

`Size.SMALL.toString()` gives "S"

conversely

`Size s = Enum.valueOf(Size.class, "SMALL");`

`Size.MEDIUM.ordinal()` gives a 0-based index (1)... XL is (3)...

-

Reflection

-

Reflection basics

-

Reflection basics

-

The Class Class

-

A Class Object

-

Class Object Creation

-

Getting a Class object

-

Checking the class of an object

-

Example

String str = "Hello";

If used in a conditional:

( str instanceof String ) //true
( Object.class.isInstance(str) ) // true
( str.getClass() == String.class )// true
( str.getClass() == Object.class ) // false
( str.getClass().equals(Object.class) ) //false

-

Useful Class object methods

-

Resources

-