OOPJ2 - Module 1 - Introduction To Object Oriented Programming
OOPJ2 - Module 1 - Introduction To Object Oriented Programming
JAVA
Module Number: 01
AIM:
To provide students with the basics of object oriented programming using JAVA
2
Introduction to Introduction to Object Oriented Programming
Objectives:
3
Introduction to Introduction to Object Oriented Programming
Outcome:
4
Introduction to Introduction to Object Oriented Programming
History of JAVA
• Java was originally designed for interactive television, but it was too advanced technology for the
digital cable television industry at the time.
• The history of java starts with Green Team. Java team members (also known as Green Team),
initiated this project to develop a language for digital devices such as set-top boxes, televisions, etc.
However, it was suited for internet programming. Later, Java technology was incorporated by
Netscape.
• The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent,
Secured, High Performance, Multithreaded, Architecture Neutral, Object-Oriented, Interpreted and
Dynamic".
5
Introduction to Introduction to Object Oriented Programming
History of JAVA
1) James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in
June 1991. The small team of sun engineers called Green Team.
2) Originally designed for small, embedded systems in electronic appliances like set-top boxes.
3) Firstly, it was called "Greentalk" by James Gosling, and file extension was .gt.
4) After that, it was called Oak and was developed as a part of the Green project.
Why Java named "Oak"?
5) Why Oak? Oak is a symbol of strength and chosen as a national tree of many countries like U.S.A.,
France, Germany, Romania, etc.
6) In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies.
6
Introduction to Introduction to Object Oriented Programming
Platform Independent
Unlike other programming languages such as C, C++ etc which are compiled into platform specific
machines. Java is guaranteed to be write-once, run-anywhere language.
On compilation Java program is compiled into bytecode. This bytecode is platform independent and
can be run on any machine, plus this bytecode format also provide security. Any machine with Java
Runtime Environment can run Java Programs.
9
Introduction to Introduction to Object Oriented Programming
Fig:-Path setting
10
Introduction to Introduction to Object Oriented Programming
Step 3: Click on Environment Step 4: Now alter the path variable so that it also contains
Variables button. the path to JDK installed directory.
Fig:-Path setting
11
Introduction to Introduction to Object Oriented Programming
JVM Architecture
What is JVM?
• JVM (Java Virtual Machine) is an abstract machine that enables your computer to run a Java program.
• When you run the Java program, Java compiler first compiles your Java code to bytecode. Then, the
JVM translates bytecode into native machine code (set of instructions that a computer's CPU executes
directly).
• Java became plot form independent because of compile and interpretation only. JVM is a machine
dependent program, there are some thousands of JVMs available in the market.
What is JRE?
• JRE (Java Runtime Environment) is a software package that provides Java class libraries, along with
Java Virtual Machine (JVM), and other components to run applications written in Java programming.
JRE is the superset of JVM.
Fig:- JRE
14
Introduction to Introduction to Object Oriented Programming
What is JDK?
• JDK (Java Development Kit) is a software development kit to develop applications in Java.
When you download JDK, JRE is also downloaded, and don't need to download it separately.
In addition to JRE, JDK also contains number of development tools (compilers, JavaDoc,
Java Debugger etc).
15
Introduction to Introduction to Object Oriented Programming
What is OOP?
• Everything in OOP is grouped as self sustainable "objects". Hence, you gain reusability by means of
four main object-oriented programming concepts.
Introduction to Introduction to Object Oriented Programming
What is an Object?
• The set of activities that the object performs defines the object's behavior. For example,
the Hand (object) can grip something, or a Student(object) can give their name or address.
• Software objects also have a state and behavior. A software object's state is stored in fields and
behavior is shown via methods.
Introduction to Introduction to Object Oriented Programming
What is a Class?
• A class is nothing but a blueprint or a template for creating different objects which defines its
properties and behaviours.
• Java class objects exhibit the properties and behaviours defined by its class. A class can contain fields
and methods to describe the behaviour of an object.
Introduction to Introduction to Object Oriented Programming
Example:-
class classname
type instance-variable1;
type instance-variableN;
type methodname1(parameter-list)
// body of method
}
Introduction to Introduction to Object Oriented Programming
• Local variables: Variables defined inside methods, constructors or blocks are called local variables.
The variable will be declared and initialized within the method and the variable will be destroyed
when the method has completed.
• Instance variables: Instance variables are variables within a class but outside any method. These
variables are initialized when the class is instantiated. Instance variables can be accessed from inside
any method, constructor or blocks of that particular class.
• Class variables: Class variables are variables declared with in a class, outside any method, with the
static keyword.
Introduction to Introduction to Object Oriented Programming
Example:-
public class Dog
{
String breed;
int ageC;
String color;
void barking()
{}
void hungry()
{}
void sleeping()
{}
}
Introduction to Introduction to Object Oriented Programming
Creating an Object
• As mentioned previously, a class provides the blueprints for objects. So basically an object is created from a
class. In Java, the new key word is used to create new objects.
• Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
• Ex: Student s =new Student(“Raj”);
Introduction to Introduction to Object Oriented Programming
Creating an Object
What is OOPS?
• Object Oriented Programming is a programming concept that works on the principle that objects are
the most important part of your program. It allows users create the objects that they want and then
create methods to handle those objects. Manipulating these objects to get results is the goal of Object
Oriented Programming.
• Object Oriented Programming popularly known as OOP, is used in a modern programming language
like Java
25
Introduction to Introduction to Object Oriented Programming
Concepts of OOP
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism.
5. Association
6. Aggregation
7. composition
Introduction to Introduction to Object Oriented Programming
What is Encapsulation ?
Encapsulation is an Object Oriented Programming concept that binds together the data and functions
that manipulate the data, and that keeps both safe from outside interference and misuse. Data
encapsulation led to the important OOP concept of data hiding.
OOP supports the properties of encapsulation and data hiding through the creation of user-defined
types, called classes.
Introduction to Introduction to Object Oriented Programming
• This technique enhances a programmer’s ability to create classes with unique data sets and functions,
avoiding unnecessary penetration from other program classes.
Introduction to Introduction to Object Oriented Programming
Data Abstraction
Data abstraction refers to, providing only essential information to the outside world and hiding their
background details, i.e., to represent the needed information in program without presenting the details.
Data abstraction is a programming (and design) technique that relies on the separation of interface and
implementation.
Introduction to Introduction to Object Oriented Programming
Inheritance
• Inheritance allows us to define a class in terms of another class, which makes it easier to create and
maintain an application.
• This also provides an opportunity to reuse the code functionality and fast implementation time.
• When creating a class, instead of writing completely new data members and member functions, the
programmer can designate that the new class should inherit the members of an existing class. This
existing class is called the base class, and the new class is referred to as the derived class.
• The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-
A mammal hence dog IS-A animal as well and so on.
Introduction to Introduction to Object Oriented Programming
• A class can be derived from more than one classes, which means it can inherit data and functions
from multiple base classes. To define a derived class, we use a class derivation list to specify the base
class(es). A class derivation list names one or more base classes and has the form:
class derived-class: access-specifier base-class
Association
• Association is a relationship between two objects. It defines the diversity between objects. In this
OOP concept, all object have their separate lifecycle, and there is no owner. For example, many
students can associate with one teacher while one student can also associate with multiple teachers.
32
Introduction to Introduction to Object Oriented Programming
Aggregation
• In this technique, all objects have their separate lifecycle. However, there is ownership such that
child object can’t belong to another parent object.
For example consider class/objects department and teacher. Here, a single teacher can’t belong to
multiple departments, but even if we delete the department, the teacher object will never be destroyed.
33
Introduction to Introduction to Object Oriented Programming
Composition
• A composition is a specialized form of Aggregation. It is also called "death" relationship. Child objects
do not have their lifecycle so when parent object deletes all child object will also delete automatically.
For that, let’s take an example of House and rooms. Any house can have several rooms. One room
can’t become part of two different houses. So, if you delete the house room will also be deleted.
34
Introduction to Introduction to Object Oriented Programming
Advantages of OOPS:
• OOP offers easy to understand and a clear modular structure for programs.
• Objects created for Object-Oriented Programs can be reused in other programs. Thus it saves
significant development cost.
• Large programs are difficult to write, but if the development and designing team follow OOPS
concept then they can better design with minimum flaws.
35
Introduction to Introduction to Object Oriented Programming
Data Types
• Constants
• Variables
Introduction to Introduction to Object Oriented Programming
What is a Constant?
• System.out.println(456); // Java
• Console.writeline(456); // Visual C#
What is a variable?
• It is a named computer location in memory that holds values that might vary
• YES
• Bytes
• YES
Introduction to Introduction to Object Oriented Programming
• static is a keyword, if we declare any method as static, it is known as static method. The core
advantage of static method is that there is no need to create object to invoke the static method. The
main method is executed by the JVM, so it doesn't require to create object to invoke the main method.
So it saves memory.
• void is the return type of the method, it means it doesn't return any value.
Introduction to Introduction to Object Oriented Programming
Example of method:-
Public class First
}
Introduction to Introduction to Object Oriented Programming
Method Declaration
An opening parenthesis
A closing parenthesis
Introduction to Introduction to Object Oriented Programming
Constructors are used to initialize the object’s state. Like methods, a constructor also contains collection of
statements(i.e. instructions) that are executed at time of Object creation.
Each time an object is created using new() keyword at least one constructor (it could be default constructor)
is invoked to assign initial values to the data members of the same class.
43
Introduction to Introduction to Object Oriented Programming
Access Modifiers
There are 4 types of java access modifiers:
• private
• default
• protected
• public
Introduction to Introduction to Object Oriented Programming
46
Introduction to Introduction to Object Oriented Programming
47
Introduction to Introduction to Object Oriented Programming
//save by A.java //save by B.java
package pack; package mypack;
public class A{ import pack.*;
protected void msg()
{ class B extends A{
System.out.println("Hello"); public static void main(String args[]){
} B obj = new B();
} obj.msg();
}
}
48
Introduction to Introduction to Object Oriented Programming
49
Introduction to Introduction to Object Oriented Programming
outside package by
Access Modifier within class within package outside package
subclass only
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
50
Introduction to Introduction to Object Oriented Programming
Control Structures
• Sequential execution
• Program statements execute one after the other
• Transfer of control
• Three control statements can specify order of statements
• Sequence structure
• Selection structure
• Repetition structure
• Flowchart
• Graphical representation of algorithm
• Flowlines indicate order in which actions execute
Introduction to Introduction to Object Oriented Programming
Control Structures
Flowlines
Action Symbols
Connector Symbols
Ja va Ke ywo rd s
abstract boolean break byte case
catch char class continue default
do double else extends false
final finally float for if
implements import instanceof int interface
long native new null package
private protected public return short
static super switch synchronized this
throw throws transient true try
void volatile while
Keywords that are
reserved, but not
used, by Java
const goto
Ja va ke ywo rds.
Introduction to Introduction to Object Oriented Programming
Assignment
• Do an online study and prepare a presentation of 10-15 slides which describe the evolution of java
from C++.
• Create a table which list all the operators in java and describe its function.
• Write an array and access the array through for, while and do while loop.
54
Introduction to Introduction to Object Oriented Programming
Assignment questions:-
a) Heap
b) Stack
c) Disk
d) File
Answer:- a
55
Introduction to Introduction to Object Oriented Programming
a) JVM
b) JDK
c) JIT
d) JRE
Answer: b
Explanation: JDK is core component of Java Environment and provides all the tools, executables and
binaries required to compile, debug and execute a Java Program.
56
Introduction to Introduction to Object Oriented Programming
3. Which component is responsible for converting bytecode into machine specific code?
a) JVM
b) JDK
c) JIT
d) JRE
Answer: a
Explanation: JVM is responsible to converting bytecode to the machine specific code. JVM is also platform
dependent and provides core java functions like garbage collection,memory management, security etc.
57
Introduction to Introduction to Object Oriented Programming
Answer: a
Explanation: Java is called ‘Platform Independent Language’ as it primarily works on the principle of
‘compile once, run everywhere’.
58
Introduction to Introduction to Object Oriented Programming
a) public
b) static
c) private
d) final
Answer: c
Explanation: main method cannot be private as it is invoked by external method. Other identifier are
valid with main method.
59
Introduction to Introduction to Object Oriented Programming
6. How can we identify whether a compilation unit is class or interface from a .class file?
Answer: a
Explanation: The Java source file contains a header that declares the type of class or interface, its visibility
with respect to other classes, its name and any superclass it may extend, or interface it implements.
60
Introduction to Introduction to Object Oriented Programming
Answer: b
Explanation: Interpreters read high level language (interprets it) and execute the program. Interpreters are
normally not passing through byte-code and jit compilation.
61
Introduction to Introduction to Object Oriented Programming
a) JVM
b) JDK
c) JIT
d) JRE
Answer: d
Explanation: JRE is the implementation of JVM, it provides platform to execute java programs.
62
Introduction to Introduction to Object Oriented Programming
a) Inheritance
b) Encapsulation
c) Polymorphism
d) Compilation
Answer: d
Explanation: There are 4 OOPS concepts in Java. Inheritance, Encapsulation, Polymorphism and Abstraction.
63
Introduction to Introduction to Object Oriented Programming
Answer: a
Explanation: There are two type of polymorphism in Java. Compile time polymorphism (overloading) and
runtime polymorphism (overriding).
64
Introduction to Introduction to Object Oriented Programming
11. Which concept of Java is a way of converting real world objects in terms of class?
a) Polymorphism
b) Encapsulation
c) Abstraction
d) Inheritance
Answer: c
Explanation: Abstraction is concept of defining real world objects in terms of classes or interfaces.
65
Introduction to Introduction to Object Oriented Programming
12. What is it called if an object has its own lifecycle and there is no owner?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
Answer: d
Explanation: It is a relationship where all objects have their own lifecycle and there is no owner. This
occurs where many to many relationship is available, instead of one to one or one to many.
66
Introduction to Introduction to Object Oriented Programming
a) class
b) struct
c) int
d) none of the mentioned
Answer: a
Explanation: None.
67
Introduction to Introduction to Object Oriented Programming
Answer: a
Explanation: None
68
Introduction to Introduction to Object Oriented Programming
a) malloc
b) alloc
c) new
d) give
Answer: c
Explanation: Operator new dynamically allocates memory for an object and returns a reference to it.
This reference is address in memory of the object allocated by new.
69
Introduction to Introduction to Object Oriented Programming
Answer: a
Explanation: None.
70
Introduction to Introduction to Object Oriented Programming
Answer: c
71
Introduction to Introduction to Object Oriented Programming
Answer: b
Explanation: Constructor returns a new object with variables defined as in the class. Instance variables
are newly created and only one copy of static variables are created.
72
Introduction to Introduction to Object Oriented Programming
Video Links
https://www.youtube.com/wa
JVM and JDK tch?v=w2jlkcdvBSg Expalins the difference between JDK and JRE
73
Introduction to Introduction to Object Oriented Programming
E-References
Topics URL
JVM , JDK and JRE https://www.geeksforgeeks.org/differences-jdk-jre-jvm/
www.freejavaguide.com/history.html
History of java
74
Introduction to Introduction to Object Oriented Programming
E-Books
Topics URL
http://java.sun.com/docs/books/jls/
http://math.hws.edu/javanotes/
JAVA E-BOOK REFERENCES
http://java.sun.com/docs/books/jls/
www.iitk.ac.in/esc101/share/downloads/javanotes5.pdf
75
Introduction to Introduction to Object Oriented Programming
Summary
• Java, unlike some languages before it allows for the use of words and commands instead of just
symbols and numbers. Java also allows for the creation of advanced data types called objects which
represent real world things like a chair or a computer where you can set the attributes of these objects
and things they do.
• Java is very flexible - it can be used to develop software as well as applets (small programs that run
on webpages). But the flexibility doesn't end there because you can run the same Java programs on
various operating systems without having to rewrite the code (unlike some other languages such as C
and C++) thanks to the Java run-time environment which interprets Java code and tells the operating
system what to do.