CIS 26
Object-Oriented Programming
Lecture 03 - The Java Language II
Statements
- Local variables can appear anywhere before their first use
- Statements MUST be capable of having a potential side-effect
Class Declarations
- Basic syntax
- Instance variables (state)
- Created on a per-object (instance) basis
- Can be initialized in place (unlike C++)
- Methods (behavior)
- Accessibility: public, private, protected, package (default)
- Constructors
- Provide initialization of the object
- Default constructor
Inheritance
A class can be declared to be an extension of another class
class MyApplet extends Applet {
...
}
- The new class inherits all the behavior (methods) and state (instance variables)
of the class it extends.
class Set {
...
boolean contains(Object obj) {...}
...
}
class SortedSet extends Set {
...
}
class MyApp {
...
SortedSet ss = ...;
if (ss.contains(...)) ...
}
- We speak of
- parent and child class
- base and derived class
- superclass and subclass
- The subclass can
- The subclass can also
- Appear in any context that the superclass can
class MyApp {
...
boolean contains
}
class Set {
...
}
- In Java, all classes form a tree hierarchy with the root being the class
Object
Composition as an Alternative to Inheritance
Consider Java's Stack class
Parameter Passing
- Always by-value (pointer analogy is sometimes useful here
class Test {
public static void main(String [] args) {
int [] arr = {1, 2, 3, 4, 5, 6};
f(arr);
System.err.println(arr[0]);
g(arr);
System.err.println(arr[0]);
}
static void f(int arr[]) {
arr[0] = 10;
}
static void g(int arr[]) {
arr = new int [] {100, 200, 300};
}
}
Class (static) fields and methods
- Shared by all instances (class-wide entities)
- Used for
- Class-related values
class Employee {
Employee(...) {
...
id = nextId++;
}
...
int id;
static int nextId = 0;
}
- methods dealing with the class as a whole
class Employee {
static int getNextId() {return nextId;}
...
static int nextId = 0;
}
- methods that have no logical receiver
class Math {
...
static int abs(int x);
}
class Employee {
Employee(String name, double salary) {...}
static Employee read(BufferedInputStream br) {
String line = br.readLine();
if (line == null) return null;
String name = getName(line);
double salary = getSalary(line);
return new Employee(name, salary);
}
}
constants (final
)
class Color {
Color(int r, int g, int b) {...}
..
static final Color RED = new Color(255, 0, 0);
}
this
- Keyword representing the receiver of a (non-static) method
- Used when object needs to refer to itself
class Employee {
Employee() {
...
System.err.println(this); // want to see the iniitalized object
}
}
class MyApplet extends Applet implements ActionListener {
public void init() {
...
Button ok = new Button("OK");
ok.addActionListener(this);
}
}
Interfaces
- Interface is a collection of method and constant declarations
- Method declaration in interfaces consists of header only-- no body
- Such a method is known as an abstract method
- Everything is
public
interface Iterator {
boolean hasNext();
Object next();
}
interface Compareable {
int compareTo(Object obj); // No body
}
- Class implementing the interface most provide definitions for those methods
class Vector implements Iterator {
...
boolean hasNext() {...}
Object next() {...}
...
}
class Employee implements Compareable {
...
int compareTo(Object obj) {...}
...
}