0% found this document useful (0 votes)
3 views48 pages

Lecture 01 03 Overview

This document outlines an introductory course on Object Oriented Programming (OOP) using Java, covering essential concepts such as classes, objects, inheritance, and polymorphism. It includes a detailed course outline, textbook recommendations, and explanations of OOP principles, Java platform components, and the Java Development Kit (JDK). By the end of the course, students will gain foundational programming skills and be prepared for more advanced challenges.

Uploaded by

neeltrial1
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)
3 views48 pages

Lecture 01 03 Overview

This document outlines an introductory course on Object Oriented Programming (OOP) using Java, covering essential concepts such as classes, objects, inheritance, and polymorphism. It includes a detailed course outline, textbook recommendations, and explanations of OOP principles, Java platform components, and the Java Development Kit (JDK). By the end of the course, students will gain foundational programming skills and be prepared for more advanced challenges.

Uploaded by

neeltrial1
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/ 48

Object Oriented

Programming using Java

By
Prof. R. S. Yadav
Course Description
• This is an introductory course, where students learn and
practice essential programming skills using the Java
programming language. This course provides an
overview of Object Oriented
• Programming (OOP) concepts using Java/C++. It helps
to understand basic OOP concepts and assist in
applying these concepts. The principles behind OOP
discussed. It covers object-oriented principles such as
classes, objects, abstraction, composition, Inheritance,
polymorphism, and interfaces. These concepts can be
implemented in the Java language. Along the way,
many of the Java library classes are seen that can be
organized to solve a variety of problems.

2
Course Description (cont.)
The Java collection classes are studied. Additional topics
include exception handling, database connectivity with
JDBC, and multi-threading. The course is programming
intensive. By the end of this course student will able to
understand the basics of OOP and be prepared to take on
more complex challenges. A lab course is associated with it
to strengthen the concepts.
Course Outline (To be covered in 22 lectures)
• UNIT I: INTRODUCTION (04)
• The meaning of Object Orientation, object identity, Encapsulation, information hiding,
polymorphism, generosity, importance of modeling, principles of modeling, object-oriented
modeling, Introduction to UML, conceptual model of the UML, Architecture.
• UNIT II: BASICS OF JAVA OR C++
• Core Java: Introduction to Object Oriented Software development through Java. Classes and
Objects. (04)
• UNIT-III Abstraction, Inheritance, Polymorphism, Nested classes and interfaces, Exceptions,
Strings, Packages, The I/O Package.(08)
• UNIT IV:Advanced Java: Event Handling, AWT, Swing, Applets, Multi-Threading, Generic,
The collection frameworks, Introduction to socket programming, Java Database connectivity
(JDBC). (08)
Text Books
• Kathy Sierra and Bert Bates, “Head First Java”, 2 nd edition, O’Reilly
• Herbert Schildt , “Java : The Complete Reference”, 9 th edition, Oracle Press
• Cay S. Horstmann and Gary Cornell , “Core Java Volume I & II”, 10 th edition, Prentice-Hall
• Tony Gaddis, “Starting Out with Java: From Control Structures through Objects”, 6th edition,
Pearson
• David Flanagan, “Java in a Nutshell”, 5th edition, O’Reilly

