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

1. Intro new

Uploaded by

ahmadashrruf3
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)
5 views

1. Intro new

Uploaded by

ahmadashrruf3
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/ 52

Object Oriented Programming

(OOP)

Dr. Abdurrahman A. Nasr


Al-Azhar University
Course objectives

1. Understand the basic concepts OOP.


2. Aware of software design techniques.
3. Capable of minimizing development effort and
maximizing reusability
4. Design and implement software programs with
different Object-oriented elements
5. Utilize codes of practice, programming standards and
quality guidelines to assist in building robust software
systems

2
Course materials
Textbook
1. Object-Oriented Programming with Java, 2nd Edition, Barry J.
Holmes - Daniel T. Joyce
2. An Introduction to Object-Oriented Programming with Java Fifth
Edition, C. Thomas Wu
3. Lecture notes by the lecturer

Tools
1. Java 11 SDK
2. Eclipse IDE
3. NetBeans IDE

Similar online courses


1. Object Oriented Programming in Java | Coursera
2. Master Object Oriented Design in Java | Udemy
3. Object Oriented Programming in Java | Udacity 3
Course syllabus
1. introduction to OOP
2. Inheritance
3. Polymorphism
4. Encapsulation
5. Software abstraction and contracts
6. Collections
7. Generics
8. Design patterns
9. UML
4
10. Multi-threading
Lecture 1

Introduction to OOP
Lecture Objectives

➢History

➢OOP Introduction

➢SDLC

➢Objects
l Programming Languages
Generations
•Generation 1—machine languages:
•Program data entered directly into RAM in form of 1s and 0s
•Generation 2—assembly languages:
•Mnemonic symbols represent instructions and data.
•Generation 3—high-level languages:
•Java, C#, python, C++, VisualBasic, …
•Designed to be easy to write, read, and manipulate.
•Requires translator component to translate to low level language
•Generation 4—high-level languages:
•Human terms
•SQL, python, MATLAB
l Programming Languages
l Programming Languages

l
l Programming Languages
Compiler
• Translates a program from a high-level to a low-
level language.
• Compile source program
• produces machine -or assembly- language
programs called object.
• Most high-level languages need a different
compiler for each type of computer and for
each operating system.
• Compilers are very large programs that are
expensive to produce.
Programming Languages
l

Java Byte-Code
• The Java compiler translate a Java program into intermediate
language (byte-code)
• An interpreter translates byte-code instruction to machine-
language
• the byte-code can be used on any computer with a byte-code
interpreter and without a need to recompile.
• Write once, run anywhere

Machine
language
00110101
java: Serves as a Java interpreter used to run Java
applications by reading and interpreting bytecode files

javac: Serves as a Java compiler used to translate Java


source code files.

JAVA DEVELOPMENT javadoc: Creates HTML documentation for Java source


KIT (JDK) code files

javap: Serves as a Java disassemble used to convert


bytecode files into a Java program description

jdb: Serves as a Java debugger used to find errors in


Java programs

jar: Serves as archive used to package related class


libraries into a single executable JAR file.
Java Virtual Machine (JVM)
Software development life cycle (SDLC)

SDLC: A view of software development in which phases


l

of development occur incrementally


▪Standardizes software development
oSimplifies understanding the project scope
oMinimizes software flaws
l The Software Development Process

Waterfall model: A version of the SDLC


▪Phases:
oCustomer request/ requirements
oAnalysis
oDesign
oImplementation
oIntegration
oTesting
oMaintenance
Waterfall model
401: SW Engineering

301: Database

201: OOP YOUR PATH

102: Data structure & Algorithms

101:Programming (Java)
l Effort of Software Phases

• Improving performance or other attributes


• Adapting the software to new hardware
• Adding features and functions to the software to respond to new user requirements
• Improving efficiency and reliability
Programming Techniques
➢ Unstructured programming
• Where all implementation in one function: Main(){-------}
• No concept of procedures
• No concept of local variables, only Global variables

