Open All | Close All
Abstract Classes - An abstract class is a class that models a type that can't be instantiated.
abstract class Horse {
String name;
Horse(String name) {
this.name = name;
}
void talk() {
System.out.println("Howdy, I'm " + name);
}
}
class Unicorn extends Horse {
Unicorn(String name) {
super(name);
}
void talk() {
super.talk();
System.out.println("I also speak a bit of French.");
}
}
class Nineteen {
public static void main(String[] args) {
Horse a = new Unicorn("Oliver"); // totally fine
a.talk();
Horse b = new Horse("Stanley"); // does not work
}
} We'll have many more examples in the notes below and tomorrow.
- Better example
Interfaces - An interface is an element of pure design.
interface Horse {
void talk();
}
class Unicorn implements Horse {
String name;
Unicorn(String name) {
this.name = name; // there can't be constructor chaining in this case
}
public void talk() {
System.out.println("Bonjour, je suis " + name);
}
}
class Eighteen {
public static void main(String[] args) {
Horse a = new Unicorn("Francois");
a.talk(); // an interface is even closer to a remote controller metaphor
}
}
- Better example.
Examples
- Extending a
JFrame - Identify inheritance, polymorphism, dynamic method lookup in the code below:
import javax.swing.*;
import java.awt.*;
class One extends JFrame {
One() {
this.setSize(300, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
JFrame a = new JFrame();
a.setSize(300, 500);
a.setVisible(true);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
One b = new One();
}
public void paint(Graphics g) {
super.paint(g);
System.out.println("Ouch!");
}
}
- The
Comparable interface - Generic, customizable interface shown below in an example:
class Student implements Comparable<Student> {
String name;
int age;
public String toString() {
return this.name + "(" + this.age + ")";
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
public int compareTo(Student other) {
if (this.age < other.age) return -1;
else if (this.age > other.age) return 1;
else return this.name.compareTo(other.name);
}
public static void main(String[] args) {
Student a, b, c;
a = new Student("Lauren", 12);
b = new Student("Larry", 12);
c = new Student("Landon", 13);
System.out.println( a + " ahead of " + b + (a.compareTo(b) > 0 ? " no" : ( a.compareTo(b) < 0 ? " yes" : " maybe")));
System.out.println( a + " ahead of " + c + (a.compareTo(c) > 0 ? " no" : ( a.compareTo(c) < 0 ? " yes" : " maybe")));
System.out.println( c + " ahead of " + b + (c.compareTo(b) > 0 ? " no" : ( c.compareTo(b) < 0 ? " yes" : " maybe")));
}
}
- Sorting arrays and
ArrayList s of objects.- Two ways of storing and sorting objects in Java.
import java.util.*;
class Example {
public static void main(String[] args) {
Student[] a = new Student[5];
a[0] = new Student("Lauren", 12);
a[1] = new Student("Laura", 12);
a[2] = new Student("Larry", 12);
a[3] = new Student("Leslie", 11);
a[4] = new Student("Lance", 13);
System.out.println(Arrays.toString(a));
Arrays.sort(a);
System.out.println(Arrays.toString(a));
ArrayList b = new ArrayList();
b.add(new Student("Arthur", 86));
b.add(new Student("Aaron", 86));
b.add(new Student("Betty", 8));
b.add(new Student("Samuel", 12));
b.add(new Student("Samantha", 12));
b.add(new Student("Sam", 12));
System.out.println(b);
Collections.sort(b);
System.out.println(b);
}
}
In-class Notes
Updated by © Adrian German for C212/A592 -- 6W2 Summer 2017
|