Lecture 2 - Review of OOP
Lecture 2 - Review of OOP
In this lecture:
COS3711 Introduction
Lecture 2
Java Applications
Based on chapter 1 Java Data Types
of our recommended
book Creating Classes
Using Objects
Constructors
Overview of OOP
Concept Definition Our interest
Objects a software bundle of related state and how state and behavior are represented within an object
behavior used to model the real-world objects the concept of data encapsulation
the benefits of designing your software in this manner
Classes a blueprint or prototype from which objects modeling the state and behavior of a real-world object
are created
Inheritance a mechanism for organizing and structuring inheriting state and behavior from superclasses
software deriving a class from another
Interface a contract between a class and the outside creating interfaces
world implementing interfaces
Packages a namespace for organizing classes and placing your code into packages
interfaces in a logical manner. Creating an Application Programming Interface (API)
Generics enable types to be parameters when defining re-using the same code with different inputs
classes, interfaces and methods. Eliminate casts
implementing generic algorithms
Exceptions Event which disrupts the normal flow of a handle exceptional events
program's execution
Testing and Compare programs behavior to expected Improve the confidence in the correctness of the program
Debugging behaviour
1
3/1/2022
Reserved Words
2
3/1/2022
byte 8 -128..127 0
short 16 -32,768..32,767 0
int 32 -2,147,483,648.. 2,147,483,647 0
-9,223,372,036,854,775,808..
long 64 0L
9,223,372,036,854,775,807
float 32 3.4e-0.38.. 3.4e+0.38 0.0f
double 64 1.7e-308.. 1.7e+308 0.0d
char 16 Complete Unicode Character Set '\u0000'
boolean 1 true, false false
Creating Classes
all code in a Java class name same
program belongs as filename.java
in a class
curly brace
anyone can indicates beginning
use this class of class body
any instance
can access this
class method, no method returns
attribute method name
need for instance nothing
curly brace
indicates beginning
of method body
any instance
can access this end of statement
method
curly brace indicates
end of method body parameter to
method main
curly brace indicates argument to
end of class body method println
located in class method println
System
3
3/1/2022
Using Constructors
A constructor is a method that always shares the same name as its class.
The new operator returns a reference to the newly created instance
the returned reference is typically assigned to a variable for further use.
4
3/1/2022
Lecture Summary
Java programs are created using classes
A class consists of:
- Variables
- Methods
A Java application must contain a main method
Use constructors to specify how objects will be created