System.out.println(3); System.out.println("Hello"); System.out.println(employee); // instance of class Employee
String initialValue = ...; TextField t1 = new TextField(initialValue); TextField t2 = new TextField(4);
... int f() {...} double f() {...} ... System.out.println(f()); ...
Resolution
occurs at compile time
double d1, d2; int i1, i2; ... d1 = d1 + d2; i1 = i1 + i2; d1 = d1 + i1;
class Complex { ... operator +(Complex c2); ... } ... Complex c1, c2; ... c1 = c1 + c2;
-
, ... <
, ...
+
and +=
for String
class Counter { Counter(int val, int min, int max, int increment) { this.val = val; this.min = min; this.max = max; this.increment = increment); } ... }want to add default constructor that
Counter() {this(0, 0, 10, 1);}
class Window { Window(int width, int height) {...} // No default constructor ... }and now we extend
Window
class TitledWindow extends Window { TitledWindow(String title, int width, int height) {???} ... private String title; }
TitledWindow
object
TitledWindow tw = new TitledWindow("My First Window", 20, 40);involves a
Window
object creation
Window
object is PART of the TitledWindow
object-- that's the whole idea of inheritance!)
Window
object's state needs to be properly initialized
Window
constructor
TitledWindow
object
Window
class is properly initialized
(i.e., the constructor is invoked).
class TitledWindow extends Window { TitledWindow(String title, int width, int height) { super(width, height); this.title = title } ... private String title; }
super
constructor inviocation MUST be the frst executable statement of the
constructor
class TitledWindow extends Window { TitledWindow(String title) { super(WIDTH, HEIGHT); this.title = title } ... private String title; static final int WIDTH = 100, HEIGHT = 50; }
Types, References and Objects in Java
class Super { ... } class Sub extends Super { ... }
Super sup; // Type of reference variable is Super Sub sub; // Type of reference variable is Sub
... = new Super(); // Type of object is Super ... = new Sub(); // Type of object is Sub
sup = new Sub(); // legal-- a Sub is-a Super
sup = sub; // legal-- a Sub is-a Super
sub = sup; // legal?
sub = new Super(); // illegal-- a Super is not necessarily a Sub
sub = sup; // legal?
sub = new Sub(); sup = sub; // sup referencing a Sub object ... sub = sup;
class Sub2 extends Super { ... } sub2 = new Sub2(); // sup referencing a Sub2 object sup = sub2; ... sub = sup;
sub = sup;