0% found this document useful (0 votes)
77 views31 pages

Chapter 1 Introduction To OOP

Java OOP

Uploaded by

sisay alemu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views31 pages

Chapter 1 Introduction To OOP

Java OOP

Uploaded by

sisay alemu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Object Oriented Programming(OOP)

CHAPTER 1
Introduction to Object
Oriented
Programming
OBJECTIVES

After studying this chapter, students should be able to:

• Learn about Programming Paradigms


 Structured Vs Object-Oriented paradigm
• Understand OOP Concepts and Features
• Fundamental Java Programming Structures:
prepared by: Melkamu D. 10/23/2024 2
PROGRAMMING
• Programming PARADIGMS
Paradigm is a way of conceptualizing what it means to perform
computation and how tasks to be carried out and organized on a computer.

Structured Programming

• Problem solving would involve the analysis of processes in terms of the


procedural tasks carried out and the production of a system whose representation
is based on the procedural flow of the processes.
• data was separate from code.
• programmer is responsible for organizing everything in to logical units of
code/data
• A procedural program is divided into functions, and (ideally, at
prepared by: Melkamu D.
least) each 3
10/23/2024

function has a clearly defined purpose & a clearly defined interface to the other
functions in the program.
CONTD…

Disadvantages of Structured Programming

1. Unrestricted Access

• Functions have unrestricted access to global data.


2. Real-World Modeling

• Unrelated functions and data, the basics of the procedural


paradigm, provide a poor model of the real world.

3. Difficult of Creating New Data Types

• Traditional languages are not extensible because they will


not let you create new data types.
prepared by: Melkamu D. 10/23/2024 4
CONTD…

OOP Approach
• A modern programming paradigm that allows the
Programmer to model a problem in a real-world fashion
as an object.
• Major objective is to eliminate some of the flaws
encountered in the procedural approach.
• OOP allows us to decompose a problem into number of
entities called objects and then build data and methods
(functions) around these entities.
• The data of an object can be accessed only by the
methods associated with the object
prepared by: Melkamu D. 10/23/2024 5

• Follows bottom-up approach in program design


CONTD…
Features of OOP

Emphasis is on data rather than procedure.


Programs are divided into objects.
Data Structures are designed such that they characterize the
objects.

Methods that operate on the data of an object are tied together in


the data structure.

Data is hidden and can not be accessed by external functions.


prepared by: Melkamu D. 10/23/2024 6

Objects may communicate with each other through methods.


BASIC OOP CONCEPTS

The following are the basic OOP concepts:

1. Objects

2. Classes

3. Data Abstraction

4. Data Encapsulation

5. Inheritance

6. Polymorphism

7. Dynamic Binding
prepared by: Melkamu D. 10/23/2024 7

8. Message Passing
CONTD…

1.Object
• An object is any real world entity which may represent place, person,
data item related to program.
• An object has state, behavior and identity.
• Ex. A ‘Mouse’ object has,
State: moving or working
Behavior: picking or pointing an object on the screen
Identity: color, company, Identification No. etc.
• An object is a variable/instance of class.
• An object is a run-time entity.
2. Class
• Is the template or blueprint that defines the states and
the behaviors common to all objects of a certain kind.
• It is a collection of objects of similar type.
•prepared
Classes are
by: Melkamu D. user-defined data types & behave
10/23/2024 like the
8

built-in types of programming language.


CONTD…
3. Data Abstraction
• Abstraction means representing essential features
without including the background details or
explanations.
• A classes can use the concept of Abstraction and are defined as
list of abstract data and functions to operate on these data.
• Since the classes use the concept of Data Abstraction, they are
known as Abstract Data Type(ADT).
4. Data Encapsulation
• The wrapping up of data and methods into a single
unit (called class) is known as encapsulation.
• This insulation of the data from direct access by
the program
prepared by: Melkamu D. is called data hiding. 10/23/2024 9
CONTD…
5.Inheritance
• Is the process by which objects of one class
acquire the properties of objects of another class.
• It provides the idea of reusability( reusing the
code).

6. Polymorphism
• In polymorphism, ‘Poly’ means many and ‘morph’ means
forms, i.e. many forms.
• Is the ability to take more than one form. It allows
a function responding in different ways.

7.Dynamic Binding
prepared by: Melkamu D. 10/23/2024 10

• Dynamic binding means that the code associated


CONTD…

8.Message Passing
• The process of invoking an operation of an object is
called Massage Passing.
• In response to the given massage, the respective
method or operation is called.
• OOPs includes objects which communicates by
sending/ receiving information with each other.
• Message Passing involves specifying the name of the
object, the name of the function (message) and the
information to be sent.
employee.salary (name);
prepared by: Melkamu D. 10/23/2024 11

