SIS2024Y
Object-Oriented Programming
Lecture 1
Introduction to Object Orientation
1
Agenda
• Object-Orientation
• Object-Oriented Programming (OOP)
• Objects and Classes
• Object-Oriented Concepts
– Abstraction
– Encapsulation
– Hierarchy
• Getting started with Java
• My first java programme
2
Object-Orientation
• Object-Oriented is about trying to represent the ‘objects’
that we find in the real world in software.
• An approach for modeling problems using models
organized around real world concepts and entities.
• The fundamental construct is the Object, which combines
both data and behaviour in a single entity.
• Object-oriented means organizing software as a collection
of discrete objects that incorporate both data structure and
behaviour.
3
Object-Oriented Programming
• OOP is a method of implementation in which
programs are organized as cooperative collection
of objects, each of which represent an instance of
some class, and whose classes are all members of
a hierarchy of classes united via inheritance
relationships. [Booch]
• Example of OOP languages:
– C++, Objective-C, Smalltalk, Delphi, Java, C#, Perl,
Python, Ruby, PHP
4
What is an object?
• Objects are the elements through which we
perceive the world around us.
• Every thing around you can be termed as an
object
– Examples: desk, student, lecturer, dog, car
• Real-world objects share two characteristics:
They all have State and Behavior.
– Example: Dogs have state (name, color, breed, hungry)
and behavior (barking, fetching, wagging tail)
5
Object Example 1 - Car
• A Car can be considered as an object.
• It would have data (State):
– Current Speed
– Fuel Level
– Colour
• And methods (Behaviour):
– Accelerate, Decelerate, Stop
– Turn lights on
6
Object Example 2 – Bank Account
• A Bank Account can also be considered as an object.
• It would have data (State):
– Initial balance
– Closing balance
– Current balance
• And methods (Behaviour):
– Withdraw money
– Deposit money
– Transfer money
7
Activity 1
• List out the State and Behaviour of a Student
object?
8
What is a Class?
• The Object-Oriented approach invites us to understand the
world, the problems around us in terms of objects and
classes.
• A class is a blue print from which individual objects are
created.
9
Class Structure
• A class consists of 3 parts:
– Name (or identity): identifies the class
– Variables (or attribute, state, field): contains the static
attributes of the class
– Methods (or behaviors, function, operation): contains the
dynamic behaviors of the class
10
Classes and Abstraction
• Abstraction is the concept of exposing only
the required essential characteristics and
behavior with respect to a context.
• Booch defines an abstraction as a simplified
description or specification, of a system that
emphasizes some of the system details while
suppressing others.
– It denotes the essential characteristics of an object
that distinguish from all other kinds of objects from
the perspective of the viewer.
11
Encapsulation
(Information Hiding)
• Encapsulation separates the external aspects of an
object, which are accessible to other objects, from the
internal implementation details, which are hidden from
other objects.
• Objects communicate with each others using well-
defined interfaces (public methods).
• Objects are not allowed to know the implementation
details of others. The implementation details are hidden
or encapsulated within the class.
12
Abstraction v/s Encapsulation
http://stackoverflow.com/questions/16014290/simple-way-to-understand-encapsulation-and-abstraction
13
Hierarchy
• Abstraction is a useful concept that allows us to
understand complex problems by defining
specific views.
• Encapsulation helps us manage this complexity
by hiding the inside view of our abstractions.
• A set of abstractions often forms a Hierarchy and
by identifying these hierarchies in our design, we
greatly simplify our understanding of the
problem.
14
Hierarchy
• The two most basic forms of hierarchy are:
– Inheritance (is-a hierarchy)
• Single inheritance (Generalisation/Specialisation)
• Multiple inheritance
– Composition/Aggregation (has-a hierarchy)
• Whole - Part
• Containership
• Collection
• Group
15
Hierarchy Examples
Inheritance
http://faculty.ycp.edu/~dhovemey/f
all2005/cs102/lecture/9-8-2005.html
Composition / Aggregation
http://gateoverflow.in/54244/isro2014-11
16
Getting started with Java
• Java is an object-oriented programming language originally
developed by Sun Microsystems and released in 1995.
• In Java every instruction is written in a class. There are
different types of classes.
– Example super class, sub class, abstract classes, main class, etc...
–
• Java programs are platform independent which means they
can be run on any operating system with any type of
processor
• Every Java program needs one class to start execution. This
class always contains a main method which is the program
entry point.
17
Java Platforms
There are four platforms of the Java programming language:
1. Java Platform, Standard Edition (Java SE) – To develop and deploy Java
applications on desktops and servers, as well as embedded environments.
2. Java Platform, Enterprise Edition (Java EE) - is the enterprise standard to build
scalable business services that feed dynamic web and mobile applications.
3. Java Platform, Micro Edition (Java ME) - is a Java platform designed for
embedded systems (mobile devices are one kind of such systems). Target devices
range from industrial controls to mobile phones and set-top boxes. Java ME was
formerly known as Java 2 Platform, Micro Edition (J2ME)
4. JavaFX - is a set of graphics and media packages that enables developers to
design, create, test, debug, and deploy rich client applications that operate
consistently across diverse platforms.
18
Where is Java used?
• Java is one of the most important programming language in today’s
IT industries.
– JSP: Java is used to create web applications like PHP and ASP,
JSP(Java Server Pages) used with normal HTML tags, which helps
to create dynamic web pages.
– Applets: This is another type of Java program that used within a
web page to add many new features to a web browser.
– J2EE: The software Java 2 Enterprise Edition are used by various
companies to transfer data based on XML structured documents
between one another.
– JavaBeans: This is something like Visual Basic, a reusable
software component that can be easily assembled to create
some new and advanced application.
– Mobile: Besides the above technology, Java is also used in
mobile devices, many kind of games and services built-in Java.
Today, all leading mobile service provider like Nokia, Siemens,
Vodafone are using Java technology. 19
What we need to run Java?
• Java software development kit (SDK)
from http://java.sun.com/
• To write your java programs you will need a text editor.
There are even more sophisticated IDE available in the
market. Example:
– Notepad: On Windows machine you can use any simple text
editor like Notepad (Recommended for this tutorial), TextPad.
– Netbeans: is a Java IDE that is open source and free which can
be downloaded from http://www.netbeans.org/index.html
– Eclipse: is also a java IDE developed by the eclipse open source
community and can be downloaded
from http://www.eclipse.org
20
Java Program Structure
• Example:
21
Java Program Structure
22
Java Program Structure
• public class Hello
– This creates a class called Hello.
– All class names required to start with a capital letter.
– The word public means that it is accessible by any
other classes.
• {…}
– The 2 curly brackets are used to group all the
commands together so it is known that the commands
belong to that class.
23
Java Program Structure
• public static void main
– The word public means that it is accessible by any other classes.
– The word static means that it is unique.
– The word void means this main method has no return value.
– main is a method where the program starts.
• System.out.println();
– System is a utility class and provides functionalities like facilities
provided functionalities like standard input, standard output,
and error output streams; access to externally defined
properties and environment variables; loading files and libraries;
– println method prints text on the screen with newline.
24
My first Java program
• Write a simple program to display the following
string on console: “My first Java program!”
• Steps:
1. Create a new Java project, Ex1
2. Create a new package, LectureOne (if asked)
3. Create new class, FirstProgram
4. Write main method
25
FirstProgram.java
• Class FirstProgram
• Output on Console
26
References
• http://docs.oracle.com/javase/tutorial/java/ja
vaOO/accesscontrol.html
• http://www.w3schools.in/java-tutorial/intro/
27