4
Object-oriented programming (OOP)
• Object-oriented programming (OOP) is a computer programming model that
organizes software design around data, or objects, rather than functions and
logic. An object can be defined as a data field that has unique attributes and
behavior.
• The main OOP concepts include abstraction, encapsulation, inheritance and
polymorphism. Also, Java works with three further OOP concepts: association,
aggregation, and composition.
Why Java is OOP language?
• Java is purely an object oriented language due to the fact that everything in
java is an object, all the program codes and data resides within classes and
objects. It comes with an extensive set of classes, arranged in packages, object
model in java in simple and easy to extend
Who is the father of OOPs?
• In the 1970s, the first version of the Smalltalk programming language was
developed at Xerox PARC by Alan Kay, Dan Ingalls and Adele Goldberg.
Object-oriented programming (OOP)
• Abstraction is a process of hiding the implementation details and showing only
functionality to the user. It hides the internal details, for example, sending SMS
where you type the text and send the message.
• Encapsulation is the mechanism that binds together code and data it
manipulates, and keeps both safe from outside interface and misuse. Class,
which contains data members and methods is used to implement
Encapsulation.
• Inheritance is a mechanism where you can to derive a class from another class
for a hierarchy of classes that share a set of attributes and methods.
• Polymorphism as the ability of a message to be displayed in more than one
form. A real-life example of polymorphism is a person who at the same time
can have different characteristics. Like a man at the same time is a father, a
husband and an employee.There are two types of polymorphism as below:
Static Binding (or Compile time) Polymorphism, e.g., Method Overloading.
Dynamic Binding (or Runtime) Polymorphism, e.g., Method overriding.
Abstraction in java
• Data Abstraction may also be defined as the process of identifying only the
required characteristics of an object ignoring the irrelevant details. The
properties and behaviors of an object differentiate it from other objects of
similar type and also help in classifying/grouping the objects.
• Consider a real-life example of a man driving a car. The man only knows that
pressing the accelerators will increase the speed of a car or applying brakes
will stop the car, but he does not know how on pressing the accelerator the
speed is actually increasing, he does not know about the inner mechanism of
the car or the implementation of the accelerator, brakes, etc in the car. This is
what abstraction is.
• In java, abstraction is achieved by interfaces and abstract classes. We can
achieve 100% abstraction using interfaces.
Abstraction in java
Encapsulation Encapsulation binds together code and
data it manipulates, and keeps both safe
from outside interface and misuse. Class,
which contains data members and
methods is used to implement
Encapsulation.

9
Encapsulation

10
Inheritance
Inheritance is a mechanism to derive a class from another class for a hierarchy
of classes that share a set of attributes and methods. This is used to provide
reusability features of software/program.

11
Polymorphism
Polymorphism means "many forms", and it
occurs when we have many classes that are
related to each other by inheritance.It can be
achieved through Method
overloading/overriding.

12
Polymorphism

13
Association, Aggregation, and Composition in Java

● Association is a connection or relation between two separate classes that are set up
through their objects. Association relationship indicates how objects know each other
and how they are using each other's functionality. It can be one-to-one, one-to-many,
many-to-one and many-to-many. The association can be achieved via “has-a”
relationship and/or “is-a” relationship.
● In Java, a Has-A relationship is called composition. It is additionally utilized for code
reusability in Java. In Java, a Has-A relationship essentially implies that an example
of one class has a reference to an occasion of another class or another occurrence of a
similar class. Both share each other's object for some work and at the same time they
can exists without each others dependency or both have their own life time.
● For example, a building has a room.
● Composition is a strong kind of “has-a” relationship because the objects’ lifecycles are
tied. It means that if we destroy the owner object, its members also will be destroyed
with it. For example, if the building is destroyed the room is destroyed as well in our
previous example. But, note that doesn’t mean, that the containing object can’t exist
without any of its parts. For example, if we tear down all the rooms inside a building,
the building will still exist.
● Aggregation is also a “has-a” relationship, but, what distinguishes it from
composition, is that the life cycles of the objects are not tied. Both the entries can
survive individually which means ending one entity will not affect the other entity.
Both of them can exist independently of each other. It is known as weak association.14
15
Java Platform
Java platform components:
● Java language.
● Java Compiler.
● Java Virtual Machine (JVM).
● Java Runtime Environment (JRE).
● Java Development Kit (JDK).
Java Language:
● Java syntax is derived from C and OOPS features are derived from C++. It has its syntax,
rules, format, and programming paradigm.
The Java Compiler:
● Function is to generate Java class files from the Java source code. Java class file contains a
platform-independent Java byte code.
● JVM loads generated class files and either interprets the byte code or compiles it to
machine code using Just-in-time (JIT) compiler.
The Java Virtual Machine (JVM):
● JVM is platform-independent that converts the byte code into machine-specific code.
● JVM provides the functionality of garbage collection, memory management, security, etc.
● This platform-independence of JVM allows us to create Java programs on one machine and
execute them on another machine (WORA – Write – Once – Run – Anywhere) .
Java Platform:parts of a JVM
● The JVM is present on the RAM. When a class file it needs to be executed.
● For execution, the class file is brought on the Ram by the class loader. Here the BYTE code
is verified for any security issues. Class loader is also responsible for loading, linking and
initialization of the program to be executed.
● The JVM memory contains various components and they help in the execution of the
program by holding PC registers that contain current instruction to be executed, Heap which
holds variables, arrays, etc.
● JVM memory also contains method area and native method stack which contains method
code and native language specification respectively. JVM stack holds the instance of the
program or the current thread. If there is more than one thread than each thread will have its
own JVM stack.
● Once the program is properly linked, then it is passed on to the execution engine. The
execution engine cooperates with the native method interface and native method libraries.
Native method interface is a programming framework that allows Java code to call libraries
and native applications that are a part of the native method libraries.
● Next, the execution engine converts the Bytecode into native machine code using just-in-
time (JIT) compiler. Then the program is executed by the machine.
● Note: JIT interprets a part of the Byte Code that has similar functionality at the same time.
Java Platform:parts of a JVM
Java Platform:parts of a JVM
Class Loader: It is used to load class files.