• A message for an object is a request for the execution


INTRODUCTION TO JAVA
• Java is an Object Oriented Programming language
developed by Sun Microsystems in the year 1991.
• Initially it was named as “Oak” by James Gosling
• In 1995, “Oak” is renamed to “Java” because the name
“Oak” did not survive legal registration.
• James Gosling and his team members were consuming
a lot of coffee while developing this language.
• Good quality of coffee was supplied from a place called
“Java Island’. Hence they fixed the name of the
language as Java. The symbol for Java language is cup
and saucer.
prepared by: Melkamu D. 10/23/2024 12

• Sun formally announced Java at Sun World conference


FEATURES OF JAVA
• Simple: Learning and practicing Java is easy because of
resemblance with C and C++.
• Object Oriented: Unlike C++, Java is purely OOP.
• Distributed: Java is designed for use on network; it has
an extensive library which works in agreement with
TCP/IP.
• Secure: Java is designed for use on Internet. Java
enables the construction of virus-free, tamper free
systems.
• Robust (Strong/ Powerful): Java programs will not
crash because of its exception handling and its memory
prepared by: Melkamu D. 10/23/2024 13

management features.
• Interpreted:
CONTD… Java programs are compiled to generate the byte
code(.class file). This byte code can be interpreted by the
interpreter contained in JVM.
• Architectural Neutral Language: Java byte code is not machine
dependent, it can run on any machine with any processor and with
any OS.
• High Performance: Along with interpreter there will be JIT (Just
In Time) compiler which enhances the speed of execution.
• Multithreaded: Executing different parts of a program
simultaneously is called multithreading. This is an essential feature
to design server side programs.
• Dynamic: We can develop programs in Java which dynamically
prepared by: Melkamu D. 10/23/2024 14
change on Internet (e.g.: Applets).
JAVA ENVIRONMENT
• Java Environment includes a large number of development tools and
hundreds of classes and methods.
• The development tools are part of the system known as Java
Development Kit (JDK) and the classes and methods are part of the
Java Standard Library (JSL), also known as Application Programming
Interface (API).
• JDK comes with a collection of tools that are used for developing and
running java programs:
• appleviewer (for viewing java applets )
• javac (java compiler)
• java (java interpreter)
• javap (java disassembler)
• javah (for C header files)
•prepared
javadoc (forD.creating HTML documents)
by: Melkamu 10/23/2024 15

• jdb (Java debugger)


JAVA API
• It includes hundreds of classes and methods grouped into several functional
packages.
• Most commonly used packages are:
• Language Support Package: a collection of classes and methods required
for implementing basic features of java.
• Utilities Package: a collection of classes to provide utility functions such
as date and time functions.
• Input/output Package: a collection of classes required for input/output
manipulation.
• Networking Package: a collection of classes for communicating with
other computers via Internet.
• AWT Package (Abstract Window Tool kit package): contains classes that
implements platform-independent graphical user interface.
•prepared
Applet Package: includes set of classes that allows us to create java
by: Melkamu D. 10/23/2024 16
applets.
• In PROGRAMMING STRUCTURE
Java programming language:
• A program is made up of one or more classes
• A class contains one or more methods
• A method contains program statements
• In Java, first we need to import the required packages. By default,
java.lang.*; is imported. Java has several such packages in its
library.
• A package is a kind of directory that contains a group of related
classes and interfaces. A class or interface contains methods.
• Since Java is purely an Object Oriented Programming language,
we cannot write a Java program without having at least one class
or prepared
object. So, itD. is mandatory to write a class in Java
by: Melkamu program.
10/23/2024 17 We
should use class keyword for this purpose and then write class
// comments about the class
CONTD…
public class Class-name
class header

// comments about the method


public static void main (String[] args)
class body
{
method header
method body
}

Comments can be placed almost anywhere


prepared by: Melkamu D. 10/23/2024 18
FIRST JAVA PROGRAM

// MyProgram.java
public class Sample
{
public static void main( String args[])
{
System.out.println(“ Hello World! “);
}
}
Let us discuss the program line by line:
• Class Declaration: the first line class Sample declares a class,
which is an object constructor. Class is keyword and declares a
new class definition and Sample is a java identifier
prepared by: Melkamu D.
that specifies
10/23/2024 19
the name of the class to be defined.
CONTD…
• Braces : Every class definition in java begins with an opening
brace “{“ and ends with a closing brace “}”.
• The main line: the third line public static void main(String args[])
defines a method named as main, is the starting point for the
interpreter to begin the execution of the program.
• public: is an access specifier that declares the main method as
“unprotected” and therefore making it accessible to all other
classes.
• static: declares that this method as one that belongs to the entire
class and not a part of any objects of the class.
• Main must always be declared as static since the interpreter
uses this method before any objects are created.
• prepared
void :by:states that the main method does not return
Melkamu D.
any value
10/23/2024 20
(but
prints some text to the screen.)
CONTD…

