class Class0 { m() { . . // lots of code --- |if|s and |for|s ... . } } class Class2 { m() { . . // lots of code --- |if|s and |for|s ... . } } class Class3 { m() { . . // lots of code --- |if|s and |for|s ... . } } ... main() { ... obj2.m(); obj3.m(); ... }
sqrt()
Math
drawRect()
Graphics
m(big_array, long_String, several_ints, ...);
class Class0 { m() { . . // lots of code --- |if|s and |for|s ... . } } class Class2 { m() { Class0.m(); } class Class3 { m() { Class0.m(); } ... main() { obj2.m(); obj3.m(); ... }
class Monster
chase()
class Leprechaun
class Hobgoblin
class Monster { chase() { . . // here fully coded . // using |if|s and |for|s etc. . } } class Hobgoblin ... { attack() { ... } // damages player no definition of chase() } class Leprechaun ... { attack() { ... } // steals player's gold no definition of chase() } class Suite ... { ... Hobgoblin h; Leprechaun l; h.attack(); // l.attack(); // h.chase(); // l.chase(); // ... }
Monster[] monsters = new Monster[...]; int num_monsters; monsters[0] = new Leprechaun(...); monsters[1] = new Hobgoblin(...); ... for( int i = 0; i < num_monsters; i++ ) { monsters[i].chase(); // monsters[i].attack(); // // // }
class B { ...
class D B { ...
class Monster { field_variables methods such as chase() } class Hobgoblin Monster { // don't define chase() -- inheriting that from |Monster| attack() { ... } } class Leprechaun Monster { similarly
B
D
class B { int z, y = 8; public int f() { return 3 * y; } public void set_z(int z_new) { z = z_new; } } class D extends B { private int x; public int g() { return 4 + x; } public void h() { set_z(5); // y = 6; // } } // code outside those classes: B b1 = new B(); b1: +---------+ | int z | | int y | | f() | | set_z() | +---------+ int w; w = b1.f(); B b2 = new B(); b2: +---------+ | int z | | int y | | f() | | set_z() | +---------+ D d1 = new D(); d1: +-------------+ | int x | | g() | | h() | | + - - - - + | | . int y . | | . int z . | | . f() . | | . set_z() . | | + - - - - + | +-------------+ w = d1.g(); w = d1.f(); // d1.h();
x
y
z
private
d1.x = 8; // b1.y = 7; //
d1
public
f()
set_z()
protected
attack()
implements ActionListener, MouseListener
clear()
super(parameters);
class B { private int y, z; public B(int y0, int z0) { y = y0; z = z0; } } class D extends B { private int x; public D(int x0, int y0, int z0) { x = x0; } }
new D()
new B()
Monster
class Monster { attack() { ... } // basic hurting player chase() { ... } }
Leprechaun
class Leprechaun extends Monster { // override: attack() { ... } // just steal gold, NOT hurting player // get |chase()| inherited from |Monster| }
super.method(args)
class Ant extends Monster { // override to sting: attack() { ... // weaken player by stinging // but also do basic hurting } // get |chase()| inherited from |Monster| }
final
A
extends
D2
. . . / A / \ B . / \ . D D2 .
Object
extends Object
d1.f()
String s = "hello"; int i = 4; i = s; // s = i; //
b1 = d1; //
Object o;
o = monsters[3]; //
o = "hello"; //
b1.f()
b1.set_z(...)
object_variable . method(arguments)
method(arguments)
instanceof
+
<=
.
object instanceof class_name
if( monsters[i] instanceof Leprechaun ) {
...
w = b1.f(); OK: the type of |b1| is |class B|, which has a method |f()| w = b1.g();
b1
g()
d1 = b1; //
w = d1.g();
String s = o; //
String
o
14.0
int
int i = Math.sqrt(196);
String s = o;
c_tchrs_problem.html
Instructor
AsstProf
Professor
c_tchrs/c_tchrs_dup.java
Instructor AsstProf Professor +----------------+ +----------------+ +----------------+ |age | |age | |age | |get_age() | |get_age() | |get_age() | |stress() | |stress() | |stress() | |receive_email() | |receive_email() | |receive_email() | |etc. | |etc. | |etc. | +----------------+ +----------------+ +----------------+
class String
class Rectangle
CollegeTeacher
c_tchrs/c_tchrs_compos.java
Instructor AsstProf Professor +----------------+ +----------------+ +----------------+ |CollegeTeacher | |CollegeTeacher | |CollegeTeacher | |+-------------+ | |+-------------+ | |+-------------+ | || | | || | | || | | || | | || | | || | | |+-------------+ | |+-------------+ | |+-------------+ | | | | | | | +----------------+ +----------------+ +----------------+
c_tchrs/c_tchrs_inherit.java
increment_ecc()
class Professor
class CollegeTeacher
cope()
respect()
receive_email()
CollegeTeacher c_tchr = p_a;
ia[0]
c_tchr
c_tchr = ap_b;
ia[i].receive_email()
CollegeTeacher.receive_email()
ia[i]