Class method area: It is one of the data Area in JVM, in which Class data will be stored. Static

Variables, Static Blocks, Static Methods, Instance Methods are stored in this area.

Heap: A heap is created when the JVM starts up. It may increase or decrease in size while the

application runs. Heap space is used for the dynamic memory allocation of Java objects and JRE

classes at runtime. New objects are always created in heap space, and the references to these

objects are stored in stack memory. These objects have global access and we can access them

from anywhere in the application.

Stack: JVM stack is known as a thread stack. It is a data area in the JVM memory which is

created for a single execution thread. This is used to store various elements i.e.; local variables,

partial results, and data for calling method and returns.

Native stack: It subsumes all the native methods used in your application.
Java Platform:parts of a JDK
So from the above diagram, we can
conclude that,

JRE = JVM + library classes.

JDK = JRE + Developer tools.

The most popular Java developer tools for different phase of development:
● Editor or IDE. Top: (in dispute) Eclipse and IntelliJ IDEA.
● Web framework. Top: AngularJS, Spring MVC.
● Application server.
● Version control system.
● Continuous Integration.
● Database.
● Persistence API and/or Object relational mapping (ORM)
Java Platform:JRE
JRE consists of Java Virtual Machine (JVM) and binaries and other required classes.

JRE is a subset of JDK and doesn’t contain any development tools Hence if your Java
applications are developed elsewhere, and you need to only execute them on your machine,
then you can install JRE separately. You do need to install JDK for this. JRE is the minimum
requirement to run Java programs on your machine.

The JRE includes the following components.

● Code libraries, property settings, and resource files: It includes charsets.jar, rt.jar, etc.
● Dynamic Link Library (DLL) files: DDL is Microsoft's implementation of the shared
library concept. A DLL file contains code and data that can be used by multiple
programs at the same time, hence it promotes code reuse and modularization.
● Java extension files: Extensions are packages of Java™ classes that you can use to
extend the functionality of the core platform. Extensions are packaged in one or more
ZIP files or JAR files, and are loaded into the Java virtual machine by an extension
class loader. For Example, files related to locale specification like localedata.jar
● Files required for security management. For Example, java.policy, java.security
● Applet support classes.
● True type font files: Usually required by the platform.
Java Platform:JRE
The JRE includes the following components.

● The Java integration libraries comprise the Java DataSource API and five extension
libraries that provide trading, permissioning, blotter, notification, and charting
functionality. A set of template Gradle projects for working with these libraries is
available on Caplin's GitHub repository: Caplin Project Templates.
Java Platform:JRE
Java Platform:Java Development Kit (JDK)
JDK, a platform-specific software, is used for Java development as it provides the entire
executable and binaries as well as tools required to compile, debug a Java program.Thus, we will
have separate JDK installers for each Operating system.

JDK contains the following components:


● jConsole: This is a Java monitoring and management Console.
● jar: This is the archiver. This tool is used to package related class libraries into a
single Jar file as well as to manage Jar files.
● jarSigner: This tool is used for jar signing and verifying.
● javap: This is a tool for class file disassembler.
● javaws: Java web start launcher for JNLP applications.
● jhat: Java heap analysis tool.
● jrunscript: Java command-line script shell.
● jstack: Utility used to print stack traces for Java threads.
● Javadoc: This automatically generates documentation from the source code
comments.
● appletviewer: Used for applet execution and debugging without a web browser.
● apt: Annotation processing tool.
Java Platform:Java Development Kit (JDK)
JDK contains the following components:
● extCheck: Utility used to check jar file conflicts.
● keytool: Using this utility you can manipulate Keystore.
● policytool: This is a policy creation and management tool.
● xjc: This is a part of the XML binding (JAXB) API that accepts XML schema and
generates Java classes.
Java Platform:parts of a JDK
Best Open-Source Java Tools For Developers in 2022
● NetBeans. The NetBeans IDE with the Darcula theme.
● Android Studio. While you can use NetBeans for building desktop and web apps,
you have to use Android Studio if you want to code Android apps.
● JUnit.
● Maven.
● Gradle.
● Jenkins.
● SpotBugs.
● Spring.
Java Platform:flow of a Java program.
History
• James Gosling and Sun Microsystems.
• Oak.
• Java, May 20, 1995, Sun World.
• HotJava
• The first Java-enabled Web browser
• JDK Evolutions.
• J2SE, J2ME, and J2EE.

