BBIT 212/ CISY 111
Object Oriented Programming (OOP)
OOP Concepts and getting started
with Java
08.02.2011
• Today
2
• Recap
– Course objective: introduce Object Oriented Programming (using Java) and prepare us for advanced programming courses
– Your programming ideas :: will be reviewed after we have a start on java
– OOP paradigm (and in context)
• Programming Paradigm
– programming “technique” (?) – way of thinking about programming – view of a program
• Programming Language
– consists of words, symbols, and rules for writing a program
4
• Imperative Programming
– program as a collection of statements and procedures affecting data (variables)
• Object-Oriented Programming
– program as a collection of classes for interacting objects
• Functional Programming
– program as a collection of (math) functions
What is Object Oriented
Programming?
• About:
– objects and assigning responsibilities
– Objects communicate to other objects by sending messages
– Messages are received by the methods of an object
• What are objects?
– an object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain.
– An "object" is anything to which a concept applies
• Objects
– An object is like a black box. The internal details are hidden.
– Tangible Things as a car, printer, ...
– Roles as employee, boss, ...
– Incidents as flight, overflow, ...
– Interactions as contract, sale, ...
6
• Why do we care about objects?
– Modularity - large software projects can be split up in smaller pieces.
– Reuseability - Programs can be assembled from pre-written software components.
– Extensibility - New software components can be written or developed from existing ones.
Example
• The Person class
public class Person {
//Data
String dateOfBirth; // e.g. "21/03/1985" String name = "Salesio";
double identification;
//Methods
String getPersonName(){ return name; }
void setDateOfBirth(String newDateOfBirth){ dateOfBirth = newDateOfBirth; }
//The main method
public static void main (String [] args){
System.out.println("A Person Says: 'Hello World!!'"); }
}
public class Person { //Data
String dateOfBirth; // e.g. "21/03/1985" String name = "Salesio";
double identification; //Methods
String getPersonName(){ return name; }
void setDateOfBirth(String newDateOfBirth){ dateOfBirth = newDateOfBirth; }
//the main method
public static void main (String [] args){
System.out.println("A Person Says: 'Hello World!!'"); Person kemuStudent = new Person();
System.out.println( kemuStudent.getPersonName() ) ;
kemuStudent.setDateOfBirth("21/03/1985"); System.out.println(kemuStudent.dateOfBirth); }
8
• The two parts of an object
– Object = Data + Methods i.e. An object has the responsibility to know and the responsibility to do.
Exercise
• Objective:
– Identify objects and identify relevant data and methods (members of the class)
• In the context of the ongoing police reforms in
Kenya, the Officer Commanding (a local police)
Station has approached you to design a simple
system for the station. The idea is to
automate/computerize the police occurrence book
(OB). The OB is used to record complaints from the
wananchi at the police station. A record of a
complaint shows, among other things the following:
– The police officer who attended to the mwananchi, the date of incidence, details of the person reporting, the report from the mwananchi, etc
• After listening to the mwananchi, the police officer
must indicate on the form whether the reported
incident is a theft, murder, disagreement, accident
or any other
Exercise
• In two classes have been indentified:
– PoliceOfficer – OccurrenceBook
• Required:
– Identify associated data and methods (please work with a colleague next to you)
10
• 10 Minutes
• Consolidation of results
Please note down the results!
• Basic terminologies/concepts of OOP
– Abstraction – Encapsulation – Inheritance – Polymorphism – Aggregation
Basic terminologies/concepts of OOP
• Abstraction is the representation of the
essential features of an object. These are
‘encapsulated’ into an abstract data type.
Create a model from a problem with abstraction.
Represents (relevant) features of the problem domain (from your perspective).
• Encapsulation is the practice of including in an
object everything it needs hidden from other
objects. The internal state is usually not
12
• Inheritance means that one class inherits the
characteristics of another class.
– This is also called a “is a” relationship:
• A car is a vehicle • A dog is an animal • Etc
• Polymorphism means “having many forms”. It
allows different objects to respond to the
same message in different ways, the response
specific to the type of the object.
• E.g.
– the message displayDetails() of the Person class should give different results when send to a PoliceOfficer object (e.g. the rank number).
• Aggregation describes a “has a” relationship.
One object is a part of another object.
• E.g.
– A Bus/car has wheels
– A company has departments
• In an aggregation relationship, the child class instance can outlive its parent class (Basic aggregation)
• There are cases that a child class's instance lifecycle is dependent on the parent class's instance lifecycle (composition aggregation)
– a parent class instance will always have at least one child class instance. when the parent instance is removed/destroyed, the child instance is
automatically removed/destroyed.
– A part (child) class can only be related to one instance of the parent class
14
• Behaviour and Messages
– The most important aspect of an object is its
behaviour (the things it can do). A behaviour is
initiated by sending a message to the object (calling a method)
• Basic terminologies/concepts of OOP
– Abstraction – Encapsulation – Inheritance – Polymorphism – Aggregation
• Questions?
JAVA
• Is used for creating:
– intelligent consumer-electronic devices (cell phones)
– Web pages with dynamic content – large-scale enterprise applications
16 • Java programs normally undergo four phases:
• Edit (Source code (.java))
Programmer writes program (and stores program on disk) • Compile (Byte codes (.class) , as (.exe) in c++
Compiler creates bytecodes from program (.class as .exe in c++)
• Load
Class loader stores bytecodes in memory • Execute
Interpreter: translates bytecodes into machine language
• Other concepts
– The Java Application Programming Interface (API)
• a large collection of ready-made software components. It is grouped into libraries of related classes and interfaces; these libraries are known as packages.
• E.g. System.out.*; java.util.* – Java Virtual Machine (JVM)
Portability of Java
• Through the Java VM, the same application is
18
Java technology series
• Java EE vs. Java SE
– EE: enterprise edition (web services, distribution, RMI, …) – SE: standard edition (stand alone applications)
• Development Tools
– The main tools you'll be using are the javac compiler, the java launcher (java), and the javadoc documentation tool.
• Application Programming Interface (API)
– Java SE Development Kit 6 (JDK 6)
– Offers a wide array of useful classes ready for use in your own applications.
• User Interface Toolkits
– Swing and Java 2D toolkits to create Graphical User Interfaces (GUIs).
• Integration Libraries
– Application Programming Interface (API) – Java RMI (Remote Method Invocation) over
Internet and Inter-ORB (Object Request Broker) protocol technology enable database access
• Part 2: Getting started with Java (Using
Jcreator)
• Exercise objective:
– Get started with Java and understand foundations of a Java program
20
• Required
– Using jCreator, edit (create), compile (build)and run a simple java application that displays “Hello World”
– 20 Mins (because of the typing involved) setting up JCreator?
public class HelloWorld
{
//main method
public static void main (String args [])
{
System.out.println("Hello World!");
}
A closer look
• In the Java programming language, every
application must contain a main method whose
signature is:
• public static void main(String[] args)
– The modifiers public and static can be written in either order (public static or static public).
– You can name the argument anything you want, but most programmers choose "args" or "argv.“
– This is the string array that will contains the command line arguments.
• The main method is the entry point for your application and will subsequently invoke all the other methods required by your program.
• System.out.println("Hello World!");
– uses the System class from the API to print the "Hello World!" message to standard output.
22
• Extended Assignment
– 1. include simple arithmetic in the “HelloWorld” – 2. Create sample classes (with relevant methods)
from the “police reforms” example
Testing a better way of communication
• Visit the address:
http://oop2011jan.wordpress.com/
• Thanks
• The two steps of Object Oriented
Programming
– Making Classes: Creating, extending or reusing abstract data types.
– Making Objects interact: Creating objects from abstract data types and defining their