Java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

CORE JAVA OOPS

Java is a high level, robust, secured and Object-Oriented Programming is a


object-oriented programming language. methodology to design a program using
It is a platform independent classes and objects. It simplifies the
software development and maintenance
JVM (Java Virtual Machine) is an by providing some concepts:
abstract machine. A specification
provides runtime environment in which  Object
java bytecode can be executed.  Class
 Inheritance
 Loads code  Polymorphism
 Verifies code  Abstraction - Encapsulation
 Executes code
 Provides runtime environment Object: An entity that has state and
behavior is known as an object
JRE is an acronym for Java Runtime
Environment. It provides runtime Class: collection of objects called
environment. It is the implementation of class. It is a logical entity
JVM. It physically exists. It contains set of
libraries + other files that JVM uses at Inheritance
runtime
When one object acquires all the
JDK is an acronym for Java properties and behaviors of parent
Development Kit. It physically exists. It object i.e. known as inheritance. It
contains JRE + development tools. provides code reusability. It is used
to achieve runtime polymorphism.
C++ vs Java
Polymorphism
Pointers. limited support
structures Doesn’t support When one task is performed by
Unions Doesn’t different ways i.e. known as
Platform dependent Independent polymorphism in java, we use
Multiple inheritance Does not method overloading and method
supports both procedural and overriding to achieve polymorphism
object-oriented programming; Pure oops
it is called a hybrid language
In java, we use method overloading
and method overriding to achieve
polymorphism

Document classification: General Business


Abstraction Constructor in java is a special type of
method that is used to initialize the
Hiding internal details and object.
complexity and showing functionality
is known as abstraction in java, we Static variable is associated with a class
use abstract class and interface to and not objects of that class.
achieve abstraction.
Static keyword is used for memory
Encapsulation management mainly. We can apply java
static keyword with variables, methods,
Binding (or wrapping) code and data blocks and nested class. The static
together into a single unit is known keyword belongs to the class than
as encapsulation. instance of the class

For example: capsule, it is wrapped This keyword in java - is a reference


with different medicines. variable that is used to refer current
class instance variable.
A java class is the example of
encapsulation. Java bean is the fully Super keyword in java - is a reference
encapsulated class because all the variable that refers to the current object.
data members are private here.
Abstract class in Java
What is difference between object-
oriented programming language and A class that is declared as abstract is
object-based programming language? known as abstract class. It needs to be
extended and its method implemented.
Object based programming language It cannot be instantiated.
follows all the features of OOPs except
Inheritance. JavaScript and VBScript are Abstract method
examples of object based programming
languages. A method that is declared as abstract
and does not have implementation is
Overloading - If a class have multiple known as abstract method. It can have
methods by same name but different abstract and non-abstract methods.
parameters, it is known as Method
Overloading. Interface are blueprint like class, but it
will have only the method declaration. It
Overriding - If subclass (child class) has will not have any method of
the same method as declared in the implementation. All the methods in the
parent class, it is known as method interface are public abstract by default.
overriding in java. It cannot be initiated.

Document classification: General Business


Interfaces are mainly used in API’s. Array - is a collection of similar type of
elements,
There can be only abstract methods in
the java interface , not method body It is a data structure where we store
similar elements. We can store only fixed
The java compiler adds public and set of elements in a java array. Array in
abstract keywords before the interface java is index based; first element of the
method and public, static and final array is stored at 0 index.
keywords before data members.
It does not grow its size at runtime. To
solve this problem, collection framework
is used in java.
Interface Abstract
Interface can have Abstract class can have String - string buffer- string builder
only abstract abstract and non-abstract
methods. methods. String String Buffer
Interface has only Abstract class can have immutable. mutable.
static and final final, non-final, static and slow and consumes is fast and consumes
variables. non-static variables. more memory when you less memory when
Interface cannot have Abstract class can have concat too many strings you cancat string
static methods, main static methods, main because every time it
method or method and constructor. creates new instance.
constructor.
The interface keyword The abstract keyword is
is used to declare used to declare abstract
interface class. String Buffer String Builder
Interface supports Abstract class does not is synchronized i.e. is non-synchronized
multiple inheritance. support multiple thread safe. i.e. not thread safe.
inheritance.
It means two threads It means two threads
can't call the methods of can call the methods
StringBuffer of StringBuilder
simultaneously. simultaneously.
is less efficient than is more efficient than
StringBuilder. StringBuffer .

Equals = checking content


== - comparing the location (memory)

Document classification: General Business


Exception Handling final finally finalize
1 is used to is used to is used to
Exception Handling is a mechanism to ) apply place perform clean
handle runtime errors such as restrictions on important up processing
ClassNotFound, IO, SQL class, method code, it will just before
and variable. be executed object is
Types of Exception Final class whether garbage
can't be exception is collected.
There are mainly two types of
inherited, final handled or
exceptions: checked and unchecked
method can't not.
where error is considered as unchecked
be overridden
exception. The sun microsystem says
and final
there are three types of exceptions:
variable value
can't be
1. CheckedException-Throwable-
changed.
Checkedexceptionsarecheckedatcom
2 Final is a Finally is a Finalize is a
pile-time.IOException, SQLException
) keyword. block. method.
2. Unchecked Exception -
RuntimeException - they are checked
Thread - is a thread of execution , Light
at runtime-ArithmeticException,
weight process . smallest unit of
NullPointerException,
processing
ArrayIndexOutOfBoundsException
3. Error- When a main method is called, jvm is
Errorisirrecoverablee.g.OutOfMemory called a thread to execute a program.
Error,VirtualMachineError
Multithreading in java is a process of
Throw Throws executing multiple threads
1 Java throw keyword Java throws keyword simultaneously. It is an idea of
) is used to explicitlyis used to declare an multitasking. there is only 4 states in
throw an exception. exception. thread life cycle in java new, runnable,
2 Throw is used Throws is used with non-runnable and terminated.
) within the method. the method
signature. New - runnable - running - waiting-
3 You cannot throw You can declare dead
) multiple exceptions. multiple exceptions We can set priorities Max min normal
e.g. public void Use the thread by implementing
method () throws runnable interface or extending thread
IOException,SQLExce class
ption.

