0% found this document useful (0 votes)
31 views65 pages

UNIT-1 Introduction 3

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

UNIT-1 Introduction 3

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

2CEIT302

Object Oriented Programming


Unit-1:Introduction
(2024-2025)
WHY DO WE NEED OBJECT‐ORIENTED
PROGRAMMING?

Object‐Oriented Programming was developed


because limitations were discovered in earlier
approaches to Programming.
To appreciate what OOP does, we need to
understand what these limitations are and how
they arose from traditional programming
languages.
HISTORY
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 the 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 best 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". 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.
HISTORY
Currently, Java is used in internet programming, mobile devices,
games, e-business solutions, etc. Following 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 it was designed for small, embedded systems
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.
HISTORY: WHY JAVA WAS
NAMED AS "OAK"?
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.
JAVA VERSION HISTORY
1. JDK Alpha and Beta (1995) 1. Java SE 12 (March 2019)
2. JDK 1.0 (23rd Jan 1996) 2. Java SE 13 (September
2019)
3. JDK 1.1 (19th Feb 1997)
3. Java SE 14 (Mar 2020)
4. J2SE 1.2 (8th Dec 1998)
4. Java SE 15 (September
5. J2SE 1.3 (8th May 2000) 2020)
6. J2SE 1.4 (6th Feb 2002) 5. Java SE 16 (Mar 2021)
7. J2SE 5.0 (30th Sep 2004) 6. Java SE 17 (September
8. Java SE 6 (11th Dec 2006) 2021)
9. Java SE 7 (28th July 2011) 7. Java SE 18 (to be released
by March 2022)
10. Java SE 8 (18th Mar 2014)
8. Java SE 19 (March 2023)
11. Java SE 9 (21st Sep 2017)
9. Java SE 20 (March 2023)
12. Java SE 10 (20th Mar 2018)
10. Java SE 21 (Sept 2023).
13. Java SE 11 (September
2018) 11. JAVA SE 22 (March. 2024)
SYLLABUS
Click Here
TYPES OF JAVA APPLICATIONS
1) Standalone Application
Standalone applications are also known as desktop applications or window-based
applications. These are traditional software that we need to install on every machine.
Examples of standalone application are Media player, antivirus, etc. AWT and Swing
are used in Java for creating standalone applications.
2) Web Application
An application that runs on the server side and creates a dynamic page is called a web
application. Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF etc. technologies
are used for creating web applications in Java.
3) Enterprise Application
An application that is distributed in nature, such as banking applications, etc. is called
an enterprise application. It has advantages like high-level security, load balancing, and
clustering. In Java, EJB is used for creating enterprise applications.
4) Mobile Application
An application which is created for mobile devices is called a mobile application.
Currently, Android and Java ME are used for creating mobile applications.
APPLICATIONS
There are many devices where Java is currently used. Some
of them are as follows:
1. Desktop Applications such as acrobat reader, media player,
antivirus, etc.
2. Web Applications such as irctc.co.in, javatpoint.com, etc.
3. Enterprise Applications such as banking applications.
4. Mobile
5. Embedded System
6. Smart Card
7. Robotics
8. Games, etc.
JAVA PLATFORMS/ EDITIONS
1) Java SE (Java Standard Edition)
It is a Java programming platform. It includes Java programming APIs
such as java.lang, java.io, java.net, java.util, java.sql, java.math etc. It
includes core topics like OOPs, String, Regex, Exception, Inner classes,
Multithreading, I/O Stream, Networking, AWT, Swing, Reflection,
Collection, etc.
2) Java EE (Java Enterprise Edition)
It is an enterprise platform that is mainly used to develop web and
enterprise applications. It is built on top of the Java SE platform. It
includes topics like Servlet, JSP, Web Services, EJB, JPA, etc.
3) Java ME (Java Micro Edition)
It is a micro platform that is dedicated to mobile applications.
4) JavaFX
It is used to develop rich internet applications. It uses a lightweight user
interface API.
PROCEDURE- OREINTED LANGUAGE
Each statement in the language tells the computer to do
something:- - get some input
- add these numbers
- divide by 6
- display that output
In the procedure oriented approach, large programs are divided
into smaller programs known as functions.
In POP, a program is written as a sequence of procedures or
function.
In POP, each procedure (function) contains a series of
instructions for performing a specific task.
During the program execution each procedure (function) can be
called by the other procedures.
To call a procedure (function), we have to write function name
only.
PROCEDURE- OREINTED
LANGUAGE
While we concentrate onto the development of functions, we give
very little attention to the data that are being used by various
functions.
In POP, the major emphasis is on procedure (function) and not
on the data.
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 accidental change by a
function. In a large program it is very difficult to identify what data
is used by which function.
Examples of procedural oriented programming language are
COBOL, FORTRAN, PASCAL, C programming language etc.
PROCEDURE- OREINTED LANGUAGE
Procedural program is divided into functions:
Each function has a clearly defined purpose and a clearly
defined interface to the other functions in the program.
The idea of breaking a program into functions can be further
extended by grouping a number of functions together into a
larger entity called a module.
Procedure‐Oriented Programming Division into Functions:
PROCEDURE- OREINTED
LANGUAGE:CHARACTERISTICS
Large programs are divided into smaller programs
known as functions.
Most of the functions share global data.
Data move openly around the system from function to
function.
Functions change the value of data at any time from
any place. (Functions transform data from one form to
another.)
It uses top-down approach in program design.
PROCEDURE OREINTED
LANGUAGE: DRAWBACKS
In POP, global data can be accessed & changed by any
procedure (function) so there is no data security. In case if we
want to change type of data of global data, then we also need to
resolve all functions that access the data. Due to this it may
happened that some errors will occurs
POP does not model real world problems because functions are
action oriented.
BASIC CONCEPTS OF OOPS
1. Objects
2. Classes
3. Data Abstraction
4. Data Encapsulation
5. Inheritance
6. Polymorphism
7. Dynamic binding
8. Message Passing
OBJECTS
Objects are the basic run‐time entities of an object oriented
system
They may represent a person, a place or any item that the
program must handle
Example:-
OBJECTS
When a program is executed, the objects interact by
sending messages to one another.
For e.g. if “customer” and “account” are two objects in
a program, then the Customer object may send a
message to the account object requesting for the bank
balance.
Each object contains data, and code to manipulate
the data
CLASSES
Collection of objects is called class. It is a logical entity.
A class can also be defined as a blueprint from which you can create an
individual object. Class doesn't consume any space.
Classes are user‐defined data types and it behaves like built in
types of programming language
Object contains code and data which can be made user define type
using class
Objects are variables of class
Once a class has been defined we can create any number
of objects for that class
A class is collections of objects of similar type
CLASSES

