Escolar Documentos
Profissional Documentos
Cultura Documentos
Pranav
Why Java?
debug
Java
Compiler
Java
Bytecode
pretty
portable
OOP Concepts
Objects physical or conceptual existence - Building
Phone conversation
Encapsulation collection of entities under one unit Tablet
Procedural
functions
modules
argument
variable
Java Architecture
Java's architecture arises out of four distinct but
interrelated technologies:
Java programming language
Java class file format
Java Application Programming Interface (API)
Java virtual machine (JVM)
are compiled.
javac
java
oriented programming:
1. Everything is an object.
2. A program is a bunch of objects telling each other what to do
by sending messages.
3. All objects of a particular type can receive the same
messages.
semicolon(;).
class ClassName
{
//Declaration of data members
//Declaration of methods
}
Object
Each object can satisfy only
certain requests.
The
Object Creation
Box mybox = new Box();
reference to object
mybox = new Box(); //
allocate a Box object
Box b1 = new Box();
Box b2 = b1;
This is called instance of a
Encapsulation
In java, the basis of encapsulation is class.
misuse.
In other words, it brings together all the variables and
methods into a single unit called class.
Objects are instance of a class
The code and data inside the class are members of the
class.
Data is members variables or instance variables, code is
member methods or just methods.
Members can be public, private or protected.
Methods
Class behavior are represented in Java by methods
Constructors
1.
a.
b.
c.
Default
Parameterized
Copy
Constructor
To initialize the object
If you define any constructors, the compiler will not insert one.
A class can have more than one constructor.
When we create an object, constructor is executed.
Example:
Class A {
A() { //constructor
//Statements
}
}
Inheritance
IS A relationship
Polymorphism
Polymorphism means many forms in Greek language
Dynamic Binding
Works at runtime
A particular object acts differently depending on the type references
passed to it
Overloading Methods
In Java, it is possible to define two or more methods
within the same class and share the same name as long
as their parameter declarations are different.
The methods are said to be overloaded, and the process
is referred to as method overloading
//Method overloading
public class MethodOverloadingDemo {
void method(){
System.out.println("First way with no parameters");
}
void method(int x){
System.out.println("Second way with one parameter "+x);
}
Abstract Classes
Often in a design, you want the base class to present only an
keyword.
You can also use the abstract keyword to describe a method that
Interface
An interface is a set of methods & constants that
100% abstraction
Class & abstract classes realize or implement
interfaces
They must have (atleast) all the methods and constants
time of signing
It is agreed upon what to implement for what?
A bank application is designed in such a way that it has to
work on computer and also on mobile devices
At that situation the projects are implemented for both
Now a sort of contract is set-up such that all the computer
based programs are designed to be executed as heavy
process and all mobile platforms programs are designed
to be executed as light weight process
Interface
member.
Access Levels
Modifier
public
protected
no modifier
private
Strings
In Java a string is a sequence of characters. But, unlike
Immutable Objects
If you are not allowed to change the value of an object, it is immutable
object.
Can use + for adding two strings
new operator is not mandatory to create immutable object
Ex., String, Integer, Float,
The string object is immutable, but the reference variable is
not.
StringBuffer
StringBuffer is a peer class of String that provides much
Exception Handling
If a runtime error has occurred the FOC abruptly stops
try block.
If an exception occurs within the try block, it is thrown, so
code can catch this exception (using catch)
To throw a specific exception from the program, use the
keyword throw.
Specifies which exception a given method can throw by a
throws clause. (duck)
Any code that absolutely must be executed (whether or
not exception has occurred) before a method returns is
put in a finally block.
Any user-defined exception class is a subclass of the
exception class
Multiple catch
Nested try
The try statement can be nested. That is, a try statement
finally
finally creates a block of code that will be executed after
Serialization
Serialization is the process of writing the state of an object
to a byte stream.
This is useful when you want to save the state of your
program to a persistent storage area, such as a file. At a
later time, you may restore these objects by using the
process of deserialization.
Serialization is also needed to implement Remote Method
Invocation (RMI).
RMI allows a Java object on one machine to invoke a
method of a Java object on a different machine.
The sending machine serializes the object and transmits
it.
The receiving machine deserializes it.
Serialization scenario
Assume that an object to be serialized has references to other
Serializable
Only an object that implements the Serializable interface
Externalizable
The Java facilities for serialization and deserialization have
Benefits of Serialization
The streaming interface to I/O in Java provides a clean
Wrappers
These are classes that encapsulate a primitive data type
within an object
There are plenty of wrapper classes
Example:
Character is a wrapper around char. To obtain a char
value in a Character object, use char charValue();
Boolean is a wrapper around boolean. To obtain a
boolean value of a Boolean object, use boolean
booleanValue()
Integer, Long, Float etc., for numerical