[go: up one dir, main page]

0% found this document useful (0 votes)
12 views5 pages

Lecture 2 - Review of OOP

The document provides an overview of object-oriented programming concepts like classes, objects, inheritance, interfaces and packages in Java. It also describes Java data types, creating and using classes and objects, and constructors.

Uploaded by

Ezekiel Ignatius
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Lecture 2 - Review of OOP

The document provides an overview of object-oriented programming concepts like classes, objects, inheritance, interfaces and packages in Java. It also describes Java data types, creating and using classes and objects, and constructors.

Uploaded by

Ezekiel Ignatius
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

3/1/2022

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

Structure of a Java Program


• Every Java program is a class
• Every class stored in a source file with name same as class name and .java
suffix e.g. Person.java
• Java classes are compiled using the command:
javac ProgramName.java
• Compiling produces byte-code files stored in a file named with a .class
suffix (e.g., Person.class)

Class name: FirstClass


File name: FirstClass.java
After compiling: FirstClass.class

1
3/1/2022

Structure of a Java Application


Every Java application is also a class
A Java application contains a method called main
Java classes can be instantiated and used in Java Applications
Java programs are executed through the Java virtual machine (JVM)
Java applications may be executed or run by typing the command:
java ProgramName

Structure of a Java Program


Every class contains:
• data (also known as attributes or fields or instance variables)
• stores the state of the object
• methods (also known as members)
Syntax for data declaration:
access-modifier type identifier
e.g. public int age;
default access modifier is public
type can be any primitive type or reference type
identifier can be any valid combination of characters (use camelCase)

Reserved Words

2
3/1/2022

Java Data Types


• Java offers two main data types:
primitive (built-in) data types and reference types (user-defined)
• Each type is a set of values together with operations on those values
Primitive Data Types Size Range Default values

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

Classes and Objects


• A class serves as a blueprint for object types
• it defines the data which the object stores, and
• the methods for accessing and modifying that data
• can be called to perform actions
• can accept parameters as arguments
• their behavior may depend on the object upon which they are invoked
and the values of any parameters that are passed
• Methods that return information without changing instance variables
are called accessor methods (getters)
• Methods that change one or more instance variables when called are
called mutator methods (setters)

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

Another Example of a Class

• This class includes:


• one instance variable name,
• one constructor to set the default value of the instance variable to “Unknown”
• one constructor to initialize the instance variable to the specified name
• one accessor method to get the name of the person
• one mutator method to update the name of the person
• overriden implementation of toString method to return the String representation of a person

Creating and Using Objects


• Classes are known as reference types in Java
• A variable of a class type is known as a reference variable.
• A reference variable stores the location (i.e., memory address) of an object
This allows a reference to an existing instance to be reassigned or for one instance to have several references
The default value for a reference variable is a special value, null, that represents the lack of an object.
• To use a class, you much create an object
• Every object is an instance of a class
• In Java, a new object is created by using the new operator followed by a call to a
constructor for the desired class.

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

You might also like