We can create object of class using following syntax


Syntax: class-name object-name
Here class name is class which is already created Object name is
any user define name. For example, if Student is class
Example:
Student ram, sham
In example ram and sham are name of objects for class Student.
We can create any number of objects for class
ENCAPSULATION
Binding (or wrapping) code and data together into a
single unit are known as encapsulation.
In simple words, “Encapsulation is a process of binding
data members (variables, properties) and member
functions (methods) into a single unit”
A java class is the example of encapsulation. Java bean is the
fully encapsulated class because all the data members are
private here.
ENCAPSULATION: EXAMPLE
Example: Medical store
Lets say you have to buy some medicines. You go to the medical
store and ask the chemist for the medicines
Only the chemist has access to the medicines in the
store based on your prescription
The chemist knows what medicines to give to you
This reduces the risk of you taking any medicine that is not
intended for you
Encapsulation In this example:-
MEDICINES -> Member
Variables
CHEMIST ->Member Methods
YOU ->External Application or piece of Code
ENCAPSULATION
Through Encapsulation,
Data is not accessible to
the outside world, and
only those functions which
are wrapped in the class
can access it
Encapsulation solves the
problem at the
implementation level

A class can specify how


accessible each of it
members (variables,
properties, and methods) is
to code outside of the class
ENCAPSULATION
So encapsulation means hiding the important features of a
class which is not been needed to be exposed outside of a
class and exposing only necessary things of a class
Here hidden part of a class acts like Encapsulation and
exposed part of a class acts like Abstraction
ABSTRACTION
Abstraction refers representation of necessary features without including more
details or explanations
Data abstraction is a programming (and design) technique that relies on the
separation of interface and implementation
When you press a key on your keyboard the character appears on the screen,
you need to know only this, but How exactly it works based on the
electronically is not needed. This is called Abstraction
Another Example when you use the remote control of your TV, you do not
bother about how pressing a key in the remote changes the channel on the TV.
You just know that pressing the +volume button will increase the volume.
INHERITANCE
The mechanism of deriving a new class from an old class is
called inheritance or derivation
The old class is known as base class while new class is known as
derived class or sub class.
The inheritance is the most powerful features of OOP
INHERITANCE : EXAMPLE
For Example:
Consider a family of three members having a mother, father
& son named Jacky.
Jacky father: tall and dark
Jacky Mother: Short and fair
Jacky is tall and fair, so he is said to have inherited the
features of his father and mother respectively.
INHERITANCE
Through effective use of inheritance, you can save lot of time
in your programming and also reduce errors Which in turn will
increase the quality of work and productivity
The different types of Inheritance are:
1. Single Inheritance
2. Hierarchical Inheritance
3. Multiple Inheritance
4. Multi Level Inheritance
POLYMORPHISM
Polymorphism is a Greek term which means ability to take more
than one form
For example, + is used to make sum of two numbers as well as it is
used to combine two strings
This is known as operator overloading because same operator may
behave differently on different instances.
POLYMORPHISM : EXAMPLE
DYNAMIC BINDING
In Dynamic binding compiler doesn’t decide the method to be called.
Overriding is a perfect example of dynamic binding. In overriding both parent and child
classes have the same method.
Dynamic binding is also called Late binding.
Binding means link between procedure call and code to be execute.
It is the process of linking of a function call to the actual code of the function at run ‐ time
For example, complier comes to know at runtime that which function of sum will be call either
with two arguments or with three arguments

