Object Oriented Programming Java
Object Oriented Programming Java
IACSD
Object Oriented Programming with
JAVA
1
E-DAC Object Oriented Programming with JAVA IACSD
Object Oriented
Everything in Java is coded using OO principles. This facilitates code modularization, reusability,
testability, and performance.
Interpreted/Portable
Java source is compiled into platform-independent bytecode, which is then interpreted (compiled
into native-code) at runtime. Java code is "Write Once, Run Everywhere"
Simple
Java has a familiar syntax, automatic memory management, exception handling, single inheritance,
standardized documentation, and a very rich set of libraries .
Secure/Robust
Due to its support for strong type checking, exception handling, and memory management, Java is
immune to buffer- overruns, leaked memory, illegal data access. Additionally, Java comes with a Security
Manager too.
Scalable
Java is scalable both in terms of performance/throughput, and as a development environment. A
single user can play a Java game on a mobile phone, or millions of users can shop though a Java-based e-
commerce enterprise application.
High-performance/Multi-threaded
With its Just-in-Time compiler, Java can achieve (or exceed) performance of native applications. Java
supports multi-threaded development out-of-the-box.
Dynamic
Java can load application components at run-time even if it knows nothing about them. Each class
has a run-time representation.
Distributed
Java comes with support for networking, as well as for invoking methods on remote (distributed)
objects through RMI.
About JVM,JRE,JDK
Java Development Kit [JDK] is the core component of Java Environment and provides all the tools,
executable and binaries required to compile, debug and execute a Java Program. JDK is a platform specific
software and that’s why we have separate installers for Windows, Mac and Unix systems.
Java Virtual Machine[JVM] is the heart of java programming language. When we run a program, JVM
is responsible to converting Byte code to the machine specific code. JVM is also platform dependent and
provides core java functions like memory management, garbage collection, security etc.
Java Runtime Environment [JRE] is the implementation of JVM, it provides platform to execute java
programs. JRE consists of JVM and java binaries and other class libraries to execute any program successfully.
To execute any java program, JRE is required.
2
E-DAC Object Oriented Programming with JAVA IACSD
JVM architecture with journey of java program from source code to execution stage
As shown in the below architecture diagram, JVM subsystems are :
Class Loader Subsystem
Runtime Data Area
Execution Engine
1. Boot Strap Class Loader – Responsible for loading classes from the bootstrap classpath, nothing but
rt.jar. Highest priority will be given to this loader.
2. Extension Class Loader – Responsible for loading classes which are inside the ext folder (jre\lib).
3. Application Class Loader –Responsible for loading Application Level Class path, path mentioned
Environment Variable etc.
3
E-DAC Object Oriented Programming with JAVA IACSD
Execution Engine
The byte code which is assigned to the Runtime Data Area will be executed by the Execution Engine. The
Execution Engine reads the byte code and executes it piece by piece.
Interpreter – The interpreter interprets the byte code faster, but executes slowly. The disadvantage
of the interpreter is that when one method is called multiple times, every time a new interpretation
is required.
JIT Compiler – The JIT Compiler neutralizes the disadvantage of the interpreter. The Execution Engine
will be using the help of the interpreter in converting byte code, but when it finds repeated code it
uses the JIT compiler, which compiles the entire bytecode and changes it to native code. This native
code will be used directly for repeated method calls, which improve the performance of the system.
Intermediate Code generator – Produces intermediate code
Code Optimizer – Responsible for optimizing the intermediate code generated above
Target Code Generator – Responsible for Generating Machine Code or Native Code
Profiler – A special component, responsible for finding hotspots, i.e. whether the method is called
multiple times or not.
Java Native Interface (JNI): JNI will be interacting with the Native Method Libraries and provides the Native
Libraries required for the Execution Engine.
Native Method Libraries: This is a collection of the Native Libraries which is required for the Execution Engine.
4
E-DAC Object Oriented Programming with JAVA IACSD
Bytecode
Bytecode is in a compiled Java programming language [by javac command] format and has the .class
extension executed by Java Virtual Machine (JVM). The Java bytecode gets processed by the Java virtual
machine (JVM) instead of the processor. The JVM transforms program code into readable machine language
for the CPU because platforms utilize different code interpretation techniques. A JVM converts bytecode for
platform interoperability, but bytecode is not platform-specific. JVM is responsible for processing & running
the bytecode.
JIT
The magic of java "Write once, run everywhere" is bytecode. JIT improves the performance of Java
applications by compiling bytecode to native machine code at run time. JIT is activated when a Java method
is called. The JIT compiler compiles the bytecode of that method into native machine code, compiling it "just
in time" to run. When a method has been compiled, the JVM calls the compiled code of that method directly
instead of interpreting it.
Typical compilers take source code and completely convert it into machine code, JITs take the same
source code and convert it into an intermediary “assembly language,” which can then be pulled from when
it’s needed. And that’s the key. Assembly code is interpreted into machine code on call—resulting in a faster
translation of only the code that you need. JIT have access to dynamic runtime information and are able to
optimize code. JITs monitor and optimize while they run by finding code more often called to make them run
better in the future.
JITs reduce the CPU’s workload by not compiling everything all at once, but also because the resulting
compiled code is optimized for that particular CPU. It’s why languages with JIT compilers are able to be so
“portable” and run on any platform or OS.
Platform independence
Java is a platform independent programming language, because your source code can be executed
on any platform [e.g. Windows, Mac or Linux etc..]. When you install JDK software on your system , JVM is
automatically installed on your system. When we compile Java code then .class file or bytecode is generated
by javac compiler. For every operating system separate JVM is available which is capable to read the .class
file or byte code and execute it by converting to native code for that specific machine. We compile code once
and run everywhere.
5
E-DAC Object Oriented Programming with JAVA IACSD
Language Fundamentals
Sample Java Program
6
E-DAC Object Oriented Programming with JAVA IACSD
7
E-DAC Object Oriented Programming with JAVA IACSD
Variables
Variable is nothing but identification of memory location.
Types of variables
Local
Variables declared inside method are local variable they have scope only within that methods.
Instance
Instance variables are declared inside class but used outside method.
Static
Static variable are same as that of instance variable but having keyword static.
They are also called as class variable because they are specified and can be access either class name
or object name.
8
E-DAC Object Oriented Programming with JAVA IACSD
Operators in Java
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation (Remainder after division)
9
E-DAC Object Oriented Programming with JAVA IACSD
Operator Meaning
+ Unary plus: not necessary to use since numbers
are positive without using it
- Unary minus: inverts the sign of an expression
++ Increment operator: increments value by 1
-- Decrement operator: decrements value by 1
! Logical complement operator: inverts the value
of a boolean
Here,
If the Expression is true, expression1 is assigned to the variable.
If the Expression is false, expression2 is assigned to the variable.
Java Arrays
An array is a container object that holds a fixed number of values of a single type. The length of an
array is established when the array is created. After creation, its length is fixed.
Each item in an array is called an element, and each element is accessed by its numerical index. As
shown in the preceding illustration, numbering begins with 0. The 9th element, for example, would
therefore be accessed at index 8.In Java, array is an object of a dynamically generated class. Java
array inherits the Object class.
10
E-DAC Object Oriented Programming with JAVA IACSD
Copying Arrays
The System class has an arraycopy method that you can use to efficiently copy data from one array
into another:
public static void arraycopy(Object src, int srcPos,
Object dest, int destPos, int length)
The two Object arguments specify the array to copy from and the array to copy to. The
three int arguments specify the starting position in the source array, the starting position in the destination
array, and the number of array elements to copy.
11
E-DAC Object Oriented Programming with JAVA IACSD
Objects in Java
Because an object is an instance of a class, you will often see the two words object and instance used
interchangeably.
To create an object (instance) of a particular class, use the new operator, followed by an invocation
of a constructor for that class, such as:
new Box();
The constructor method initializes the state of the new object.
The new operator returns a reference to the newly created object.
As with primitives, the variable type must be compatible with the value type when using object
references, as in:
Box b1 = new Box();
To access member data or methods of an object, use the dot (.)
notation: variable.field or variable.method().
12
E-DAC Object Oriented Programming with JAVA IACSD
Subject to the declared access mode, accessible from outside the class using the same syntax
Instance or data members
Unique to each instance (object) of that class (that is, each object has its own set of instance fields)
Accessible within instance methods and constructors using this.fieldName
The this qualifier is optional, unless a local variable of the same name exists in that scope.
Subject to the declared access mode, accessible from outside the class from an object reference
using objectRef.fieldName
Constructors
Constructors are like special methods that are called implicitly as soon as an object is instantiated
(i.e. on new ClassName()).
Constructors have no return type (not even void).
The constructor name must match the class name.
If you don’t define an explicit constructor, Java assumes a default constructor
The default constructor accepts no arguments.
The default constructor automatically invokes its base class constructor with no arguments,
as discussed later in this module.
You can provide one or more explicit constructors to:
Simplify object initialization (one line of code to create and initialize the object)
Enforce the state of objects (require parameters in the constructor)
Invoke the base class constructor with arguments, as discussed later in this module.
Adding any explicit constructor disables the implicit (no argument) constructor.
As with methods, constructors can be overloaded.
Each constructor must have a unique signature.
The parameter type list must be different, either different number or different order.
Only parameter types determine the signature, not parameter names.
One constructor can invoke another by invoking this(param1, param2, …) as the first line of its
implementation.
13
E-DAC Object Oriented Programming with JAVA IACSD
Java Packages
A package is simply a container that groups related types (Java classes, interfaces, enumerations, and
annotations). For example, in core Java, the System class belongs to the java.lang package. The
package contains all the related types that are needed for the basic java development.
14
E-DAC Object Oriented Programming with JAVA IACSD
Built-in Package
Built-in packages are existing java packages that come along with the JDK. For example, java.lang,
java.util, java.io
User-defined Package
Java also allows you to create packages as per your need. These packages are called user-defined
packages.
package packageName;
Java uses file system directories to store packages.
For example:
└── com
└── test
└── TestPlanet.java
Now, edit Test.java file, and at the beginning of the file, write the package statement as:
package com.test;
Here, any class that is declared within the test directory belongs to the com.test package.
Java has an import statement that allows you to import an entire package, or use only certain classes
and interfaces defined in the package.
15
E-DAC Object Oriented Programming with JAVA IACSD
always use sqrt() method of Math class by using Math class i.e. Math.sqrt(), but by using static import
we can access sqrt() method directly.
Once we made object eligible for garbage collection, it may not destroy immediately by the garbage
collector. Whenever JVM runs the Garbage Collector program, then only the object will be destroyed.
But when JVM runs Garbage Collector, we can not expect.
We can also request JVM to run Garbage Collector. There are two ways to do it :
Using System.gc() method : System class contain static method gc() for requesting JVM to
run Garbage Collector.
Using Runtime.getRuntime().gc() method : Runtime class allows the application to interface
with the JVM in which the application is running. Hence by using its gc() method, we can
request JVM to run Garbage Collector.
finalize( ) Method
Sometimes an object will need to perform some action when it is destroyed. For example, if
an object is holding some non-Java resource such as a file handle or character font, then you
might want to make sure these resources are freed before an object is destroyed. To handle such
situations, Java provides a mechanism called finalization. By using finalization, you can define specific
actions that will occur when an object is just about to be reclaimed by the garbage collector.
To add a finalizer to a class, you simply define the finalize( ) method. The Java run time calls that
method whenever it is about to recycle an object of that class.
16
E-DAC Object Oriented Programming with JAVA IACSD
Inheritance
Inheritance is one of the cornerstones of object-oriented programming because it allows the creation
of hierarchical classifications. Using inheritance, you can create a general class that defines traits
common to a set of related items. This class can then be inherited by other, more specific classes,
each adding those things that are unique to it. In the terminology of Java, a class that is inherited is
called a superclass. The class that does the inheriting is called a subclass. Therefore, a subclass is a
specialized version of a superclass. It inherits all of the instance variables and methods defined by
the superclass and adds its own, unique elements.
Inheritance allows you to define a class based on the definition of another class.
The class it inherits from is called a base class or a parent class.
The derived class is called a subclass or child class.
Subject to any access modifiers, which we’ll discuss later, the subclass gets access to the fields and
methods defined by the base class.
The subclass can add its own set of fields and methods to the set it inherits from its parent.
Inheritance simplifies modeling of real-world hierarchies through generalization of common
features.
Common features and functionality is implemented in the base classes, facilitating code
reuse.
Subclasses can extended, specialize, and override base class functionality.
Inheritance provides a means to create specializations of existing classes. This is referred to as sub-
typing. Each subclass provides specialized state and/or behavior in addition to the state and behavior
inherited by the parent class. For example, a manager is also an employee but it has a responsibility
over a department, whereas a generic employee does not.
Inheritance in Java
You define a subclass in Java using the extends keyword followed by the base class name. For
example:
class Manager extends Employee
{
// mgr class members
}
In Java, a class can extend at most one base class. That is, multiple inheritance is not
supported.
If you don’t explicitly extend a base class, the class inherits from Java’s Object class, discussed
later in this module.
Java supports multiple levels of inheritance.
For example, Child can extend Parent, which in turn extends Grandparent, and so on.
17
E-DAC Object Oriented Programming with JAVA IACSD
To successfully override the base class method definition, the subclass method must have
the same signature.
If the subclass defines a method with the same name as one in the base class but a different
signature, the method is overloaded not overridden.
A subclass can explicitly invoked an ancestor class’s implementation of a method by
prefixing super. to the method call.
‘’super” Keyword
Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the
keyword super. super has two general forms.
To call the superclass constructor.
A subclass can call a constructor defined by its superclass by use of the following form of super:
super(arg-list);
Here, arg-list specifies any arguments needed by the constructor in the superclass. super( )
must always be the first statement executed inside a subclass’ constructor.
To access a member of the superclass that has been hidden by a member of a subclass.
The usage has the following general form:
super.member
Here, member can be either a method or an instance variable.
This second form of super is most applicable to situations in which member names of a subclass hide
members by the same name in the superclass.
Polymorphism
Polymorphism is the ability for an object of one type to be treated as though it were another type.
In Java, inheritance provides us one kind of polymorphism.
An object of a subclass can be treated as though it were an object of its parent class, or any
of its ancestor classes. This is also known as upcasting.
For example, if Manager is a subclass of Employee:
Employee e = new Manager(...);
Why is polymorphism useful? It allows us to create more generalized programs that can be extended
more easily.
18
E-DAC Object Oriented Programming with JAVA IACSD
For example, if Employee is a class with method calSalary which is abstract then it is declared
as,
public abstract class Employee{
abstract double calSalary();
}
“final” Keyword
The keyword final has three uses.
Using final to declare Constants
It is used to create the equivalent of a named constant. A variable can be declared as final. Doing so
prevents its contents from being modified. This means that you must initialize a final variable when it is
declared. For example:
e.g. final int SIZE= 100;
Using final to Prevent Overriding
To disallow a method from being overridden, specify final as a modifier at the start of its declaration.
Methods declared as final cannot be overridden.
Using final to Prevent Inheritance
If we want to prevent a class from being inherited precede the class declaration with final keyword.
Declaring a class as final implicitly declares all of its methods as final, too.
19
E-DAC Object Oriented Programming with JAVA IACSD
Interfaces
Interfaces are used for Role Based Inheritance & Programming by Contract. When we want a
common role/ behavior to be implemented by multiple classes in different hierarchies we use
interfaces.
Also for creating a contract as the class that implements an interface must implement all the
methods declared in the interface. In the Java programming language, an interface is a reference
type, similar to a class, which can contain only constants, method signatures, default methods, static
methods, and nested types.
Method bodies exist only for default methods and static methods. Interfaces cannot be
instantiated— they can only be implemented by classes or extended by other interfaces.
An interface defines a set of methods, without actually defining their implementation.
A class can then implement the interface, providing actual definitions for the interface
methods.
In essence, an interface serves as a “contract” defining a set of capabilities through method
signatures and return types.
By implementing the interface, a class “advertises” that it provides the functionality required
by the interface, and agrees to follow that contract for interaction.
Defining a Java Interface
Use the interface keyword to define an interface in Java.
The interface definition consists of public abstract method declarations. For example:
public interface Shape {
double getArea();
double getPerimeter();
}
All methods declared by an interface are implicitly public abstract methods.
If required any data member must be declared and initialized by the interface are also public static
&final.
Implementing a Java Interface
You define a class that implements a Java interface using the implements keyword followed by the
interface name. For example:
class Circle implements Shape { // ... }
A concrete class must then provide implementations for all methods declared by the interface .
Omitting any method declared by the interface, or not following the same method signatures
and return types, results in a compilation error.
An abstract class can omit implementing some or all of the methods required by an interface.
In that case concrete subclasses of that base class must implement the methods.
A Java class can implement as many interfaces as needed.
Simply provide a comma-separated list of interface names following
the implements keyword. For example:
class ColorCircle implements Shape, Color { // ... }
A Java class can extend a base class and implement one or more interfaces.
In the declaration, provide the extends keyword and the base class name, followed by
the implements keywords and the interface name(s).
20
E-DAC Object Oriented Programming with JAVA IACSD
21
E-DAC Object Oriented Programming with JAVA IACSD
Exception Handling
Exceptions are events that occur during the execution of programs that disrupt the normal flow of
instructions (e.g. divide by zero, array access out of bound, etc.).
In Java, an exception is an object that wraps an error event that occurred within a method and
contains:
Information about the error including its type
The state of the program when the error occurred
Optionally, other custom information
Exception objects can be thrown and caught.
Exceptions are used to indicate many different types of error conditions.
JVM Errors:
OutOfMemoryError
StackOverflowError
LinkageError
All exceptions and errors extend from a common java.lang.Throwable parent class. Only Throwable
objects can be thrown and caught.
Exception Hierarchy
22
E-DAC Object Oriented Programming with JAVA IACSD
Handling Exceptions
The try-catch-finally structure is used to handle exceptions.
try {
// Code bock
}
catch (ExceptionType1 e1) {
// Handle ExceptionType1 exceptions
}
catch (ExceptionType2 e2) {
// Handle ExceptionType2 exceptions
}
// ...
finally {
// Code always executed after the
// try and any catch block
}
Both catch and finally blocks are optional, but at least one must follow a try.
The try-catch-finally structure can be nested in try, catch, or finally blocks.
The finally block is used to clean up resources, particularly in the context of I/O.
If you omit the catch block, the finally block is executed before the exception is propagated.
Exceptions can be caught at any level.
If they are not caught, they are said to propagate to the next method.
Custom or User Defined Exception
If you wish to define your own exception:
Create an Exception class.
Decide if the exception should be checked or unchecked.
Checked extends Exception
Unchecked extends RuntimeException
Define constructor(s) that call into super’s constructor(s), taking message and/or cause
parameters.
23
E-DAC Object Oriented Programming with JAVA IACSD
24
E-DAC Object Oriented Programming with JAVA IACSD
Collection Hierarchy
25
IACSD Concepts of Programming
26
IACSD Concepts of Programming
Threading in Java
When you want application to provide the most responsive interaction with the user, even if the
application is currently doing other work we use multithreading.
Using multiple threads of execution is one of the most powerful ways to keep your application
responsive to the user and at the same time make use of the processor in between or even during
user events.
27
IACSD Concepts of Programming
One Major disadvantage of Java synchronized keyword is that it doesn't allow concurrent
read, which can potentially limit scalability.
One more limitation of java synchronized keyword is that it can only be used to control access to a
shared object within the same JVM. If you have more than one JVM and need to synchronize access
to a shared file system or database, the Java synchronized keyword is not at all sufficient. You need
to implement a kind of global lock for that.
Java synchronized keyword incurs a performance cost. A synchronized method in Java is very
slow and can degrade performance. So use synchronization in java when it absolutely requires and
consider using java synchronized block for synchronizing critical section only.
Java synchronized block is better than java synchronized method in Java because by using
synchronized block you can only lock critical section of code and avoid locking the whole method
which can possibly degrade performance.
Deadlock
In Java, a deadlock is a situation where minimum two threads are holding the lock on some different
resource, and both are waiting for other’s resource to complete its task. And, none is able to leave the lock
on the resource it is holding.
In given figure Thread-1 has A but need B to complete processing and similarly Thread-2 has resource
Bbut need A first.
The Object class in Java has three final methods that allow threads to communicate about the locked
status of a resource.
wait() : It tells the calling thread to give up the lock and go to sleep until some other thread enters
the same monitor and calls notify(). The wait() method releases the lock prior to waiting and
reacquires the lock prior to returning from the wait() method
28
IACSD Concepts of Programming
notify(): It wakes up one single thread that called wait() on the same object. It should be noted that
calling notify() does not actually give up a lock on a resource. It tells a waiting thread that that thread
can wake up. However, the lock is not actually given up until the notifier’s synchronized block has
completed.
notifyAll(): It wakes up all the threads that called wait() on the same object. The highest priority
thread will run first in most of the situation, though not guaranteed. Other things are same as
notify()method above.
29