28
Characteristics of Java
Simple.
• Java is very easy to learn, and its syntax is simple, clean and easy to understand.
Object-oriented.
• Everything in java is an object, all the program codes and data resides within classes
and objects. It comes with an extensive set of classes, arranged in packages, object
model in java in sample and easy to extend.
• Java strongly supports the concepts of Object-Oriented Programming due to which it
is called a pure object-oriented language.
• Java supports major Object-Oriented programming features like Encapsulation,
Abstraction, and Inheritance.
• Almost everything in Java is an object. All programs and data live within objects and
classes. ‘Objects’ model Java rather than the ‘processes’. Java comes with an
extensible set of classes organized in packages.
• For example, we cannot develop an executable program in Java without making use of
the class. This indicates that Java very strictly applies the principle of Encapsulation.

29
Characteristics of Java
Architecture-neutral.
• Architecture neutral programming language, java allows its application to compile
on one hardware architecture and to execute on another hardware architecture.The
java uses a "binary code format" that's independent of hardware architectures,
operating system interfaces, and window systems. The format of this system-
independent binary code is architecture neutral.
• Compiled and Interpreted
• Robust.
• Secure.
• Distributed.
• Multi-threaded and Interactive.
• High Performance.
• Dynamic and Extensible.
• Portable.

30
Architecture-neutral

31
Compiled and Interpreted
● Usually, a computer language can be either compiled or interpreted. Java integrates the
power of Compiled Languages with the flexibility of Interpreted Languages.
● Java compiler (javac) compiles the java source code into the bytecode.
● Java Virtual Machine (JVM) then executes this bytecode which is executable on many
operating systems and is portable.

32
Robust

● Java is robust as it is capable of handling run-time errors, supports automatic garbage


collection and exception handling, and avoids explicit pointer concept.
● Java has a strong memory management system. It helps in eliminating errors as it
checks the code during both compile and runtime.
● Java is garbage-collected language – JVM automatically deallocates the memory blocks
and programmers do not have to worry about deleting the memory manually as in case
of C/C++.
● Java also provides the concept of exception handling which identifies runtime errors
and eliminates them.
● In Java, any runtime error encountered by the JVM is never passed directly to the
underlying system rather immediately terminates the program stopping it from causing
any harm to the underlying system.

33
Secure
Security is an important issue for any programming language as there is a threat of malicious
activities and viruses. Java supports access modifiers to check memory access and also ensures
that no viruses enter an applet.
Java is a more secure language as compared to C/C++, as it does not allow a programmer to
explicitly create pointers. Thus in Java, we can not gain access to a particular variable if we do
not initialize it properly.

Programs run in a virtual machine sandbox – A separate environment that allows users to
execute their applications without affecting the underlying system.

It has a bytecode verifier that checks the code fragments for any illegal code that violates the
access right.

34
Distributed

● Java is distributed because it encourages users to create distributed applications.


● In Java, we can split a program into many parts and store these parts on different
computers. A Java programmer sitting on a machine can access another program running
on the other machine.
● This feature in Java gives the advantage of distributed programming, which is very
helpful when we develop large projects. Java helps us to achieve this by providing the
concept of RMI (Remote Method Invocation) and EJB (Enterprise JavaBeans).
● Java comes with an extensive library of classes for interacting, using TCP/IP protocols
such as HTTP and FTP, which makes creating network connections much easier than in
C/C++.
● It also enables multiple programmers at many locations to work together on a single
project.

35
Multi-threaded and Interactive
A thread is an independent path of execution within a program, executing concurrently.
Multithreaded means handling multiple tasks simultaneously or executing multiple portions
(functions) of the same program in parallel.

The code of java is divided into smaller parts and Java executes them in a sequential and
timely manner.