Document classification: General Business


Synchronized
Synchronization in java is the capability
to control the access of multiple threads
to any shared resource. allow only one
thread to access the shared resource.

Wrapper: class is a class whose object


wraps or contains primitive data types

generic classes only work with objects


and don't support primitives. As a
result, if we want to work with them, we
have to convert primitive values into
wrapper objects.
For example, the Java Collection
Framework works with objects
exclusively. Long back when (prior to
Java 5, almost 15 years back) there was
no autoboxing and we, for example,
couldn't simply call add(5) on a
collection of Integers.

Serialization

Is a mechanism writing the state of an


object into byte stream. Used to travel
object's state in network
ObjectOutputStream

De-serialization
Is the process of reconstructiong the
object from the serialized state.
ObjectInputStream

Volatile
Volatile keyword in Java is used as an
indicator to Java compiler and Thread
that do not cache value of this variable
and always read it from main memory

Document classification: General Business


Collection - is a framework that ArrayList LinkedList
provides an architecture to store and 1) Internally uses Internally uses doubly
manipulate the group of objects. dynamic array to store linked list to store the
the elements. elements.
Java Collection framework provides 2) Manipulation with Manipulation with
many interfaces (Set, List, Queue) and ArrayList is slow LinkedList is faster than
classes (ArrayList, Vector, LinkedList, because it internally ArrayList because it
PriorityQueue, HashSet, LinkedHashSet, uses array. If any uses doubly linked list
TreeSet etc). element is removed so no bit shifting is
from the array, all the required in memory.
ArrayList class bits are shifted in
memory.
 uses a dynamic array for storing the
3) ArrayList class can LinkedList class can act
elements. It extends AbstractList
act as a list only as a list and queue both
class and implements List interface.
because it implements because it implements
 Can contain duplicate elements.
List only. List and Deque
 Maintains insertion order.
interfaces.
 Is non synchronized.
4) ArrayList is better LinkedList is better for
 Allows random access because array
for storing and manipulating data.
works at the index basis.
accessing data.
 Manipulation is slow because a lot of
shifting needs to be occurred if any
Difference between List and Set:
element is removed from the array
list. List can contain duplicate elements
whereas Set contains unique elements
LinkedList class
only.
 uses doubly linked list to store the
HashSet class – uses hashtable to store
elements. It extends the AbstractList
the elements.
class and implements List and
Deque interfaces. It extends AbstractSetclass and
 can contain duplicate elements. implements Set interface. • contains
 maintains insertion order. unique elements only.
 Is non synchronized.
 manipulation is fast because no LinkedHashSet class:
shifting needs to be occurred.
•contains unique elements only like
HashSet. It extends HashSet class and
implements Set interface.

• maintains insertion order.

Document classification: General Business


TreeSet TreeMap class

 contains unique elements only like  Contains values based on the key. It
HashSet. The TreeSet class implements the NavigableMap
implements NavigableSet interface interface and extends AbstractMap
that extends the SortedSet interface. class.
 maintains ascending order.  It contains only unique
elements.
Map Interface  It cannot have null key but can
have multiple null values.
A map contains values based on the key  It is same as HashMap instead
i.e. key and value pair. Map contains maintains ascending order.
only unique elements.
Hashtable class –is an array of list. Each
HashMap class – contains values based list is known as a bucket. The position of
on the key. It implements the Map bucket is identified by calling the
interface and extends AbstractMap hashcode() method.
class.
It contains values based on the key. It
 It contains only unique elements. implements the Map interface and
 It may have one null key and multiple extends Dictionary class.
null values.
 It maintains no order  It contains only unique elements.
 It may have not have any null key
LinkedHashMap class or value.
 It is synchronized.
 Contains values based on the key. It
implements the Map interface and
extends HashMap class.
 It contains only unique elements.
 It may have one null key and multiple
null values.
 It is same as HashMap instead
maintains insertion order.

Document classification: General Business


Hash Map Hash Table
Non synchronized. It is not-thread safe and cannot be Synchronized. It is thread-safe and can be shared with
shared between many threads without proper many threads.
synchronization code.
Allows one null key and multiple null values. Does not allow any null key or value.
New class introduced in JDK 1.2. legacy class.
fast. slow.
HashMap can be synchronized by calling this code is internally synchronized and can't be
unsynchronized.
Map m = Collections.synchronizedMap(hashMap);
is traversed by Iterator. is traversed by Enumerator and Iterator.
Iterator in HashMap is fail-fast. Enumerator in Hashtable is not fail- fast.
inherits AbstractMap class. inherits Dictionary class.

Array List Vector


Not synchronized. Synchronized.
Increments 50% of current array size if number of Increments 100% means doubles the array size if
element exceeds from its capacity. total number of element exceeds than its capacity.
not a legacy class, it is introduced in JDK 1.2. legacy class.
fast because it is non- synchronized. slow because it is synchronized

i.e. in multithreading environment; it will hold the


other threads in runnable or non- runnable state
until current thread releases the lock of object.
uses Iterator interface to traverse the elements. uses Enumeration interface to traverse the elements.
But it can use Iterator also.

Document classification: General Business

You might also like