2. Object Oriented Programming
Object Oriented Programming (OOP) is a programming
method that based from object. Small think from OOP is
an object collection that interact with each other and
exchange an information
3. Class
In the real world, you'll often find many individual objects all
of the same kind. There may be thousands of other bicycles in
existence, all of the same make and model. Each bicycle was
built from the same set of blueprints and therefore contains the
same components. In object-oriented terms, we say that your
bicycle is an instance of the class of objects known as bicycles.
A class is the blueprint from which individual objects are
created.
Syntax : class Bicycles{
// field, constructor, and
// method declarations
}
4. Object
Real-world objects share two characteristics: They all
have state and behavior .Bicycles have state (current gear,
current pedal cadence, current speed) and behavior
(changing gear, changing pedal cadence, applying brakes).
Identifying the state and behavior for real-world objects is
a great way to begin thinking in terms of object-oriented
programming
5. Reference
A reference is an address that indicates where an object's
variables and methods are stored.
7. Garbage Collector
Garbage Collection is process of reclaiming the runtime
unused memory automatically. In other words, it is a way to
destroy the unused objects.
It makes java memory efficient because garbage collector
removes the unreferenced objects from heap memory.
It is automatically done by the garbage collector(a part of
JVM) so we don't need to make extra efforts.
9. Constructor
A class contains constructors that are invoked to create objects
from the class blueprint. Constructor declarations look like
method declarations except that they use the name of the class
and have no return type. For example, Bicycle has one
constructor:
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
To create a new Bicycle object called myBike, a constructor is
called by the new operator:
Bicycle myBike = new Bicycle(30, 0, 8);
10. You don't have to provide any constructors for your class, but you
must be careful when doing this. The compiler automatically
provides a no-argument, default constructor for any class without
constructors. This default constructor will call the no-argument
constructor of the super Class. In this situation, the compiler will
complain if the super Class doesn't have a no-argument
constructor so you must verify that it does. If your class has no
explicit super Class, then it has an implicit super Class of Object,
which does have a no-argument constructor.
11. Initialize Block
Initializer block contains the code that is always executed
whenever an instance is created. It is used to declare/initialize the
common part of various constructors of a class.
public class Prog12 {
{
System.out.println("Inside Initialization block");
}
public static void main(String[] args) {
Prog12 p = new Prog12(); //initialize block executed
Prog12 p1 = new Prog12(); //initialize block executed
}
}
12. This Keyword
Within an instance method or a constructor, this is a reference
to the current object — the object whose method or
constructor is being called. You can refer to any member of
the current object from within an instance method or a
constructor by using this.
class Prog12 {
public int x = 0;
public int y = 0;
public Prog12(int x, int y) {
x = x; // 0
this.y = y; // 2
}
Prog12 p = new Prog12(1,2);
14. Nested Class
The Java programming language allows you to define a class
within another class. Such a class is called a nested class and is
illustrated here:
class OuterClass {
...
class NestedClass {
...
}
}
A nested class is a member of its enclosing class. Non-static
nested classes (inner classes) have access to other members of the
enclosing class, even if they are declared private. Static nested
classes do not have access to other members of the enclosing
class. As a member of the OuterClass, a nested class can be
declared private, public, protected, or package private. (Recall
that outer classes can only be declared public or package private.)
15. Inner Class
As with instance methods and variables, an inner class is associated with
an instance of its enclosing class and has direct access to that object's
methods and fields. Also, because an inner class is associated with an
instance, it cannot define any static members itself.
Objects that are instances of an inner class exist within an instance of the
outer class. Consider the following classes:
class OuterClass {
...
class InnerClass {
...
}
}
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
16. Anonymous Class
Anonymous classes enable you to make your code more concise.
They enable you to declare and instantiate a class at the same
time. They are like local classes except that they do not have a
name. Use them if you need to use a local class only once.
Syntax :-
HelloWorld frenchGreeting = new HelloWorld() {
String name = "tout le monde";
public void greet() {
greetSomeone("tout le monde");
} public void greetSomeone(String someone) {
name = someone;
System.out.println("Salut " + name);
}
};
17. Abstract Class
An abstract class is a class that is declared abstract—it may or
may not include abstract methods. Abstract classes cannot be
instantiated, but they can be subclasses.
If a class includes abstract methods, then the class itself must be
declared abstract, as in:
public abstract class GraphicObject {
// declare fields
// declare non abstract methods
abstract void draw(); }
When an abstract class is subclassed, the subclass usually provides
implementations for all of the abstract methods in its parent class.
However, if it does not, then the subclass must also be
declared abstract.
18. Difference b/w Abstract Class and Interface
Abstract classes are similar to interfaces. You cannot
instantiate them, and they may contain a mix of methods
declared with or without an implementation. However, with
abstract classes, you can declare fields that are not static and
final, and define public, protected, and private concrete
methods. With interfaces, all fields are automatically public,
static, and final, and all methods that you declare or define
(as default methods) are public. In addition, you can extend
only one class, whether or not it is abstract, whereas you can
implement any number of interfaces.
19. Argument Passing
You can use any data type for a parameter of a method or a
constructor. This includes primitive data types, such as doubles,
floats, and integers and reference data types, such as objects
and arrays.
Primitive arguments, such as an int or a double, are passed into
methods by value. This means that any changes to the values of
the parameters exist only within the scope of the method. When
the method returns, the parameters are gone and any changes to
them are lost.
Reference data type parameters, such as objects, are also
passed into methods by value. This means that when the
method returns, the passed-in reference still references the
same object as before. However, the values of the object's
fields can be changed in the method, if they have the proper
access level.
20. Method Overloading
If a class has multiple methods having same name but
different in parameters, it is known as Method Overloading.
There are two ways to overload the method in java
By changing number of arguments
By changing the data type
21. Static method
Variables and methods marked static belong to the class rather than
to any particular instance of the class. These can be used without
having any instances of that class at all. Only the class is sufficient
to invoke a static method or access a static variable. A static
variable is shared by all the instances of that class. Only one copy
of the static variable is created.
A static method cannot access non-static/instance variables,
because a static method is never associated with any instance. The
same applies with the non-static methods as well, a static method
can’t directly invoke a non-static method. But static method can
access non-static methods by means of declaring instances and
using them.
static methods can’t be overridden. They can be redefined in a
subclass, but redefining and overriding aren’t the same thing. Its
called as Hiding.
22.
23. Finalize()
Called by the garbage collector on an object when garbage
collection determines that there are no more references to the
object. A subclass overrides the finalize method to dispose of
system resources or to perform other cleanup.
The Java programming language does not guarantee which
thread will invoke the finalize method for any given object.
The finalize method is never invoked more than once by a
Java virtual machine for any given object.
protected void finalize() throws Throwable
24. Native
While you can write applications entirely in Java, there are situations
where Java alone does not meet the needs of your application.
Programmers use the JNI to write Java native methods to handle those
situations when an application cannot be written entirely in Java.
The following examples illustrate when you need to use Java native
methods:
standard Java class library does not support the platform-dependent
features needed by the application.
You already have a library written in another language, and wish
to make it accessible to Java code through the JNI.
You want to implement a small portion of time-critical code in a
lower-level language such as assembly.
25. Generics
Java Generic methods and generic classes enable
programmers to specify, with a single method declaration, a
set of related methods, or with a single class declaration, a set
of related types, respectively.
Generics also provide compile-time type safety that allows
programmers to catch invalid types at compile time.
27. Generic Class
public class Box<T> {
private T t;
public void add(T t) {
this.t = t; }
public T get() {
return t; }
public static void main(String[] args) {
Box<Integer> integerBox = new Box<Integer>();
Box<String> stringBox = new Box<String>();
integerBox.add(new Integer(10));
stringBox.add(new String("Hello World"));
System.out.printf("Integer Value :%dnn", integerBox.get());
System.out.printf("String Value :%sn", stringBox.get());
}
}
28. Shallow Vs Deep Cloning
The process of creating just duplicate reference variable but
not duplicate object is called shallow cloning. Whereas the
process of creating exactly independent duplicate object is
called deep cloning.
Shallow Cloning
Test t1 = new Test();
Test t2 = t1;
Deep Cloning
Test t1 = new Test();
Test t2 = (Test)t1.clone();