Java is interactive because its code supports effective CUI (Character User Interface) and GUI
(Graphical User Interface) programs. It greatly improves the interactive performance of
graphical applications.

Advantages:

● The multithreading is used for the maximum utilization of resources is possible.


● It doesn’t occupy memory for each thread. It shares a common memory area.
● There is no need to wait for the application to finish one task before beginning another
one.
● There is a decreased cost of maintenance. Also, It is time-saving.
● It improves the performance of complex applications.
36
Multi-threaded and Interactive

37
High Performance
● The performance of Java is impressive for an interpreted language because of its
intermediate bytecode.
● Java provides high performance with the use of “JIT – Just In Time compiler”, in which
the compiler compiles the code on-demand basis, that is, it compiles only that method
which is being called. This saves time and makes it more efficient.
● Java architecture is also designed in such a way that it reduces overheads during runtime.
The inclusion of multithreading enhances the overall execution speed of Java programs.
● Bytecodes generated by the Java compiler are highly optimized, so Java Virtual Machine
can execute them much faster.

38
Dynamic and Extensible

• Java is dynamic and extensible means with the help of OOPs, we can add classes
and add new methods to classes, creating new classes through subclasses. This
makes it easier for us to expand our own classes and even modify them.
• Java gives the facility of dynamically linking new class libraries, methods, and
objects. It is highly dynamic as it can adapt to its evolving environment.
• Java even supports functions written in other languages such as C and C++ to be
written in Java programs. These functions are called “native methods”. These
methods are dynamically linked at runtime.

39
Portable

• Java is “portable” refers to the SE (Standard Edition) version. The portability


actually comes from architecture-neutrality.
• In C/C++, the source code may run slightly differently on different hardware
platforms, but Java simplifies it. You can run Java bytecode on any hardware that
has a compliant JVM which can convert the bytecode according to that particular
machine.
• In Java, the size of the primitive data types is machine-independent, which were
dependent in the case of C/C++. So, these provisions make Java programs
portable among different platforms such as Windows, Unix, Solaris, and Mac.
• Moreover, any changes and updates made in Operating Systems, Processors and
System resources will not enforce any changes in Java programs.

40
Java Applications
• Stand-alone Applications
• Just like any programming language
• Applet
• Run under a Java-Enabled Browser
• Midlet
• Run in a Java-Enabled Mobile Phone
• Servlet
• Run on a Java-Enabled Web Server
• Switchlet
• Machine learning
Java Applications

The top 10 applications of Java.


○ Mobile Applications
○ Desktop GUI Applications
○ Web-based Applications
○ Enterprise Applications
○ Scientific Applications
○ Gaming Applications
○ Big Data technologies
○ Business Applications
○ Distributed Applications
○ Cloud-based Applications

42
JDK Versions with release year
• JDK 1.02 (1995)
• JDK 1.1 (1996)
• JDK 1.2, 1998)
• Java 2 SDK v 1.3 ( 2000)
• Java 2 SDK v 1.4 (2002)
• Java SE 5 (2004).
• Java SE 6 (2006).
• Java SE 7 (2011).
• Java SE 8 (2014).
• Java SE 9 (2017).
• Java SE 10,11 (2018).
• Java SE 12,13 (2019).
• Java SE 14,15 (2020).
• Java SE 16,17 (2021).
• Java SE 18 (2022).

43
Java Processing and Execution

• Begin with Java source code in text files: Model.java


• A Java source code compiler produces Java byte code
• Outputs one file per class: Model.class
• May be standalone or part of an IDE
• A Java Virtual Machine loads and executes class files
• May compile them to native code (e.g., x86) internally
What is in a .class file?

• Create source file


• Compile source file into .class file
• Run the .class file
Java Bytecode

• Stack-based virtual machine


• Small instruction set: 202 instructions (all are 1 byte
opcode + operands)
• Memory is typed
• Every Java class file begins with magic number
3405691582 = 0xCAFEBABE
Javap examples
javac -g Test1.java javap -c Test1
public class Test1 { …
public int add(int, int);
public int add(int a, int b) { Code:
int c= a+b; 0: iload_1 // push onto stack
return c; 1: iload_2 // push onto stack
} 2: iadd // add var 1 and 2
} 3: istore_3
// store as local var 3
4: iload_3 // store onto stack
5: ireturn // return int

Javap included with Java Development Kit (JDK)


Thank you
?

You might also like