Professional Documents
Culture Documents
Object
Example/Instance of a class. It exist in real world.
How to create an object instance?
Syntax: new constructor_method
E.g. new Student()
Encapsulation
Binds together the data and functions & provide security
Achieved using POJO class (declare all variable as private & create public getter, setter
methods)
Abstraction
Hide certain details and only show the essential features of the object.
Achieved using Interface & Abstract Class
Polymorphism
Poly - Many Forms. Same name can be used for different operations
Achieved using Method Overloading & Method Overriding.
Java Bean/POJO
class {
- private member variables //class variable
- public getters() & setters() //to access private variable
Constructor
When you create a new instance of the class, Constructor will be called automatically.
This can be used to initialize values in the class
Restrictions
Abstract Class
Abstract class is class which may or may not contains abstract methods. The child extends
Abstract class, it should give define the abstract methods.
Abstract Class can have
Interface
An interface is a reference type, similar to a class & it’s used to achieve 100 % abstraction
Inheritance
Inheriting the parent properties / behaviour. Represents the IS A relationship in real world.
Achieved using extends keyword.
Possible inheritance hierarchy
When a class inherit from two or more classes, there is a chance two methods with same
signature exist in both the super classes. So compiler will face ambiguity situation on choosing
between the two method implementation. So in Java, multiple inheritance is not supported by
default.
class A {
}
class B {
}
class C extends A,B {
// Error not possible
}
How multiple inheritance indirectly achieved in Java?
In Java, multiple inheritance is indirectly implemented using Interface. So a class can implement
more than one interface at time.
If two interfaces have methods with same signature, Java will take the implementation given in
the class for both the interface methods.
Exception Mechanism
Error
Unexpected situation, occurs during program execution where further execution is impossible
Exception
Unexpected situation, occurs during program execution where further execution is possible
By default, JRE will handle the exception & exit the program execution safely. User can also
handle exception manually using try/catch/throw/finally mechanism.
Exception Types
Checked Exception
Checked exceptions are checked at compile-time.
If a method is throwing a checked exception then it should handle the exception using either
try-catch block or it should declare the exception using throws keyword.
E.g. FileNotFoundException, IOException
Unchecked Exception
Unchecked exceptions are not checked at compile-time rather they are checked at runtime.
All unchecked exception extends RuntimeException class.
E.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException
Exception Handling
Exception handling is done using try/catch/finally mechanism or using throws keyword
try - contains a block of program statements within which an exception might occur. try can
be followed by either one or more catch block or finally block or both.
catch - each catch block is an exception handler that handles the type of exception indicated
by its argument
} catch (IndexOutOfBoundsException e) {
System.out.println("IndexOutOfBoundsException: " + e.getMessage());
} catch (IOException e) {
System.out.println("Caught IOException: " + e.getMessage());
}
throws - throws keyword used to declare an exception (usually checked exception) in method
signature. throws keyword propagate the exception (if occurred) to the caller method. If we use
throws keyword, we can skip the try/catch mechanism.
E.g. void display() throws IOException {
}
throw - throw keyword is used to explicitly throw an exception.
E.g. throw new ArithmeticException()
Multi-Threading in Java
Multithreading in java is a process of executing multiple threads simultaneously. Usually used in
games.
There are two ways of creating a Thread.
By extending Thread class
class MultiClass extends Thread{
public void run(){
System.out.println("thread method started…");
}
public static void main(String args[]){
MultiClass t1=new MultiClass ();
t1.start();
String
In Java, String is an object that represents sequence of char values. String is immutable in Java.
For example:
Method 1: (Using String literal)
char[] ch={'i','t','e','c','h'};
String s=new String(ch);
Array
Array is a collection of similar data type of elements. They are stored in continuous memory
space. In Java, Array is treated as Object.
To access elements in Array, we use the index of the element. Array uses the [] bracket to
define the size of array.
Array index always starts with Zero. There are two ways to define the array.
Array defining Method 1
int a[]=new int[2];
a[0]=10;
a[1]=20;
Array defining Method 2
int a[]={33,3,4,5};
Collection
It’s just a short introduction about Java collection.
Story behind Collection
Handling large number of objects situation is not new in Java. Even when Java first introduced
we have Vector, HastTable classes to work with Collection of objects.
But on that time importance were given to multi-threading than efficiency.
So by default HastTable and Vector classes are synchronized and are thread safe. In order to
achieve thread safe they compromised in performance. Later they figured performance matters
than thread safety in Collection.
Java 1.2 and Collection rebirth
So a complete new set of classes and interfaces were introduced in Java 1.2 to redefine the way
Java deals with collection.
On top of it we have number of interfaces developed for collection.
Collection interface is the primary interface (with add, addAll, iterator methods) which declares
basic definition to work with any collection.