➢Procedure programming
• Where repeated part of code separated in a function e.g.,
factorial function

➢Modular programming
• No. of functions become huge
• functions Partition into logical parts according to
business/functionality e.g. all mathematical functions together
• can load modules that include functions we need only
Unstructured programming

Case Study - A Payroll Program


▪Consider a payroll program that processes employee
records at a small manufacturing firm. This company has
three types of employees:
• Managers: Receive a regular salary.
• Officer: Receive an hourly wage and are eligible
for overtime after 40 hours.
• Salesman: Are paid according to a piece rate.
Procedural programming
STRUCTURED PROGRAMMING
OO Programming Concepts
➢Object-oriented programming (OOP) involves programming using a
more structured and isolated entities, called Objects.

➢Object = real world runtime entity, for example, student, button,


text, customer, screen,...
➢Objects communicate with each other thru events/calls

➢Objects have a unique identity, state, and behaviors.


o Identity = hashcode
o State = properties + values.
o Behavior = method | function| procedure.
24
l
Objects
❑An object is a thing.

❑ Example of Objects
John Mary 238-49-1357
Customer Dog Account Object name

Object ‘type’
OO PROGRAMMING
Object 2
Object 1
l

messages
Data
l Data
l

Function
l Function
l

Object 3
l

Data
l

Function
l
• To create sets of objects that work together
concurrently to produce a better SW that models
problem domain
• Promote greater design
• Easier to maintain
WHY OOP? • Adapts to changing requirements
• Code reusability & reliability
• Robust programming structure
• High level of abstraction
• Modeling reality
Modular programming

Case Study REVISTED - A Payroll Program


▪Consider a payroll program that processes employee
records at a small manufacturing firm. This company has
three types of employees:
• Managers: Receive a regular salary.
• Officer: Receive an hourly wage and are eligible
for overtime after 40 hours.
• Salesman: Are paid according to a piece rate.
MODULAR PROGRAMMING
l Classes

➢Classes are abstraction constructs, that define objects of the same type.
➢A class provides a special type of methods, known as constructors, which
are invoked to construct objects from the class.
➢A Java class uses variables to define data fields and methods to define
behaviors.

Audi-2020 BMW -2021 Ferrari-2000


Car Factory

Class Objects
30
l
Class and Object
Class:
▪Category of things
▪Template to create objects
▪ A class name can be used in Java as the type
▪Defines the variables and methods common to objects of a same
type.

Object
▪a particular item that belongs to a class
▪Also called an “instance”
▪Example
String s1 = "Hello";

String is the class,


the variable s1=“Welcome” is an object (instance of the String class)
with a unique value ‘Welcome’
Stack and Heap
Stack and Heap
Stack and Heap
Method and Parameters

▪ Modifiers: visibility of the method, public, private, package, and


protected

▪ Return-type: value data type returned after method exec


▪ Parameter-list: declares the data variables that are passed to
and used by the method
▪ method-name
Class members

❑ Member variable
▪ Anywhere in the class
▪ Outside all methods
▪ Can be accessed
▪ internally with “this”
▪ externally with object reference

l
Instance method

▪ Method inside class that behaves differently for each class


instance (i.e. object)
Packages group related classes
Java hierarchically organizes classes into packages*
java.lang
java.text
java.util

Classes need to be referred using its complete name
(package + class name): for example, java.util.Calendar
Packages can be “imported” to avoid writing package names every
time you use a class (except java.lang)

import java.util.*;
Creating Package

-Date -Date -Date


-Point -Point -Point

second.section1 second.section2 second.section3

package second.section1; package second.section2; package second.section3;


