3. Keep instance variables protected (with an
access modifier, often private).
Make public accessor methods, and force
calling code to use those methods rather
than directly accessing the instance
variable.
For the methods, use the JavaBeans naming
convention of set and get.
4. Abstraction refers to the ability to make a class abstract in
OOP. An abstract class is one that cannot be instantiated. All
other functionality of the class still exists, and its fields,
methods, and constructors are all accessed in the same manner.
You just cannot create an instance of the abstract class.
public abstract class Shape{
public abstract void abstract display();
}
public class Rectangle extends Shape{
public void display(){
System.out.print(“Rectangle”);
}
}
public class Circle extends Shape{
public void display(){
System.out.print(“Circle”);
}
}
5. Inheritance can be defined as the process
where one object acquires the properties of
another.
most commonly used keyword would
be extends and implements.
6.
7.
8. public class Maruti{
private Engine engine;
}
public class Engine{}
HAS-A relationships are based on usage,
rather than inheritance. In other words,
class A HAS-A B if code in class A has a
reference to an instance of class B.
HAS-A relationships allow you to design
classes that follow good OO practices by
not having monolithic classes that do a
gazillion different things. Classes (and
their resulting
objects) should be specialists.
9. Runtime
class Bike{
void run(){System.out.println("running");}
}
class Splender extends Bike{
void run(){
System.out.println("running safely with 60
km");
}
public static void main(String args[]){
Bike b = new Splender();//upcasting
b.run();
}
}
Polymorphism is the ability of an object to
take on many forms. The most common use
of polymorphism in OOP occurs when a parent
class reference is used to refer to a child
class object. Any Java object that can pass
more than one IS-A test is considered to be
polymorphic.
Polymorphic method invocations apply only
to instance methods. You can always refer
to an object with a more general reference
variable type (a superclass or interface),
but at runtime, the ONLY things that are
dynamically selected based on the actual
object (rather than the reference type) are
instance methods. Not static methods. Not
variables. Only overridden instance methods
are dynamically invoked based on the real
object's type.
10. Overriding
Any time you have a class that inherits a
method from a superclass, you have the
opportunity to override the method
The key benefit of overriding is the
ability to define behavior that's specific
to a particular subclass type
public class Animal { public void eat() {
System.out.println("Generic Animal Eating
Generically"); } } class Horse extends
Animal { public void eat() {
System.out.println("Horse eating hay, oats,
" + "and horse treats"); } }
11.
12.
13. Overloaded methods let you reuse the same
method name in a class, but with different
arguments (and optionally, a different
return type).
14.
15. public interface Animal{
void eat();
}
public class Dog implements Animal{
public void eat(){
System.out.println(“Dog eats Pedigree”);
}
}
public class Cat implements Animal{
public void eat(){
System.out.println(“Cat drinks milk”);
}
}
public interface Animal{
void eat();
}
public interface Mammal extends Animal{
void eat();
}
16. You can return null in a
method with an object
reference return type.
public Button doStuff() {
return null; }
An array is a perfectly
legal return type. public
String[] go() { return new
String[] {"Fred", "Barney",
"Wilma"}; }
In a method with a
primitive return type,
you can return any
value or variable that
can be implicitly
converted to the
declared return type.
public int foo() { char c
= 'c'; return c; // char is
compatible with int }
In a method with a
primitive return type,
you can return any
value or variable that
can be explicitly cast to
the declared return
type. public int foo () {
float f = 32.5f; return (int)
f; }
You must not return
anything from a method
with a void return type.
public void bar() { return
"this is it"; // Not legal!! }
In a method with an
object reference return
type, you can return any
object type that can be
implicitly cast to the
declared return type.
public Animal
getAnimal() { return new
Horse(); // Assume Horse
extends Animal }
17. Constructors and
Instantiation
Every class in java has a
constructor including the
abstract class.
Default Constructor:
public class Rectangle{
private int length;
public void
Rectangle(){}
public void
Rectangle(int length){
this.length = length;
}
}
18.
19.
20.
21.
22. Integer i1 = new Integer(42); Integer i2 = new
Integer("42");
or
Float f1 = new Float(3.14f); Float f2 = new
Float("3.14f");
In summary, the essential method signatures for Wrapper
conversion methods are
primitive xxxValue() - to convert a Wrapper to a primitive
primitive parseXxx(String) - to convert a String to a
primitive Wrapper valueOf(String) - to convert a String to a
Wrapper
23. The heap is that part of memory where
Java objects live, and it's the one
and only part of memory that is in
any way involved in the garbage
collection process.
All of garbage collection revolves
around making sure that the heap has
as much free space as possible.
When the garbage collector runs, its
purpose is to find and delete objects
that cannot be reached.
A Java program is in a constant cycle
of creating the objects it needs
(which occupy space on the heap), and
then discarding them when they're no
longer needed, creating new objects,
discarding them, and so on, the
missing piece of the puzzle is the
garbage collector. When it runs, it
looks for those discarded objects and
deletes them from memory so that the
cycle of using memory and releasing
it can continue. Ah, the great circle
of life.
24. The garbage collector is under the control of the JVM.
The JVM decides when to run the garbage collector.
From within your Java program you can ask the JVM to run the garbage collector, but
there are no guarantees, under any circumstances, that the JVM will comply.
Left to its own devices, the JVM will typically run the garbage collector when it
senses that memory is running low.
Experience indicates that when your Java program makes a request for garbage
collection, the JVM will usually grant your request in short order, but there are no
guarantees.
25. Eden Space: When an instance is created, it is first stored in the
eden space in young generation of heap memory area.
Survivor Space (S0 and S1): As part of the minor garbage collection
cycle, objects that are live (which is still referenced) are moved
to survivor space S0 from eden space. Similarly the garbage
collector scans S0 and moves the live instances to S1.
Old Generation: Old or tenured generation is the second logical part
of the heap memory. When the garbage collector does the minor GC
cycle, instances that are still live in the S1 survivor space will
be promoted to the old generation. Objects that are dereferenced in
the S1 space is marked for eviction.
Major GC: Old generation is the last phase in the instance life
cycle with respect to the Java garbage collection process. Major GC
is the garbage collection process that scans the old generation part
of the heap memory. If instances are dereferenced, then they are
marked for eviction and if not they just continue to stay in the old
generation.
Memory Fragmentation: Once the instances are deleted from the heap
memory the location becomes empty and becomes available for future
allocation of live instances. These empty spaces will be fragmented
across the memory area. For quicker allocation of the instance it
should be defragmented. Based on the choice of the garbage
collector, the reclaimed memory area will either be compacted on the
go or will be done in a separate pass of the GC.
26. The Serial GC
The serial collector is the default
for client style machines in Java SE
5 and 6. With the serial collector,
both minor and major garbage
collections are done serially (using
a single virtual CPU). In addition,
it uses a mark-compact collection
method. This method moves older
memory to the beginning of the heap
so that new memory allocations are
made into a single continuous chunk
of memory at the end of the heap.
This compacting of memory makes it
faster to allocate new chunks of
memory to the heap.
27. Switch Description
-Xms
Sets the initial heap size
for when the JVM starts.
-Xmx
Sets the maximum heap
size.
-Xmn
Sets the size of the Young
Generation.
-XX:PermSize
Sets the starting size of the
Permanent Generation.
-XX:MaxPermSize
Sets the maximum size of
the Permanent
Generation
Switch Description
28. The simplest way to ask for garbage collection
(remember—just a request) is
System.gc();
or
Runtime.getRuntime().gc()
finalize method in java
For any given object, finalize() will be
called only once (at most) by the garbage
collector.
Calling finalize() can actually result in
saving an object from deletion.