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

Object Oriented Programming - Java Intro

Introduction into OOP - Java

Uploaded by

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

Object Oriented Programming - Java Intro

Introduction into OOP - Java

Uploaded by

drfc14
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

MSc Software Development

Programming
OOP - Intro
Object-oriented programming
Programming paradigms are fundamental styles or approaches to building computer programs.
They provide a set of principles, methods, and concepts for designing and implementing software
systems. Different paradigms offer distinct ways to structure, organise, and execute code.
These include :

Imperative Programming
Procedural Programming
Object-Oriented Programming (OOP)
Functional Programming
Declarative Programming
Logic Programming
Event-Driven Programming
Aspect-Oriented Programming (AOP)

Java is an Object-Oriented Programming (OOP) based language: which structures programs around objects that encapsulate
data and behaviour.

Software Development
2
Object-oriented programming
Object-Oriented Programming (OOP) is a programming paradigm that organises software design
around the concept of "objects" that can contain data, in the form of fields (attributes or
properties), and code, in the form of procedures (methods or functions).
OOP closely models the way in which humans think about and deal with the real world. Software
systems are viewed as a community of objects that cooperate with each other by passing
messages
Why so popular ? The goal is to build software quickly, correctly and economically. Objects (or
more precisely) the classes the object come from are essentially reusable software components.
OOP leads itself to reusable and exchangeable components – similar to the concept of
exchangeable parts which helped spur the industrial revolution. Increased productivity. OO programs
are easier to understand, correct and modify.

The core concepts of OOP include:


Classes and Objects
Encapsulation
Inheritance
Polymorphism
Abstraction

Software Development
3
Classes and objects
Class: A blueprint that defines the properties and behaviours of objects. It encapsulates data (attributes) and
methods.
Object: An instance of a class, representing a real-world entity. Each object has its own state and behaviour.

Car class

A class is similar in concept to the


car blueprints.

In Java (and other OOP languages) a


program unit called a class hosts the
set of methods that perform the
class’s tasks (behaviour) and
attributes (state) that describe the
properties of the car.

State (Attributes): Toyota (make), Red (colour), 2022 (year), 60 mph (speed), 80% (fuel), etc.
Behaviour (Methods): Drive (move the car), Accelerate (increase speed), Refuel (add fuel), StartEngine etc.
Software Development
4
Classes and Objects
Object: An instance of a class, representing a real-world entity. Each object has its own state and behaviour.

Class (blueprint) Object – (a real car(s))

Instantiation (build) – make it real !

Software Development
5
Objects – instance variables and methods
One or more actual cars can be built from a blueprint. In much the same way, one or more objects can be instantiated from a
class. Each object is an individual instance of a class.

Each car object has its own attributes such as colour, current speed, mileage. These are considered at design time e.g.
speedometer, odometer etc. As the car is driven it carries along (maintains) and will maintain its own attributes but doesn’t
know the speed etc. of any other cars.
Each object similarly has its own attributes. Known as instance variables.
An object has its own instance methods too. Pressing the accelerator pedal in a car sends a message to the car to perform a
task e.g. go faster. That will only affect that car – not others created from the blueprint. To send messages to an object is via a
method call.

Bank Account – another OOP example


Class
Attributes: Account number, Sort Code, Customer name, Balance …
Behaviour: Deposit, Withdraw, View Balance …

Object(s) : Each customer has their own Bank Account. Each holds and maintains its own balance but
will not know the details of other bank account balances. Withdrawing cash from an account (Bank
Account object) affects the balance in that account only.

Software Development
6
OOP – 4 pillars

restricts direct access to Focuses on the essential Allows a class to inherit Allows objects of
some components of an properties of an object, properties and behaviour different classes to be
object. It protects the data hiding the irrelevant from another class treated as objects of a
from external interference details. It simplifies (superclass or parent class). common superclass. It
and misuse. Access to the complex systems by It promotes code reusability enables one interface to
data is usually achieved providing a clear and and establishes an "is-a" be used for a general
through methods (getters simple interface. relationship between class of actions.
and setters). classes.

Software Development
7
Let’s code (OOP)
You have previously created an object of an existing class Scanner, then used that object to read data from the keyboard.

Now, you’ll create a new (your own design) class, then use it to create an object of another class you have written.

