|
Wed, 27 Jan
|
|
- Review:
Java design goals:
simple and familiar,
object oriented,
robust and secure,
architecture neutral and portable,
high-performance,
interpreted,
threaded,
dynamic
Portability:
bytecodes and primitive types well defined
Java environment:
.java (source code),
javac (compiler),
.class (bytecode),
java (virtual machine)
Hello World!
Classes and object
Objects: have state (variables) and behaviour (methods)
Class vs. instance fields:
- common to all objects of class, eg. Time.AM,
- one copy per object/instance, eg. t.sec
Class vs. instance methods:
- use only class data/methods, eg. Queue q=new Queue(); q.enqueue();,
- use instance data/methods too,
eg. double x=Math.sin(pi) [vs.
double x=(new Number(pi)).sin();]
- Public: exposed to all objects
Private: exposed only within same class
Protected: exposed to subclasses of this class
- Inheritance: class Manager extends Employee,
Dog/Cat extends Animal
allows specialization (code reuse)
and polymorphism (behaviour depends on type)
- Abstract vs. concrete: Animal
can only instantiate concrete classes,
must implement all abstract methods in subclasses
concrete method: die(), abstract method: talk()
ensures expressions like Animal.talk() are valid for all
object instances of subclasses
- Final:
classes: can't subclass (error to have abstract, final... Why?)
methods: can't override/specialize in polymorphic manner
fields: can't change initial value
- Interface: totally abstract, eg. Enumeration
just define the "interface" methods, no implementation
|
|
|