Chapter - 1 - Introduction To OOP
Chapter - 1 - Introduction To OOP
Chapter - 1 - Introduction To OOP
INTRODUCTION TO OBJECT
ORIENTED PROGRAMMING
1
CONTENTS
2
WHAT IS PROGRAMMING?
3
PROGRAMMING PARADIGMS
• We normally use flowcharts to organize these actions and represent the flow of
control from one action to another.
• OOP organizes a program around its data (that is, objects) and a set of well-
defined interfaces to that data.
• Therefore, OOP can help in developing much larger and complex programs
9
than structured programming.
Fig: Shows the organization of data and function in object-oriented programs.
The data of an object can be accessed only by the function (methods) associated with
that object.
However, function (method) of one object can access the function (method) of other
objects 10
S O M E O F T H E F E AT U R E S O F O B J E C T O R I E N T E D P R O G RA M M I N G A R E :
13
CLASS AND OBJECT
A class can be defined as a template/ blue print that describes the
behaviors/states and attributes that objects of that class will have.
A class encapsulates data (attributes or fields) and methods (functions)
that operate on that data.
It acts as a template from which individual objects can be created. In other
words, a class defines what an object will be and how it will behave.
A group of objects that share common properties for data part and some
program part are collectively called as class.
For example, a class that represents a bank
account might contain one method to deposit
money to an account, another to withdraw money
from an account and a third to inquire what the
account’s current balance is.
They represent real-world entities and encapsulate both data (states) and
the methods (behaviours) that operate on the data.
States: Objects
States represent the characteristics or attributes of an object.
Behavi
State
example, a dog object might have states such as colour and name.
or
Behaviours:
example, a dog object might have behaviours such as wagging, barking, and
eating. 15
Classes and Objects
In this example, , we can see that a class Person has been defined with the data members
‘unique_id’, ‘name’, ‘age’, ‘city’ and ‘gender’ and methods ‘eat()’, ‘study()’, ‘sleep()’ and
‘play()’.
Two objects of this class have been defined.
The first object has ‘name’ as ‘Parsoon’, ‘age’ as ‘25’, ‘city’ as ‘Delhi’ and ‘gender’ as 16
‘male’.
VARIABLES
Methods
A method is basically a behavior. A class can contain many
methods. It is in methods where the logics are written,
data is manipulated and all the actions are executed.
Constructors
Every class has a constructor. If we do not explicitly write a
constructor for a class the Java compiler builds a default
constructor for that class.
Each time a new object is created, at least one constructor
will be invoked. The main rule of constructors is that they
should have the same name as the class.
A class can have more than one constructor. 18
PILLARS OF OBJECT ORIENTED PROGRAMMING
There are four major pillars of Object Oriented
Programming .
These are:-
Encapsulation
Inheritance
Abstraction
Polymorphism
19
ENCAPSULATION
As per dictionary, abstraction is the quality of dealing with ideas rather than events .
In other words, the user will have the information on what the object does instead of
how it does it.
Showing only the essential parts and hiding the implementation details.
The keyword appears in the class declaration somewhere before the class keyword.
These classes cannot be used to instantiate objects, because, as we’ll soon see, abstract
classes are incomplete.
22
POLYMORPHISM
24
HISTORY OF JAVA
• Initiated by James Gosling and his team in 1991 as a project called "Oak". It was later
renamed to "Java" in 1995 as a core component of Sun Microsystems' Java platform (Java
1.0 [J2SE]).
• Multiple configurations were built to suit various types of platforms. For example, J2EE was
built for Enterprise Applications, and J2ME was built for Mobile Applications.
• Sun Microsystems has renamed the new J2 versions as Java SE (Standard Edition), Java EE
(Enterprise Edition), and Java ME (Micro Edition) respectively.
• Java is guaranteed to be Write Once, Run Anywhere (WORA). This means that code written
on one platform can be run on any platform without modification, as long as the
platform has a compatible Java Virtual Machine (JVM) installed.
• On 13 November 2006, Sun Microsystems released much of Java as free and open
25
source software under the terms of the GNU General Public License (GPL).
FEATURES OF JAVA
30
PHASE 1 EDIT(WRITING THE CODE)
In this phase, developers use a text editor or an Integrated Development
Environment (IDE) to write the Java source code.
The source code is typically saved in a file with a .java extension. This file
contains the human-readable Java code that defines the program's logic and
behaviour.
// Filename: HelloWorld.java
public class HelloWorld {
public static void main(String[]
args) {
System.out.println("Hello,
World!");
}
}
31
PHASE 2: COMPILE
• After writing the Java code, the next step is to compile it. The Java
compiler (javac command) translates the source code into an intermediate
form called bytecode which is saved in a file with a .class extension.
• Bytecode is a set of instructions that is platform-independent and can be
executed on any device with a Java Virtual Machine (JVM) installed.
• The bytecode is platform-independent, meaning it can be run on any
device with a JVM installed.
32
JAVA VIRTUAL MACHINE (JVM)
• The Java source code is compiled by the Java compiler into bytecodes
that define the tasks to be performed during the execution phase (Phase 5).
• These bytecodes are executed by the Java Virtual Machine (JVM), which is
an integral part of the JDK and serves as the foundation of the Java platform.
33
JAVA VIRTUAL MACHINE (JVM)
• The JVM, specifically, simulates the operation of a computer, while hiding the
underlying operating system and hardware from the programs that interact with it.
• As a result, Java's bytecodes are portable, and you can execute them without
having to recompile the source code.
34
To invoke the JVM, you can use the java command followed by
the name of the Java application you want to execute.
For example, if you want to run a Java application named
"Welcome," you would type:
35
PHASE 3: LOADING A PROGRAM INTO MEMORY
The JVM’s class loader takes the .class files containing the
program’s bytecodes and transfers them to primary memory.
The class loader also loads any of the .class files provided by Java
that your program uses. The .class files can be loaded from a disk
on your system or over a network
36
37
PHASE 4: BYTECODE VERIFICATION
In Phase 4, as the classes are loaded, the bytecode verifier
examines their bytecodes to ensure that they’re valid and do
not violate Java’s security restrictions .
Java enforces strong security to make sure that Java programs
arriving over the network do not damage your files or your system
(as computer viruses and worms might)
38
PHASE 5: EXECUTION
In Phase 5, the JVM executes the program’s bytecodes, thus performing the
actions specified by the program.
In early Java versions, the JVM was simply an interpreter for Java
bytecodes.
This caused most Java programs to execute slowly, because the JVM would
interpret and execute one bytecode at a time.
40
Thus Java programs actually go through two translation phases
41
MY FIRST JAVA APPLICATION
1st Line: public class HelloWorld
• Java is a true object-oriented language and therefor, Everything
must be placed inside a class
• ‘class’ is a keyword and declares a new class definition follows by
the name – HelloWorld
• The class uses an access specifier ‘public’, which indicates that
our class is accessible from other packages(packages are
collection of classes) 2nd Line: {
• Every class definition in Java
begins with Opening curly
brace ‘{‘ and ends with a
matching closing brace ‘}’
• We can place ‘{‘ next to the
first line
• E.g. public class HelloWorld 42
{
3rd Line: public static void main (String[] args)
• Indicates a method called ‘main’ in our class
• The ‘main’ method is the starting point of a Java program. It is where
JVM begins execution of the program.
• A java application can have any number of classes but only one of them
must include a main method to initiate the execution.
• “public” : is an access specifier that says the “main” method is
accessible to any object (or to all classes)
• “static” : declares this method does not belong to an instance of an a
class, rather it belongs to the class as a whole. “main” must always be
declared “static” since the interpreter uses this method before any
objects are created.
• “void” : indicates main method does not return any value
• “String[] args” : declares parameter named args which contains an
array of strings. Array of strings is passed by command line. 43
• 5th Lne: System.out.println(“Hello World!”);
• Since Java is a true Object Oriented language, every method must be
part of an object. The “println” method is a member of the out object,
which is static data member of the “System” class.
• Prints a given text to the screen.
• “println” appends a newlne to the end of string, “print” does not.
• Every Java program consists of at least one class declaration that is defined by the programmer.
• By convention all class names in Java begin with capitalized first letters of each word in a name.
• Class name can contain: digits, letters, underscore, dollar sign but do not begin with a digit.
• Java is CASE SENSETIVE!
• When we save a public class declaration in a file, the file name must be “class name” followed
by “.java” extension.
• Every Java statement must end with a semicolon.
44
COMMENTING YOUR PROGRAMS
45
A syntax error occurs when the compiler encounters code that
violates Java’s language rules (i.e., its syntax)
Every Java program consists of at least one class that you (the
programmer) define.
47
DECLARING A METHOD
public static void main( String[] args ) is the starting point of every Java
application.
The parentheses after the identifier main indicate that it’s a program building
block called a method. Java class declarations normally contain one or more
methods.
Methods perform tasks and can return information when they complete their tasks.
Keyword void indicates that this method will not return any information.
the String[] args in parentheses is a required part of the method main’s declaration
48
PERFORMING OUTPUT WITH SYSTEM.OUT.PRINTLN
50
public class EndOfChapter1 {
}
}
51
END OF CHAPTER 1
52