Refer: https://www.javatpoint.com/static-binding-and-dynamic-binding
MESSAGE PASSING

Objects can communicate with each others by passing message same as


people passing message with each other.
Message passing in Java is like sending an object i.e. message from
one thread to another thread.
Objects can send or receive message or information.
Message passing involves the following basic steps:
Creating classes that define objects & their behaviour.
Creating objects from class definitions.
Establishing communication among objects
Concept of message passing makes it easier to talk about building systems
that directly model or simulate their real world counterparts
MESSAGE PASSING

producer will produce, the consumer will be able to consume that only. We
mostly use Queue to implement communication between threads.
BENEFITS OF OOP
Code can be reuse by using inheritance
Data can be hiding from outside world by using encapsulation
Operators or functions can be overloaded by using polymorphism,
so same functions or operators can be used for multitasking
Object oriented system can be easily upgrade from small system to
large system
It is easy to partition work for same project
Message passing techniques make communication easier
Software complexity can be easily managed
Maintenances cost is less
Simple to implement
DIFFERENCE BETWEEN OOP AND POP:
FEATURES OF JAVA
Java is simple
Java is object-oriented
Java is distributed
Java is interpreted
Java is robust
Java is portable
Java’s performance
Java is multithreaded
Java is dynamic
Java is Secure
JAVA IS SIMPLE

Derived from C(syntax) & C++(features)

Easy to write & Compile

There is no need to remove unreferenced objects because there


