Beruflich Dokumente
Kultur Dokumente
Lecture Handout
Course Name: Object Oriented Programming (OOP)
Course Code:
Chapter: Introduction To Object Oriented Programming (OOP)
Section:
Instructor: Surafel S
Mail address: jjet2116@gmail.com
Reference: An Introduction to Object-Oriented Programming with Java, Thomas Wu
This course is not a Java course, rather it is a course in Object Oriented Programming (OOP).
Java will be just a “medium of instruction”. What you learn here will be applicable to other
object-oriented languages (C++, C#, VB.NET, Python…
Course Delivery:
We will use google classroom to provide you the lecture course, using google classroom also
Assignments and quiz assessment also can be delivered. Lecture demo also will be shared
through your communication channels for more elaboration. Once the decision was made to
start a face to face, we will have a tutorial session before your final exam assessments. Small
programming assignments, individually or in group. will be given to enhance your
understanding of the lecture
Assignments and project
• The lectures will be much like: አበበ በሶ በላ, You will be told about and shown some object
oriented programming concepts.
• The assignments and lab sessions will be like: አበበ ምን በላ?,You will be required to
Course Note
practice what is discussed and shown in class
• The semester project will be like: አበበ የሚወደውን አይነት በሶ አበስብሱ, you will develop a
full-fledged software. You will learn lots of items that are not covered in class while
doing the project. You may need to refer additional materials. You can seek my
assistance at my office hours (yet to be posted)
Academic honesty
• All answers to assignments and exams should be your own work. Unless it is a group
assignment, you shall not share assignment solutions with your classmates. However,
you are encouraged to discuss general ideas and approaches.
• Your assignments will be run-through an advanced plagiarism detection software. All
students who have submitted identical code will get zero points (it does not matter who
copied from whom)
Developments in software technology continue to be dynamic. New tools and techniques are announced in quick
succession. This has forced the software engineers and industry to continuously look for new approaches to
software design and development, and they are becoming more and more critical in view of the increasing
complexity of software systems as well as the highly competitive nature of the industry. These rapid
advances appear to have created a situation of crisis within the industry. The following issued need to be
addressed to face the crisis:
OOP simplifies the software development and maintenance by providing some concepts so that, it is necessary
to understand some of the concepts used extensively in object-oriented programming. These include: Objects,
Classes, Data abstraction and encapsulation, Inheritance, Polymorphism, Dynamic binding, Message passing,
Coupling, Cohesion, Association, Aggregation, Composition
OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 2
Object
Object means a real word entity such as pen, chair, table etc. object, class, inheritance, polymorphism, abstraction,
and encapsulation.
They may also represent user-defined data such as vectors, time and lists. Programming problem is
analyzed in term of objects and the nature of communication between them. Program objects should be
chosen such that they match closely with the real-world objects. Objects take up space in the memory and
have an associated address like a record in Pascal, or a structure in c.
When a program is executed, the objects interact by sending messages to one another. for example, if “customer”
and “account” are two objects in a program, then the customer object may send a message to the count object
requesting for the bank balance. Each object contain data, and code to manipulate data. Objects can interact
without having to know details of each other’s data or code. It is a sufficient to know the type of message accepted,
and the type of response returned by the objects. Although different author represent them differently fig shows
two notations that are popularly used in object-oriented analysis and design.
Class
We just mentioned that objects contain data, and code to manipulate that data. The entire set of data and code
of an object can be made a user-defined data type with the help of class. In fact, objects are variables
of the type class. Once a class has been defined, we can create any number of objects belonging to that
class. Each object is associated with the data of type class with which they are created. A class is thus a collection
OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 3
of objects similar types. For examples, Mango, Apple and orange members of class fruit. Classes are
user-defined data types and behave like the built-in types of a programming language. The syntax used to create
an object is not different then the syntax used to create an integer object in C, C+, Java etc. If fruit has
been defining as a class, then the statement.
Fruit Mango.
We can also take a look the figure bellow to understand the concept of Class and Objects diagrammatically
Inheritance
Inheritance is the process by which objects of one class acquired the properties of objects of another classes. It
provides code reusability. It enables to achieve runtime polymorphism. In other words, inheritance is the concept
that when a class of objects is defined, any subclass that is defined can inherit the definitions of one or more
general classes.
In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional
features to an existing class without modifying it. This is possible by deriving a new class from the existing one.
OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 4
The new class will have the combined feature of both the classes. The real appeal and power of the inheritance
mechanism is that it allows the programmer to reuse a class i.e almost, but not exactly, what he wants, and to
tailor the class in such a way that it does not introduced any undesirable side-effects into the rest of classes.
Polymorphism
Polymorphism is another important OOP concept. Polymorphism, a Greek term, means the ability to take
more than one form. Polymorphism means one name, many forms. It manifests itself by having multiple methods
all with the same name, but slightly different functionality. There are two basic types of polymorphism.
Overriding and overloading.
An operation may exhibit different behavior is different instances. The behavior depends upon the types
of data used in the operation. For example, consider the operation of addition. For two numbers, the
operation will generate a sum. If the operands are strings, then the operation would produce a third
string by concatenation. The process of making an operator to exhibit different behaviors in different instances is
known as operator overloading.
Fig. 6 illustrates that a single function name can be used to handle different number and different types of
argument. This is something similar to a particular word having several different meanings depending upon
the context. Using a single function name to perform different type of task is known as function overloading. In
summary an Overriding, also called run-time polymorphism. Overloading is a compile-time polymorphism.
Fig 7: Polymorphism
Polymorphism plays an important role in allowing objects having different internal structures to share
the same external interface. This means that a general class of operations may be accessed in the same
OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 5
manner even though specific action associated with each operation may differ. Polymorphism is extensively
used in implementing inheritance.
“An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object
and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.” — G. Booch
The wrapping up of data and function into a single unit (called class) is known as encapsulation. Data
and encapsulation are the most striking feature of a class. The data is not accessible to the outside world, and
only those functions which are wrapped in the class can access it. These functions provide the interface
between the object’s data and the program. This insulation of the data from direct access by the program is
called data hiding or information hiding.
Dynamic Binding
Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic
binding means that the code associated with a given procedure call is not known until the time of the call at run
time. It is associated with polymorphism and inheritance. A function call associated with a polymorphic
reference depends on the dynamic type of that reference.
Consider the procedure “draw” in fig. 6. by inheritance, every object will have this procedure. Its algorithm
is, however, unique to each object and so the draw procedure will be redefined in each class that defines
the object. At run-time, the code matching the object under current reference will be called.
Message Passing
An object-oriented program consists of a set of objects that communicate with each other. The process of
programming in an object-oriented language, involves the following basic steps:
OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 6
3. Establishing communication among objects.
Objects communicate with one another by sending and receiving information much the same way as people
pass messages to one another. The concept of message passing makes it easier to talk about building
systems that directly model or simulate their real-world counterparts.
A Message for an object is a request for execution of a procedure, and therefore will invoke a function
(procedure) in the receiving object that generates the desired results. Message passing involves specifying
the name of object, the name of the function (message) and the information to be sent. Example:
Object has a life cycle. They can be created and destroyed. Communication with an object is feasible as
long as it is alive.
Coupling
Coupling refers to the knowledge or information or dependency of another class. It arises when classes are aware
of each other. If a class has the details information of another class, there is strong coupling. In Java, we use
private, protected, and public modifiers to display the visibility level of a class, method, and field. You can use
interfaces for the weaker coupling because there is no concrete implementation.
Cohesion
Cohesion refers to the level of a component which performs a single well-defined task. A single well-defined task
is done by a highly cohesive method. The weakly cohesive method will split the task into separate parts. The
java.io package is a highly cohesive package because it has I/O related classes and interface. However, the
java.util package is a weakly cohesive package because it has unrelated classes and interfaces.
Association
Association represents the relationship between the objects. Here, one object can be associated with one object or
many objects. There can be four types of association between the objects:
• One to One
• One to Many
• Many to One, and
• Many to Many
OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 7
Let's understand the relationship with real-time examples. For example, One country can have one prime minister
(one to one), and a prime minister can have many ministers (one to many). Also, many MP's can have one prime
minister (many to one), and many ministers can have many departments (many to many).
Aggregation
Aggregation is a way to achieve Association. Aggregation represents the relationship where one object contains
other objects as a part of its state. It represents the weak relationship between objects. It is also termed as a has-a
relationship in Java. Like, inheritance represents the is-a relationship. It is another way to reuse objects.
Composition
The composition is also a way to achieve Association. The composition represents the relationship where one
object contains other objects as a part of its state. There is a strong relationship between the containing object and
the dependent object. It is the state where containing objects do not have an independent existence. If you delete
the parent object, all the child objects will be deleted automatically.
Procedure oriented programming basically consists of writing a list of instructions for the computer to follow and
organizing these instructions into groups known as functions. We normally use flowcharts to organize these
actions and represent the flow of control from one action to another.
In a multi-function program, many important data items are placed as global so that they may be accessed
by all the functions. Each function may have its own local data. Global data are more vulnerable to an
inadvertent change by a function. In a large program it is very difficult to identify what data is used by
OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 8
which function. In case we need to revise an external data structure, we also need to revise all functions that
access the data. This provides an opportunity for bugs to creep in.
Another serious drawback with the procedural approach is that we do not model real world problems very
well. This is because functions are action-oriented and do not really corresponding to the element of the problem.
Some Characteristics exhibited by procedure-oriented programming are:
OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 9
• Objects may communicate with each other through function.
• New data and functions can be easily added whenever necessary.
• Follows bottom up approach in program design.
Object-oriented programming is the most recent concept among programming paradigms and still
means different things to different people.
3. OOPs provides the ability to simulate real-world event much more effectively. We can provide
the solution of real word problem if we are using the Object-Oriented Programming language.
Real-business system are often much more complex and contain many more objects with complicated
attributes and method. OOP is useful in these types of application because it can simplify a complex
problem. The promising areas of application of OOP include:
• Real-time system
• Simulation and modeling
• Object-oriented data bases
• Hypertext, Hypermedia, and expertext
• AI and expert systems
• Neural networks and parallel programming
• Decision support and office automation systems
• CIM/CAM/CAD systems
The object-oriented paradigm sprang from the language, has matured into design, and has recently moved into
analysis. It is believed that the richness of OOP environment will enable the software industry to improve
not only the quality of software system but also its productivity. Object-oriented technology is certainly going
to change the way the software engineers think, analyze, design, and implement future system.
Programming languages such as Cobol, Fortran do not have structural principles. They use the Goto statement to
control the flow of the program. Therefore, programs using this type of code are made up of many jumps and
conditional statements that make it difficult to understand.
Therefore, C was invented in 1970, to replace the assembly language and to create a structured, effective and
high-level language. The development of C was the result of the development process started with BCPL by
Dennis Ritchie. BCPL is an old language developed by Martin Richard. Ken Thompson developed a language
called B, which was influenced by BCPL.
OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 11
C is a processor-oriented programming language; it is easy to execute and understand. C became quite famous at
that time because it was reliable, simple, and easy to use.
Though C was a quite efficient and successful programming language, the complexity of the program was seeking
more efficient language to solve problems. When we write a program in C, it has a limit, such as a maximum of
25000 lines of code, beyond which it cannot handle the complexity. But writing and managing large programs
was a demand at that time. So, a new concept came.
C++ came with object-oriented programming features. C++ is the extension of C language which has been used
extensively. It is a powerful modern language that includes the power and simplicity of C and the characteristics
of OOP. C++ provides more functional software benefits than C.
C ++ with OOP became quite famous but then a new problem arose, to control the software on different machines,
a separate compiler is required for that CPU. But building a C++ compiler was quite expensive. Therefore, an
efficient and easy solution was needed, and this requirement became the reason for the creation of Java, which is
a portable and platform-independent language.
The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured,
High Performance, Multithreaded, Architecture Neutral, Object-Oriented, Interpreted, and Dynamic". Java was
developed by James Gosling, who is known as the father of Java, in 1995. James Gosling and his team members
started the project in the early '90s. Currently, Java is used in internet programming, mobile devices, games, e-
business solutions, etc. There are given significant points that describe the history of Java.
James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in
June 1991. The small team of sun engineers called Green Team.
Initially designed for small, embedded systems in electronic appliances like set-top boxes.
Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.
After that, it was called Oak and was developed as a part of the Green project.
Why Oak? Oak is a symbol of strength and chosen as a national tree of many countries like the
U.S.A., France, Germany, Romania, etc.
In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies.
Why had they chosen java name for Java language? The team gathered to choose a new name.
The suggested words were "dynamic", "revolutionary", "Silk", "jolt", "DNA", etc. They wanted
something that reflected the essence of the technology: revolutionary, dynamic, lively, cool,
unique, and easy to spell and fun to say.
According to James Gosling, "Java was one of the top choices along with Silk". Since Java was
so unique, most of the team members preferred Java than other names.
OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 12
Java is an island of Indonesia where the first coffee was produced (called java coffee). It is a
kind of espresso bean. Java name was chosen by James Gosling while having coffee near his
office.
Notice that Java is just a name, not an acronym.
Initially developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle
Corporation) and released in 1995.
In 1995, Time magazine called Java one of the Ten Best Products of 1995.
JDK (Java Development Kit) 1.0 released in(January 23, 1996). After the first release of Java,
there have been many additional features added to the language. Now Java is being used in
Windows applications, Web applications, enterprise applications, mobile applications, cards,
etc. Each new version adds the new features in Java.
From the table above we can see that the naming and the version number have been changing over times:
Versions 1.0 and 1.1 are named as JDK (Java Development Kit).
From versions 1.2 to 1.4, the platform is named as J2SE (Java 2 Standard Edition).
From versions 1.5, Sun introduces internal and external versions. Internal version is continuous from
previous ones (1.5 after 1.4), but the external version has a big jump (5.0 for 1.5). This could make
confusion for someone, so keep in mind that version 1.5 and version 5.0 are just two different version
names for only one thing.
From Java 6, the version name is Java SE X.
Major versions were released after every 2 years, however the Java SE 7 took 5 years to be available after its
predecessor Java SE 6, and 3 years for Java SE 8 to be available to public afterward.
OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 13
in this chapter, you will study the structure of the Java program. Such as how to create a simple Java program and
what its different sections mean.
Section Description
Documentation You can write a comment in this section. Comments are beneficial for the programmer
Section because they help them understand the code. These are optional, but we suggest you use
them because they are useful to understand the operation of the program, so you must
write comments within the program.
Package You can create a package with any name. A package is a group of classes that are defined
statement by a name. That is, if you want to declare many classes within one element, then you can
declare it within a package. It is an optional part of the program, i.e., if you do not want
to declare any package, then there will be no problem with it, and you will not get any
errors. Here, the package is a keyword that tells the compiler that package has been
created.
It is declared as:
package package_name;
Import This line indicates that if you want to use a class of another package, then you can do this
statements by importing it directly into your program.
Example:
import calc.add;
Interfaces are like a class that includes a group of method declarations. It's an optional
Interface
section and can be used when programmers want to implement multiple inheritances
statement
within a program.
A Java program may contain several class definitions. Classes are the main and essential
Class Definition
elements of any Java program.
Every Java stand-alone program requires the main method as the starting point of the
Main Method program. This is an essential part of a Java program. There may be many classes in a Java
Class program, and only one class defines the main method. Methods contain data type
declaration and executable statements.
Table 2: Java Programming Structure
Here is an example of the Hello Java program to understand the class structure and features. There are a few lines
in the program, and the primary task of the program is to print Hello Java text on the screen.
OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 14
Writes the words "Hello Java" on the screen */
public static void main(String[] args)
{
System.out.println("Hello Java.");
}
}
Program Output
Hello Java,
/* Comments */ The compiler ignores comment block. Comment can be used anywhere in the
program to add info about the program or code block, which will be helpful for
developers to understand the existing code in the future easily.
Braces Two curly brackets {...} are used to group all the commands, so it is known that the
commands belong to that class or method.
public static void • When the main method is declared public, it means that it can also be used
main by code outside of its class, due to which the main method is declared public.
• The word static used when we want to access a method without creating its
object, as we call the main method, before creating any class objects.
• The word void indicates that a method does not return a value. main() is
declared as void because it does not return a value.
• main is a method; this is a starting point of a Java program.
You will notice that the main method code has been moved to some spaces left. It is
called indentation which used to make a program easier to read and understand.
String[] args It is an array where each element of it is a string, which has been named as "args". If
your Java program is run through the console, you can pass the input parameter, and
main() method takes it as input.
System.out.println(); This statement is used to print text on the screen as output, where the system is a
predefined class, and out is an object of the PrintWriter class defined in the system.
The method println prints the text on the screen with a new line. You can also use
print() method instead of println() method. All Java statement ends with a semicolon.
Table 3: Description of the Program structure
Here are the most important points to note about the Java programs:
OBJECT ORIENTED PROGRAMMING (OOP): CHAPTER 01: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) 15