• All parameters to a method are declared inside a pair of


parenthesis. Here, String args[ ] declares a parameter
named args, which contains array of objects of the class type
String.

• The Output Line: The only executable statement in the


program is System.out.println(“Hello World!” );
• This is similar to cout<< constructor of C++.

• The println method is a member of the out object, which is a


static data member of System class.
prepared by: Melkamu D. 10/23/2024 21
JAVA PROGRAM STRUCTURE

Documentation Section Suggested

Optional
Package Statement

Optional
Import Statements

Optional
Interface Statements

Class Definitions Optional

Main Method class


{ Essential
Main Method Definition
} D.
prepared by: Melkamu 10/23/2024 22
DOCUMENTATION SECTION
• Comprises a set of comment lines giving the
name of the program, the author and other
details, which the programmer would like to
refer at a later stage.
• Comments are description about the aim and features of the
program.
• Comments increase readability of a program.
• Java supports three types of comments:
1. Single line comment //
2. Multiple line comment /*………………
prepared by: Melkamu D. ………………*/ 10/23/2024 23

3. Documentation comment /**….*/


PACKAGE STATEMENT
• Is the first statement in Java file and is optional.
• It declares a package name and informs the compiler that the
classes defined here belong to this package.
Example: package student;

Import statements
• Next to package statements (but before any class definitions)
a number of import statements may exist. This is similar to
#include statements in C or C++.
• Using import statements we can have access to classes that
areprepared
partby:of other named packages.
Melkamu D. 10/23/2024 24

Example: import java.lang.Math;


INTERFACE STATEMENTS
• An interface is like a class but includes a group of method
declarations.
• is also an optional section.
• is used only when we wish to implement the multiple
inheritance features in the program

Class Definitions
• A Java program may contain multiple class definitions.
• Classes are the primary and essential elements of a Java
program.
• These classes are used to map objects of real-world
problems.
• The number
prepared by: Melkamuof
D. classes depends on the complexity of the
10/23/2024 25

problem.
MAIN METHOD
• Since every Java stand-alone program requires a
main method as its starting point, this class is the
essential part of a Java program.

• A simple Java program may contain only this part.


• The main method creates objects of various classes
and establishes communications between them.

• On reaching the end of main, the program


terminates and control passes back to the operating
system.
prepared by: Melkamu D. 10/23/2024 26
CREATING A SOURCE FILE
• Type the program in a text editor (i.e. Notepad, WordPad, Microsoft
Word or Edit Plus).
• We can launch the Notepad editor from the Start menu by
selecting Programs > Accessories > Notepad. In a new
document, type the above code (i.e. Sample Program).
• Save the program with filename same as Class_name (i.e.
Sample.java) in which main method is written. To do this in Notepad,
first choose the File > Save menu item. Then, in the Save dialog box:
• Using the Save in combo box, specify the folder (directory) where
you'll save your file.
• In this example, the directory is JQR on the D drive.
• In the File name text field, type "Sample.java", including the
prepared by: Melkamu D. 10/23/2024 27
quotation marks. Then the dialog box should look like this:
CONTD…

Now click Save, and exit Notepad.


prepared by: Melkamu D. 10/23/2024 28
EXECUTING THE SOURCE FILE
• To Compile the Sample.java program go to DOS prompt. We can do this from the Start
menu by choosing Run... and then entering cmd. The window should look similar to
the following figure.

• The prompt shows current directory. To compile Sample.java source file, change current
directory to the directory where Sample.java file is located. For example, if source
directory is JQR on the D drive, type the following commands at the prompt and press
Enter:

• Now the prompt should change to D:\JQR>

prepared by: Melkamu D. 10/23/2024 29


CONTD…

• At the prompt, type the following command and press Enter: javac
Sample.java
• The compiler generates byte code and Sample.class will be created.
• To run the program, enter java followed by the class name created at the time of
compilation at the command prompt in the same directory as: java Sample

prepared by: Melkamu D. 10/23/2024 30

• The program interpreted and the output is displayed.


E ND
T h e
! ! !
You
a n k
prepared by: Melkamu D.

Th 10/23/2024 31

You might also like