public Class Date { public Class Date { public Class Date {
private int day, month, year; private int day, month, year; private int day, month, year;
public Date (){ public Date (){ public Date (){
day = 1; day = 1; day = 1;
month= 1; month= 1; month= 1;
year= 206; year= 206; year= 206;
} } }
} } }
package second.section1; package second.section2; package second.section3;
public Class Point{ public Class Point{ public Class Point{
Using Package
❑ Organizing your classes into folders
• A class can only be in one package
• No duplicate class definition in the same package
• Put the package statement at the beginning
• Packages correspond to directories in local file system

❑ Default Package
• A class without any package defined is in a “default package”
• Classes in the default package cannot be referenced outside the
default package
Point class example
▪ Package definition
▪ Defining member variables
▪ Defining member methods
▪ Define methods with return data type
▪ Defining method with void return type
▪ Interaction with similar objects
▪ Using methods to update member variables
▪ Setter & Getters (aka accessors and mutators)
Constructor

▪ Used to instantiate an instance of a class (i.e. object)


▪ Allocate a new memory storage using “new” keyword
▪ Special shortcut for the String class
▪ Default constructor (zero-argument constructor)
▪ A constructor without any parameter
▪ If a programmer doesn’t define any constructor for a class,
JRE will implicitly create a default constructor as in Point class
Constructor
USER DEFINED CONSTRUCTOR
▪ Constructor explicitly defined by the programmer
▪ Very similar to an instance method, except it’s named as the class name
and has no return type (not even void).
▪ May include parameters

public class Vehicle {


public Vehicle(...) //no return :“void” or any other data type
{
// object initialization
}
}
Default Constructors
Person.java

public class Person {


public String firstName, lastName;
}
PersonTest.java
public class PersonTest {
public static void main(String[] args) {
Person p = new Person();
p.firstName = “Hamza";
p.lastName = “Ezz";

/*It took three lines of code to make a properly constructed person. It would
be possible for a programmer to build a person and forget to assign a first or
last name.*/
}
}
User-Defined Constructor
public class Person { Person.java
public String firstName, lastName;
public Person(String initialFirstName, String initialLastName) {
firstName = initialFirstName;
lastName = initialLastName;
}
}

PersonTest.java
public class PersonTest {
public static void main(String[] args) {
Person p = new Person(“Hamza", “Ezz");

/*It took one line of code to make a properly constructed person. It


would not be possible for a programmer to build a person and forget to assign
a first or last name*/
}
}
User-Defined Constructor Example
Date.java
public Class Date {
private int day, month, year; today
public Date (int d, int m, int y){
day = d; day=
month= m; month=
year= y;
} year=
}

DateTest.java meeting

public Class TestDate { day=


public static void main (String arg[]){ month=
Date meeting= new Date(5,11,2015); year=
Date today = new Date(20,10,2015);
}
}
User-Defined Constructor (zero-Arg.)
Date.java
public Class Date {
today
public int day, month, year;
public Date (){
day = 1; day=
month= 1; month=
year= 2013;
year=
}
}
meeting
DateTest.java
day=
public Class TestDate { month=
public static void main (String arg[]){
year=
Date meeting= new Date();
Date today = new Date();
}
}
PRIMITIVE AND OBJECT ASSIGNMENT

49
GARBAGE COLLECTION

As shown in the previous figure, after the assignment


statement c1 = c2,

c1 points to the same object referenced by c2.

The object previously referenced by c1 is no longer referenced.

This object is known as garbage.

Garbage is automatically collected by JVM.


• The Garbage Collector Automatically frees an object, if not referenced.
• prevents memory leaks

50
ASSIGNMENT

▪ Design and implement a “Company” class


▪ Define company attribute
▪ Define company operations
▪ Add setter/getters methods
▪ Check for valid company data in the constructor
▪ Draw class diagram and object diagrams for
▪ Company
▪ Department
▪ User
▪ Customer
▪ Employee
▪ Manager
▪ Engineer
51
Instance method
References

▪ Tutorials point
▪ How to do in Java
▪ OOP course notes

You might also like