Java Revision Notes
Java Revision Notes
Computer Science 3A - CSC3A10/CSC03A3 1/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
1 Java Programming
Classes, Types and Objects
Methods
Expressions
Control Flow
Arrays
General Java programs
Exercises
2 Object Orientation
Goals, Principles and Patterns
Inheritance
Polymorphism
Exceptions
Interfaces
Multiple Inheritance
Abstract class
Computer Science 3A - CSC3A10/CSC03A3 2/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Casting
Generics
Exercises
Computer Science 3A - CSC3A10/CSC03A3 3/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Java Programming
Computer Science 3A - CSC3A10/CSC03A3 4/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Declare a class
(Properties and Methods)
Class modifiers (abstract, 1 enum Day{
final, public) 2 Monday ,
3 Tuesday ,
Objects (new, Number, 4 ... ,
String and Dot operator) 5 Sunday
6 };
Variable modifiers 7 Day x = Day . Monday ;
(private, public,
protected, static, final)
Enums
Computer Science 3A - CSC3A10/CSC03A3 5/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Methods
Method modifiers:
private
public
protected
static
final
abstract
Computer Science 3A - CSC3A10/CSC03A3 6/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Method Types
Method types:
Procedure
Function
Constructor
Main
1 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
2 // Main c o d e h e r e
3 }
Main method
Computer Science 3A - CSC3A10/CSC03A3 7/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Expressions
Literals
Operators (i--, i++ and ++i)
Logical operators
Bitwise operators
Operator precedence
Casting (explicit cast and implicit cast)
Computer Science 3A - CSC3A10/CSC03A3 8/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Control Flow
If statement
elseif
switch
loops (while, for and do-while)
Explicit control-flow statements (return, break and continue)
Computer Science 3A - CSC3A10/CSC03A3 9/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Arrays
Capacity
Out of bounds errors
Declaring arrays
Arrays are objects
Cloning an array
Computer Science 3A - CSC3A10/CSC03A3 10/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Packages
Nested classes
Anonymous classes
etc.
Computer Science 3A - CSC3A10/CSC03A3 11/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Computer Science 3A - CSC3A10/CSC03A3 12/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Exercises
Reinforcement exercises:
R1.2
R1.3
R1.4
R1.8
Creativity exercises:
C1.10
C1.12
C1.13
C1.17
Computer Science 3A - CSC3A10/CSC03A3 13/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Object Orientation
Computer Science 3A - CSC3A10/CSC03A3 14/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
OO Goals
Robustness
Adaptability
Reusability
Computer Science 3A - CSC3A10/CSC03A3 15/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
OO Design principles
Abstraction
Distill complex system into its most fundamental parts
Applying abstraction paradigm - Abstract data types (ADT)
Encapsulation
Modularity
Hierarchical organization
Computer Science 3A - CSC3A10/CSC03A3 16/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Design Patterns
Computer Science 3A - CSC3A10/CSC03A3 17/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Inheritance
Computer Science 3A - CSC3A10/CSC03A3 18/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Polymorphism
“Many forms”
In OO design, objects
take different form
Override
Overloading (with a
different signature) or
name, type and argument
combination)
Self study - Using
inheritance in Java and Figure: Polymorphism example,
numeric progression where the c method has multiple
example definitions
Computer Science 3A - CSC3A10/CSC03A3 19/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Exceptions
Throwing exceptions:
Objects that are thrown when unexpected condition experienced
Thrown exceptions are caught
1 i f ( some c o n d i t i o n )
2 throw new M y E x c e p t i o n ( ‘ ‘We h a v e a p r o b l e m ! ’ ’ ) ;
Computer Science 3A - CSC3A10/CSC03A3 20/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Exceptions II
Throws example
Computer Science 3A - CSC3A10/CSC03A3 21/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Exceptions III
Throws example
Computer Science 3A - CSC3A10/CSC03A3 22/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Exceptions IV
finally
optional
Executed regardless of exceptions being thrown or caught
Computer Science 3A - CSC3A10/CSC03A3 23/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Interfaces
Computer Science 3A - CSC3A10/CSC03A3 24/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Interfaces II
Interface example
Computer Science 3A - CSC3A10/CSC03A3 25/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Multiple Inheritance
Computer Science 3A - CSC3A10/CSC03A3 26/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Abstract class
Computer Science 3A - CSC3A10/CSC03A3 27/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Casting
Casting up
java.lang.Object ⇒ java.lang.Number ⇒ java.lang.Integer
1 // I n t e g e r i= new I n t e g e r ( 3 ) ; D e p r e c a t e d w i t h J a v a 9
2 I n te g er i = I nt e ge r . valueOf (3) ;
3 Number n = i ;
Casting up
Computer Science 3A - CSC3A10/CSC03A3 28/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Casting II
Casting down
1 Number n = I n t e g e r . v a l u e O f ( 2 ) ;
2 Integer i = ( Integer ) n;
Casting down
Computer Science 3A - CSC3A10/CSC03A3 29/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Casting III
Casting exceptions
instanceof
1 i f (n instanceof Integer )
2 Integer i = ( Integer ) n;
Computer Science 3A - CSC3A10/CSC03A3 30/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Casting IV
Casting with interfaces
1 public i n t e r f a c e Person {
2 p u b l i c boolean equalTo ( Person o t h e r ) ;
3 }
4
5 p u b l i c c l a s s S t u d e n t implements P e r s o n {
6 // . . .
7 p u b l i c boolean equalTo ( Person o t h e r ) {
8 Student otherStudent = ( Student ) other ;
9 // . . .
10 }
11 // . . .
12 }
Computer Science 3A - CSC3A10/CSC03A3 31/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Generics
Generic framework
Abstract types that avoid many explicit casts
Define a class in terms of formal type parameters
1 p u b l i c c l a s s P a i r <K, V> { . . . }
Computer Science 3A - CSC3A10/CSC03A3 32/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Generics II
Generic type
not defined at compile time
specified at run time
Instantiate an object with actual type parameters
1 P a i r <S t r i n g , A r r a y L i s t > = new P a i r <S t r i n g , A r r a y L i s t >() ;
Generic instantiation
Computer Science 3A - CSC3A10/CSC03A3 33/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Generics III
Generic instantiation
Computer Science 3A - CSC3A10/CSC03A3 34/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation
Exercises
Reinforcement exercises:
R2.3
R2.4
R2.5
R2.6
R2.9
R2.10
Creativity exercises:
C2.12
C2.14
C2.18
Computer Science 3A - CSC3A10/CSC03A3 35/35 Academy of Computer Science and Software Engineering