References and Interfaces
> Click or hit Control-Enter to run Example.main above
Quick Q5 Review
public class Laptop {
public static int count = 0;
private String model;
Laptop(String setModel) {
model = setModel;
count++;
}
public static void main(String[] unused) {
Laptop apple = new Laptop("Apple");
Laptop PC = new Laptop("Windows");
Laptop broken = new Laptop("Linux");
System.out.println(apple.count);
}
}
When the main
method defined above runs, what will be printed?
We Do A Bit of Java Internals
Either because it’s
-
important
-
interesting
-
fun
-
or some combination of the above.
References
Reference: a value that enables a program to indirectly access a particular datum, such as a variable’s value or a record, in the computer’s memory or in some other storage device. The reference is said to refer to the datum, and accessing the datum is called dereferencing the reference.
Review: Java Reference Variables
class Person { }
/*
* me is declared to hold a reference to an object of type Person,
* but currently refers to nothing.
*/
Person me;
/*
* Initializing an instance to null is another way of indicating
* that it currently refers to nothing.
*/
Person you = null;
me = new Person(); // Now me refers to a new Person object
you = me; // Now me and you refer to the same Person object
System.out.println(you == me);
you = new Person(); // Now you refers to a new Person object
System.out.println(you == me);
We can refer to a Java variable that refers to an object as a reference variable.
Review: References Are Not Objects
References are not the thing the refer to.
Some real-world references include:
-
A phone number: which refers to a phone
-
A street address: which refers to a physical location
-
A social security number: which refers to a person
Review: Copying References
Copying a reference does not copy the object it refers to.
-
Copying a phone number doesn’t copy the phone. Anyone with the number can call the same person.
-
Copying a street address doesn’t copy the location. Anyone with the address can navigate to the same spot.
-
Copying a social security number doesn’t copy the person 1. Anyone with social security number may be able to impersonate that person.
Copying References
class Person {
public int age;
}
Person me;
Copying References
class Person {
public int age;
}
Person me = new Person();
Copying References
class Person {
public int age;
}
Person me = new Person();
Person you = me;
Copying References
class Person {
public int age;
}
Person me = new Person();
Person you = me;
me.age = 10;
Copying References
class Person {
public int age;
}
Person me = new Person();
Person you = me;
me.age = 10;
System.out.println(you.age);
Pass By (Copied) Reference
class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(38);
System.out.println(birthday(me));
System.out.println(me.age);
In Java methods receive a copy of a reference to the passed object.
So they can modify the object the reference refers to.
Pass By (Copied) Reference
class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(38);
Pass By (Copied) Reference
class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(38);
System.out.println(birthday(me));
Pass By (Copied) Reference
class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(38);
System.out.println(birthday(me));
System.out.println(me.age);
> Click or hit Control-Enter to run Example.main above
How To Copy Objects
public class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
Person(Person other) {
this.age = other.age;
}
}
If we want to copy an object, we have a few options:
-
Object
provides aclone
method -
You can implement a copy constructor as shown above
Shallow v. Deep Copies
public class Person {
public Pet pet;
Person(Person other) {
this.pet = other.pet;
}
}
What is a potential problem with the copy constructor shown above?
-
It only copies the reference to the
Pet
object. So both the existing and the new object will share the samePet
object. -
This is called a shallow copy. A deep copy copies all of the objects so the old and new object share nothing.
Review: Reference v. Object Equality
public class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
boolean equals(Person other) {
return this.age == other.age;
}
}
Person me = new Person(38);
Person other = new Person(38);
System.out.println(me == other);
System.out.println(me.equals(other));
-
If two references are equal then they refer to the same object, and
.equals
is almost always true. -
If two references are not equal, the class may still define
.equals
to be true depending on the value of the instance variables.
References and Method Overriding
Note that Java uses the reference type, not the object type when matching method signatures.
> Click or hit Control-Enter to run Example.main above
Questions About Object References?
This concept is critical once we start talking about data structures and algorithms next week.
Interfaces
Interfaces are an incredibly important idea when building computer programs and systems.
What Is An Interface?
Interface: a shared boundary across which two or more separate components of a computer system exchange information.
-
Interfaces can be between two pieces of software, between software and hardware, between computers and their users, or between various permutation of these components.
-
Interfaces enable different parts of a system to interact in a structured way.
Examples of Computer Interfaces
-
Software-software: between the test cases that we write and the code that you complete for each MP or homework problem.
-
Software-hardware: between my laptop and the Chromecast that is displaying today’s lecture slides.
-
Computer-user: computer displays, keyboards, pointing devices, and other peripherals.
Software Interfaces
We’re going to focus on software interfaces, and specifically on interfaces in Java.
-
However, interfaces are not a Java- or language-specific idea.
-
Some languages—like Java, Go, and others—include a specific notion of interfaces as part of the language.
-
For other languages this is done by convention.
-
But all software development involves interfaces, regardless of what language you are using.
Note: Every Java Object Has An Interface
Even Java classes that don’t implements
a Java interface provide an interface.
The interface to a Java class
is the the set of methods that it
provides.
Interface Documentation
Interfaces are also a place where we need excellent documentation.
-
This facilitates communication between users of an interface and providers of an interface.
-
This is exactly what Javadoc is for.
Java Interfaces
public interface Add {
int add(int first, int second);
}
-
Java interfaces look like empty objects: just method signatures with no implementation.
-
Interfaces can declare both methods and variables.
-
However, interfaces variables are
public static final
by default, meaning that they are only useful for declaring constants.
Implementing Interfaces
public interface Add {
int add(int first, int second);
}
public class Adder implements Add {
public int add(int first, int second) {
return first + second;
}
}
-
Interfaces don’t do anything useful by themselves. Instead, they have to be implemented by specific classes.
-
To declare that a class implements an interface you use the
implements
keyword as shown above. -
To implement an interface you must implement all of the methods that it declares.
> Click or hit Control-Enter to run Example.main above
Interface Casting
public interface Add {
int add(int first, int second);
}
public class Adder implements Add {
public int add(int first, int second) {
return first + second;
}
public int multiply(int first, int second) {
return first * second;
}
}
Add add = new Adder();
System.out.println(add.add(10, 20));
// But this doesn't work because multiply is not part of the add interface
System.out.println(add.multiply(10, 20));
-
Similar to inheritance I can automatically cast an object reference to any interface that it implements.
-
However, if I do that I can no longer access methods that are not part of the interface.
> Click or hit Control-Enter to run Example.main above
Interfaces v. Inheritance
So far this seems very similar to inheritance and overloading.
-
The interface is like the parent class
-
implement
is likeextends
-
Providing your own implementation is like overriding a parent’s method
> Click or hit Control-Enter to run Example.main above
abstract
Methods
It’s actually even more similar than it seems.
Remember abstract
classes?
abstract
classes can also have abstract
methods:
public abstract class Add {
public abtract int add(int first, int second);
}
> Click or hit Control-Enter to run Example.main above
So Why Interfaces?
Added Flexibility
Sometimes we want to mix capabilities from different branches of the tree.
Multiple Inheritance
public interface Add {
int add(int first, int second);
}
public interface Subtract {
int subtract(int first, int second);
}
public class Mathy implements Add, Subtract {
public int add(int first, int second) {
return first + second;
}
public int subtract(int first, int second) {
return first - second;
}
}
Unlike inheritance, classes can implement multiple interfaces.
> Click or hit Control-Enter to run Example.main above
Interface as Contract
/**
* Compares this object with the specified object for order.
*
* Returns a negative integer, zero, or a positive integer as this object is
* less than, equal to, or greater than the specified object.
*/
public interface Comparable {
int compareTo(Object other);
}
Interfaces represent a contract between the interface provider and the interface user.
The interface represents all that the two components on either side need to agree on for things to work correctly.
Interface as Contract
public interface Comparable {
int compareTo(Object other);
}
By implementing
Comparable
you commit to being able to compare two instances of your class.
Using this ability I can implement code that:
-
sorts an array containing instances of your class
-
finds the maximum or minimum value of multiple instances of your class
-
arranges instances of your class into a binary tree 2
Interface as Abstraction Barrier
public interface Comparable {
int compareTo(Object other);
}
Good interfaces also represent a barrier between two unrelated parts of a computer program or system.
-
If I implement
Comparable
I don’t need to worry about how my implementation is used, but suddenly my class will have many new desirable features -
If I use
Comparable
I don’t need to worry about how the interface is implemented but I know that I can correctly compare two objects
> Click or hit Control-Enter to run Example.main above
Announcements
-
MP3 is due today! Office hours all day today. As a reminder, MP3 cannot be dropped.
-
MP4 will be out today and due in 2 weeks. It’s our first Android MP and we’ll do Android setup in lab this week.