Chatbot
app

User Story: As a user, I would like a Acceptance Criteria:


system that displays a distinct message •The system should recognise and output the appropriate message for each time of day.
on the screen based on the time of day, •It should display "Good morning" for the morning, "Afternoon !" for the afternoon, "Evening
time. Relax." during the evening, and "Don't let the bed bugs bite" for nighttime..
distinguishing between morning,
afternoon, evening, and nighttime.

Software Development
8
Let’s code (OOP)
High-level design : The system will have one class (MessageGenie) that will have several methods,
that when invoked will output an appropriate message to the screen. We’ll also use a Test class (to
informally test/simulate how the ChatBot will use the MessageGenie class).

Good morning
MessageGeni
ChatBot e

morning(),
Afternoon!
afternoon,
main() Calls appropriate evening(),
method nighttime() Evening time. Relax.
depending on
time of day
Goodnight. Bed bugs…

Software Development
9
Let’s code (OOP)
UML (Unified Modelling Language) class diagrams are
commonly used to design / describe classes.

The UML representation of a class is a rectangle The middle compartment is empty, because the MessageGenie class does not have
containing three compartments stacked vertically. any attributes.

The top compartment shows the class's name. The UML models operations by listing the operation name preceded by an access
The middle compartment lists the class's modifier (in this case +) and followed by a set of parentheses. Method
attributes (variables). The bottom compartment morningGreeting does not require additional information to perform its tasks, so the
lists the class's operations (methods). When parentheses following the method name in the class diagram are empty.
drawing a class element on a class diagram, you The plus sign (+) in front of the operation name indicates that methods are public
must use the top compartment, and the bottom operation in the UML (i.e. a public method in Java).
two compartments are optional.

Software Development
10
Let’s code (OOP)

Software Development
11
Let’s code (OOP)
In order to use the instance methods, variables etc. of the The phrase "instantiating
MessageGenie class you will need to create (instantiate) a class" means the same
an object of the type MessageGenie thing as "creating an
object." When you create
an object, you are
creating an "instance" of
a class, therefore
"instantiating" a class

The new operator instantiates a class by allocating memory for a new object and returning
a reference to that memory. The new operator also invokes the object constructor.

The new operator requires a single argument: a call to a constructor. The name of the
constructor provides the name of the class to instantiate.
The new operator returns a reference to the object it created. This reference is usually
assigned to a variable of the appropriate type, like:

Point originOne = new Point(23, 94);

This example creates a object of type Point and passes in two int arguments (23 and 94).
The object is then referred to as originOne (the object reference)

To invoke a method in another class you will need a knowledge of the method
signature. You must compile with the signature or a compile time error will occur.
Software Development
12
Let’s code (OOP)
Under the bonnet… follow the Thread

Program Start

ChatBot ChatBot app start


Good morning
main(){ Afternoon !
Output start message Evening time. Relax.
instantiate the MessageGenie Create object
Don't let the bed bugs bite
method calls to : morning()
ChatBot app end
afternoon(), MessageGenie
evening(),
nighttime() morning(),
output end message afternoon,
} // end main evening(),
nighttime()

Program finished Software Development


13
Calculator class
Create the following class to represent a simple calculator

SimpleCalc

+ add( num1 : int; num2 : int) : void


+ subtract ( num1 : int; num2 : int) : void
+ multiply ( num1 : int; num2 : int) : void
+ divide ( num1 : int; num2 : int) :void

Create another class (Driver.java) with a main method. Within this method instantiate the SimpleCalc and exercise/ test all four of its methods. Run the Driver.java
class.

Software Development
14
Could you use

Convertor class cm to inches : cm * 0.394


constants here ?

Inch to Cm : inch * 2.54


Create the following class to represent a simple calculator Miles To Km : mile * 1.609
Km to miles : km * 0.621

UnitConvertor

+ cmToInches( cms : double) : double


+ inchesToCms( inches : double) : double
+ kmToMiles( kms : double) : double
+ milesToKms ( miles : double) : double

Create another class (BuilderHelperApp.java) with a main method. Within this method instantiate the UnitConvertor and
exercise/ test all its methods. Run the BuilderHelperApp.java class.

Software Development
15

You might also like