is an Automatic Garbage Collection in Java.
JAVA IS OBJECT ORIENTED
Java is an object-oriented programming language. Everything in
Java is an object. Object-oriented means we organize our
software as a combination of different types of objects that
incorporate both data and behavior.
Consider the Automobile Class. There may be many automobiles
with different names and brands, but they will all have some
basic characteristics, such as four wheels, a speed limit, and a
range of mileage. The class is Car, and the attributes are wheels,
speed limitations, and mileage.
PROGRAM
class Computer { Computer() { System.out.println("Constructor of
Computer class."); } void computer_method() {
System.out.println("Power gone! Shut down your PC soon..."); }
public static void main(String[] args) { Computer
my = new Computer(); Laptop your = new Laptop();
my.computer_method(); your.laptop_method(); }}
class Laptop { Laptop() { System.out.println("Constructor of Laptop
class."); } void laptop_method() { System.out.println("99% Battery
available."); }}
JAVA IS DISTRIBUTED
Java facilitates users to create distributed applications in Java.
RMI and EJB are used for creating distributed applications.
This feature of Java makes us able to access files by calling the
methods from any machine on the internet.
JAVA IS INTERPRETED
Compiling the java source code into an intermediate representation
called BYTECODE
It can run on every platform having Java Virtual Machine installed
Java Virtual Machine
Runtime Environment to execute java programs
Interprets the byte code and converts into platform specific code
Secure execution of the programs without any side effects
JAVA IS INTERPRETED
JAVA IS INTERPRETED
Stage 1: Class Loader
The main class is loaded into the memory bypassing its ‘.class’
file to the JVM, through invoking the latter. All the other classes
referenced in the program are loaded through the class loader.A
class loader, itself an object, creates a flat namespace of class
bodies that are referenced by a string name.
JAVA IS INTERPRETED
Stage 2: Bytecode Verifier
After the bytecode of a class is loaded by the class loader, it has to be
inspected by the bytecode verifier, whose job is to check that the
instructions don’t perform damaging actions. The following are some of
the checks carried out:
Variables are initialized before they are used.
Method calls match the types of object references.
Rules for accessing private data and methods are not violated.
Local variable accesses fall within the runtime stack.
The run-time stack does not overflow.
If any of the above checks fail, the verifier doesn’t allow the class to be
loaded.
JAVA IS INTERPRETED
Stage 3: Just-In-Time Compiler
This is the final stage encountered by the java program, and its
job is to convert the loaded bytecode into machine code.
The JIT compiler is enabled by default. When a method has
been compiled, the JVM calls the compiled code of that method
directly instead of interpreting it.
Example: JVM, CLR
JAVA IS PORTABLE
It facilitates you to carry the Java bytecode to any platform. It
doesn't require any implementation.
JAVA IS ROBUST
The program run effectively on multiple platforms
Strictly typed language
Code checking at compile time as well as run time
Memory management using the Garbage Collector
Automatic deallocation of memory with unused Objects
Exception Handling
Prevent program from crashing due to runtime error
JAVA IS MULTITHREADED

A thread is like a separate program, executing concurrently. We can


write Java programs that deal with many tasks at once by defining
multiple threads. The main advantage of multi-threading is that it
doesn't occupy memory for each thread. It shares a common
memory area. Threads are important for multi-media, Web
applications, etc.
JAVA IS DYNAMIC
By connecting to the Internet, a user immediately has access to
thousands of programs and other computers.
During the execution of a program, Java can dynamically load classes
that it requires either from the local hard drive, from another
computer on the local area network or from a computer somewhere
on the Internet.
Java is capable dynamically linking in new class libraries, methods, and
objects.
JAVA IS SECURE
The Java language has built-in capabilities to ensure that violations of security
do not occur.
Security manager guarantees that the doubted code or some malicious code
does not accomplish the goal of accessing some features of the platform and
API’s.
Threat or viruses and abuse of resources are everywhere. Java system not only
verifies all memory access but also ensure that no viruses are communicated
with an Applet.
Absence of pointer in java ensures that program cannot gain access to memory
locations without proper authorization.
TYPES OF JAVA PROGRAM
1. Application Program (Stand-alone application)
2. Applets Programs
JVM
JVM (Java Virtual Machine) is an abstract machine.
Called virtual machine because it doesn't physically exist. It
is a specification that provides a runtime environment in
which Java bytecode can be executed. It can also run those
programs which are written in other languages and compiled
to Java bytecode.
JVMs are available for many hardware and software
platforms. JVM, JRE, and JDK are platform dependent
because the configuration of each OS is different from each
other. However, Java is platform independent. There are
three notions of the JVM: specification, implementation,
and instance.
JVM
The JVM performs the following main tasks:
Loads code
Verifies code
Executes code
Provides runtime environment
JRE
JRE (Java Runtime Environment). It is also written as Java
RTE. The Java Runtime environment is a set of software
tools which are used for developing Java applications. It is
used to provide the runtime environment. It is the
implementation of JVM. It physically exists. It contains a
set of libraries + other files that JVM uses at runtime.
JDK : JAVA DEVELOPMENT
KIT
JDK (Java Development Kit). The Java Development Kit
(JDK) is a software development environment which is
used to develop java applications and applets. It contains
JRE + development tools.
STRUCTURE OF JAVA
PROGRAM
The requirement for Java Program
For executing any Java program, the following software or
application must be properly installed.
Install the JDK if you don't have installed it, download the JDK and
install it.
Set path of the jdk/bin directory.
Create the Java program
Compile and run the Java program
CREATING HELLO WORLD
EXAMPLE
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
File Name must be : Simple.java
PARAMETER USED IN PROGRAM

class keyword is used to declare a class in Java.


public keyword is an access modifier that represents visibility. It
means it is visible to all.
static is a keyword. If we declare any method as static, it is known
as the static method. The core advantage of the static method is that
there is no need to create an object to invoke the static method. The
main() method is executed by the JVM, so it doesn't require creating
an 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.
main represents the starting point of the program.
String[] args or String args[] is used for command line argument.
System.out.println() is used to print statement. Here, System is
a class, out is an object of the PrintStream class, println() is a method
of the PrintStream class. We will discuss the internal working of
System.out.println() statement in the coming section.
QUESTIONS:

1. By changing the sequence of the modifiers, method


prototype is not changed in Java.
static public void main(String args[])
2. The subscript notation in the Java array can be used
after type, before the variable or after the variable.
public static void main(String[] args)
public static void main(String []args)
public static void main(String args[])
3. Having a semicolon at the end of class is optional in
Java.
class A{
static public void main(String... args){
System.out.println("hello java4");
}
};

You might also like