0% found this document useful (0 votes)
418 views

Java Revision Notes

This document outlines the topics to be covered in a lecture on Java programming and object orientation. The lecture will cover Java basics like classes, objects, methods, expressions, control flow and arrays. It will then discuss object oriented concepts such as inheritance, polymorphism, exceptions, interfaces and design patterns. Code examples and exercises will be provided to reinforce key concepts.

Uploaded by

thapelo mokolo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
418 views

Java Revision Notes

This document outlines the topics to be covered in a lecture on Java programming and object orientation. The lecture will cover Java basics like classes, objects, methods, expressions, control flow and arrays. It will then discuss object oriented concepts such as inheritance, polymorphism, exceptions, interfaces and design patterns. Code examples and exercises will be provided to reinforce key concepts.

Uploaded by

thapelo mokolo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Outline Java Programming Object Orientation

Computer Science 3A - CSC3A10/CSC03A3


Lecture 1: Java Basics and OO

Academy of Computer Science and Software Engineering


University of Johannesburg

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

Classes, Types and Objects

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

General Java programs

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

General Java programs II

Developing a Java program:


Design
Psuedo-code
Coding
Documentation
Readability and Style
Testing
Debugging

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

Template for a software solution


Consists of name and context (describes scenario for usage)
Algorithm patterns
Software engineering patterns

Computer Science 3A - CSC3A10/CSC03A3 17/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation

Inheritance

Modular and hierarchical


organization structure
Base class or super class
Subclass inherits
(extends) the base class
Dynamic dispatch/binding

Figure: Inheritance example,


where S is the parent of T

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

Throw clause specifies throw exceptions at declaration


1 p u b l i c v o i d w a t c h i n g R u g b y ( ) throws n o B e e r s E x c e p t i o n , n o B i l t o n g E x c e p t i o n
2
3 p u b l i c v o i d P l a n n i n g S a t u r d a y ( ) throws n o B e e r s E x c e p t i o n ,
noBiltongException
4 {
5 w a t c h i n g R u g b y ( ) ; // do n o t h a v e t o h a v e t r y c a t c h b l o c k
6 }

Throws example

Computer Science 3A - CSC3A10/CSC03A3 21/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation

Exceptions III

Catching exceptions require a try-catch block


1 try
2 {
3 PlanningSaturday () ;
4 }
5 catch ( E x c e p t i o n e ) {
6 i f ( e instanceof noBeersException )
7 sendFriend () ;
8 }
9 finally startBraai ;

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

Method declarations with no body and no data


Methods are always empty
May not be instantiated
Class implementing interface must implement all interface’s methods

Computer Science 3A - CSC3A10/CSC03A3 24/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation

Interfaces II

1 public interface Sellable


2 {
3 public string description () ;
4 public int l i s t P r i c e () ;
5 public int lowestPtice () ;
6 }
7
8 p u b l i c c l a s s P r o d u c t implements S e l l a b l e
9 // . . .
10 public s t r i n g d e s c r i p t i o n () { return d e s c r i p t i o n ;}
11 public int l i s t P r i c e () { return p r i c e ;}
12 public int lowestPtice () { return p r i c e ∗0.5;}
13 }

Interface example

Computer Science 3A - CSC3A10/CSC03A3 25/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation

Multiple Inheritance

No multiple inheritance for classes allowed


Multiple inheritance on interfaces is allowed
1 p u b l i c c l a s s MotorCar e x t e n d s L a n d V e h i c l e implements I S e l l a b l e ,
IPurchasable

Multiple Inheritance alternative

Computer Science 3A - CSC3A10/CSC03A3 26/35 Academy of Computer Science and Software Engineering
Outline Java Programming Object Orientation

Abstract class

Empty method declarations


Concrete declarations of methods and variables
May not be instantiated
Can extend other abstract classes
1 p u b l i c a b s t r a c t c l a s s Number { . . . }

Abstract class example

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;

Multiple Inheritance alternative

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 }

Casting with interfaces example

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> { . . . }

Single-letter uppercase names

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

Generics can also be restricted


1 p u b l i c c l a s s P a i r <K, V e x t e n d s A r r a y L i s t > { . . . }

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

You might also like