Chapter 1 Introduction To OOP
Chapter 1 Introduction To OOP
CHAPTER 1
Introduction to Object
Oriented
Programming
OBJECTIVES
function has a clearly defined purpose & a clearly defined interface to the other
functions in the program.
CONTD…
1. Unrestricted Access
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
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
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
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
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
// 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…
Optional
Package Statement
Optional
Import Statements
Optional
Interface Statements
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
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.
• 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:
• 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
Th 10/23/2024 31