Introduction To Practical Computer Science II
Lecture: Introduction To OOP
Herat University
Computer Science Faculty
Lecturer: Azita Azimi
Date: May 28, 2021
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 2
Course Introduction
¡ Course Name : Introduction to Practical Computer Science II
¡ Number of Credits: 5 CP
¡ Course Description:
¡ Introduction to Object Oriented Programming
¡ Students learn to analyze small problems and to find solutions for them
¡ Students learn to distinguish between Structural programming and Object-Oriented programming
¡ Understand Object-Oriented concept
¡ Understand main functionalities of Object-Oriented Programming
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 3
Course Text book
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 4
Teaching Methods/Strategies
¡ Lecture presentation
¡ Exercises:
¡ Assignments
¡ Debriefing
¡ Q &A
¡ Tutorial
¡ Q &A
¡ Panel discussions
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 5
Grading Scheme
¡ Exercises and Class Activity: 20 %
¡ Mid-Term Exam: 20 %
¡ Final Exam: 60 %
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 6
Organizational Issues
¡ Please come on time
¡ Be quiet during the lectures
¡ Silent your mobile phone
¡ If you have any question just rise your hand
¡ Don’t take photo of the codes during class
¡ Study the reference book carefully
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 7
Outline:
¡ Different Styles of Programming
¡ Procedural/Structure Programming
¡ Object Oriented Programming
¡ Why Object Oriented Programming?
¡ Basic Components of OOP
¡ What is an object?
¡ Class
¡ Object vs. Class
¡ Class Structures
¡ Constructors
¡ Constructors Usages
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 8
Different Styles Of Programming
¡ There are two styles of programming:
1. Procedural/Structure Programming
2. Object Oriented Programming
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 9
Procedural/Structure Programming
¡ A program in a procedural language is basically a list of instructions.
¡ As programs become larger they are usually broken down into
¡ smaller units, such as functions, procedures, subroutines.
¡ Procedural language's techniques were based on creating programs with a structure,
clean flow.
¡ Examples of procedural languages: Pascal, Fortran
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 10
Problems with Structured Programming:
¡ For many years the structured programming model was prevalent in the software
industry.
¡ But structured programming is a program development methodology. It does not
deal with the real-world problem at the origin of every software project
¡ We can say that structured programming focuses on the solution-set of a software
project. But defining, and modeling the problem-set is one of the major blocks of
software development,
¡ The object-oriented approach focuses on this problem set.
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 11
Object Oriented Programming
¡ In the beginning of 1960 attempt to design a programming language that extended in
a way suitable for performing Computer simulations of real-world situation.
¡ So they create Objects Programming , it models real world Objects.
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 12
Why Object Oriented Programming?
¡ The following are the most important claims of the object-oriented approach:
1. The OO model can be understood by clients and software users with no technical
knowledge of programming or computers. For this reason, program analysis and design
methods based on object-orientation facilitate communications with clients.
2. Real-world problems can be modeled in object-oriented terms.
3. Object-oriented programming languages promote and facilitate code reuse. Reusing code
increases programmer productivity.
4. The need to modify program behavior with minimal changes to existing code.
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 13
Basic Components of OOP
¡ Basic components of Object Oriented Programming Language:
1. Objects
2. Classes
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 14
What is an Object?
¡ If we consider the real-world we can find many objects around us:
All these objects have state and behavior!
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 15
What is an Object?
Object = State + Behavior
¡ State defines the object attributes
¡ Behavior defines what the object does.
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 16
Car Object Example
¡ State: ¡ Behavior:
¡ four wheels, ¡ driving,
¡ four doors, ¡ Brake,
¡ a car radio ¡ slowing down,
¡ more. ¡ Speed Up.
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 17
Objects in Programming
¡ Object is most important feature of OOP.
¡ In object-oriented programming we create software objects that model real world
objects.
¡ They have state and behavior.
¡ An object in software programs maintains its state in one or more variables.
¡ An object in software programs implements its behavior with methods
An object is a software bundle of variables and related methods
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 18
Cont…
¡ An object is also known as an instance. An instance refers to a particular object.
¡ A software object has variables that formally known as instance variables. They
contain state for particular car object.
¡ In addition of variables a software car would have methods to brake, speed up and
change gears. These methods are known as instance methods.
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 19
Class
¡ In real world, you often have many objects of the same kind.
¡ Objects of the same kind are similar and you can create a prototype for those
objects.
¡ A prototype of objects is called a class.
¡ Using object oriented terminology, we say that your car object is an instance of the
class of cars.
¡ Object is a variable of user defined type called class.
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 20
Car Class
¡ A class is a prototype, that defines the variables and the methods common to all
objects of a certain kind.
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 21
Instance Of a Class
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 22
Instance Of a Class
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 23
Class Example: Teacher
1. Mr.Farman teaches Database
2. Mr.Ali teaches Software Engineering
3. Mr.Ahmadi teaches Computer Science
¡ Each one is a Teacher
¡ We say these objects are instances of the Teacher class
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 24
Class Example: Students
1. Ahmad studies mathematics
2. Shoaib studies physics
3. Mahmood studies chemistry
¡ Each one is a Student
¡ We say these objects are instances of the Student class
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 25
Object vs Class
¡ Objects ¡ Classes
¡ has state + behavior ¡ we can implement state and behavior
¡ has specific state + behavior ¡ has specifiation of state and behavior
Object is an instance of a class
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 26
Student Class
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 27
Class Structure:
¡ An object is created based on class.
¡ A class can contain:
¡ Class Declaration
¡ Variables
¡ Constructors
¡ Standard
¡ Overloaded
¡ Methods
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 28
Student Class:
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 29
Constructor:
¡ A constructor is a special method.
¡ Constructors has the same name as our class.
¡ Constructor do not return any value. So it should not have any return type.
¡ A class can have more than one constructor.
¡ There are Two types of Constructors:
¡ Standard
¡ Overloaded
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 30
Standard (Default) Constructor
¡ Standard (Default) Constructor.
¡ Constructor without any argument is called the Standard constructor.
¡ If we do not define a default constructor the compiler will generate a default constructor.
¡ Example:
class Student{
// member variables go here
public Student(){
// This constructor does not parameter
}
}
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 31
Overloaded Constructor
¡ Overloaded Constructor
¡ As any other method, a constructor can get parameters.
¡ This parameters can be used to set initial values of the objects properties.
¡ A constructor with arguments is called the Overload constructor.
¡ Example:
class Student{
// member variables go here
public Student(String name){
// This constructor has one parameter, name.
}
}
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 32
Constructor usage
The constructor is called when an object of the class is created
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 33
Any Question???
INTRODUCTION TO JAVA PROGRAMMING (11TH ED.) Y. DANIEL LIANG 34