You are on page 1of 6

*Java is a free-form language:

This means you need not follow any indentation rules. You can even write an ent
ire program on one line, and still execute it successfully.
*A Java program is a free-format sequence of characters that is tokenized by the
compiler (broken into a stream of tokens) for further analysis.
*They should not begin with numbers.
- AvgTemp, count, a4, $test, _tmp --> Valid
- 2count, high-temp, Not/ok --> Not Valid
*Abstraction
Essential element of an object oriented programming.
You can manage complexity thorough an abstraction.
*Encapsulation
Mechanism that binds together the code and data it manipulates, and keeps both
safe from outside interference and misuse.
*Inheritance
Inheritance is the process by which one object acquires the properties of anoth
er object.
Thus in a class hierarchy, two classes are formed - superclass and a subclass.
*Polymorphism
Polymorphism (In Greek Poly means many and morphos means forms) is the ability
of different objects to respond to the same message in different ways.
Polymorphism helps us to design extensible software as we can add new objects t
o the design without rewriting existing procedures.
Polymorphism in Java is achieved by Overloading and Overriding.

*Class is a template (or blueprint) for an object and Object is an instance of t


he Class.
*A class defines the properties and behaviors for the objects.
*The properties are also called attributes, and are defined by fields in a class
.
*The behaviors are also known as operations, and are defined using methods in a
class.
*Characteristics of an Object:
State
- The state of an object encompasses the current values of all its attributes.
An attribute can be static or dynamic.
Behavior
- Behavior is how an object acts or reacts, in terms of its state changes and
operations performed upon it.
Identity
- Identity is that property of an object which uniquely identifies the object.

*Super is used to invoke the super class constructor, method or variable from su
b class.
*Super must be the first statement in the sub class constructor or method.
*Second form of Super is used to refer to superclass members.
*Method overloading is one of the ways in which Java implements Static Polymorph
ism.
In Java, it is possible to overload two or more methods, only if below statement
s are true: -
The methods should have different parameters.
The methods may or may not have different return type.
Methods should have same name.
All the methods should be in the same scope
*this():
It can be used to call one constructor from the other. The number of parameters,
determines which constructor should be called.
this() must be the first statement in the constructor.
Remember this() and super() can not be in the same constructor.
*Method overriding is the way in which java implements Dynamic Polymorphism.
Method in a sub class should have same name and signature (return type and argu
ments) as the method in the super class.
When an overridden method is called within the sub class, it will always refer
to the version of that method defined by the subclass. The version of the method
defined by the super class will be hidden.
Method overriding occurs only when names and type signature of two methods are
exactly identical otherwise two methods are simply overloaded not overridden.
*When, member is declared as static, it can be accessed, before any objects of t
hat class are created.
Both methods and variables can be declared as static.
main () method is static because it is called before any object exists.
*When objects of its class are declared, no copy of static variables is made. In
stead, all instances of the class share the same static variables.
*All the static variables are initialized to their default values.
*Static methods cannot be overridden,
although they can be re-declared / redefined by a subclass.
So even though static methods can sometimes appear to be overridden,
polymorphism rules do not apply.
Type of a reference variable decides the method call.
*Static Variables
Variables are declared as static essentially global variables.
When objects of its class are declared, no copy of static variables is made. In
stead, all instances of the class share the same static variables.
All the static variables are initialized to their default values.
*Static Methods
They can call only other static methods.
They must access only static data.
They cannot refer to super or this.

*The keyword final has 3 uses - final variable, final method and final class
*Final method
Final method cannot be overridden.
Final method can be overloaded.
Since final methods are not overridden, call to such methods is resolved at com
pile time (Early binding)

*Final class
Final class cannot be inherited.
Final class can be instantiated.
Declaring a class as final implicitly declares all its methods as final too.

*Native methods are also called as foreign methods.


Their implementation is not defined in Java but in another programming language
, for example, C or C++.
The Java Native Interface (JNI) is a special API that allows Java methods to in
voke native functions implemented in C.
Method should end with a semicolon. No opening-closing braces.
*Transient: Objects can be stored using serialization.
*Serialization transforms objects into an output format that is helpful for stor
ing objects.
Objects can later be retrieved in the same state as when they were serialized.

*A field can be specified as transient in the class declaration,


indicating that its value should not be saved when objects of the class are wri
tten to persistent storage.
*Volatile: During execution, compiled code might cache the values of fields for
efficiency reasons.
Since multiple threads can access the same field,
it is vital that caching is not allowed to cause inconsistencies when reading a
nd writing the value in the field.
*Packages are a named collection of classes grouped in a directory.
*Packages are a way of grouping related classes & interfaces.
*Public: 1# Same class, 2# Same Package Sub - class , 3# Same Package Non - Sub
class , 4# Different Package Sub - class , 5# Different Package Non - Sub class
*Protected: 1# Same class, 2# Same Package Sub - class , 3# Same Package Non - S
ub class , 4# Different Package Sub - class
*No Modifier: 1# Same class, 2# Same Package Sub - class , 3# Same Package Non -
Sub class
*Private: 1# Same class
*Abstract methods
have no implementation specified.
These methods are declared with a prefix abstract.
It is required that these methods must be overridden by a sub class.
Otherwise sub class must also be declared as an abstract.
*Any class that contains one or more abstract methods must also be declared abst
ract.
To declare a class abstract, abstract keyword is used before the class keyword
at the beginning of the class declaration.
Abstract class cannot be instantiated with the new operator because an abstract
class is not fully defined.
Abstract class can be inherited.
Abstract constructors or abstract static methods cannot be declared.
Abstract class can have abstract or fully implemented methods.
*It is illegal to declare a class as both final and abstract
*An Interface is essentially a collection of constants & abstract methods.
*Using an interface, you can specify what a class should do but not how it does
it.
Interfaces are similar to abstract classes but they do not have any instance va
riables.
*Variables can be defined inside an interface, which are implicitly final and st
atic.
*In an interface, all the methods must be public and abstract. (Never static, fi
nal, synchronized, native).

*Interface Variables:
public static final int x = 1; // Exactly what you get implicitly
*Order of Execution
Static variable initialization
Static initializer blocks execution (in the order of declaration if multiple bl
ocks are present)
Constructor header (super implicit / explicit)
Instance variables initialization / instance initializer blocks execution.
Rest of the code in the constructor
*Garbage Collection:
Storage for objects is allocated in a designated part of memory called the heap
. The size of the heap is finite.
Garbage collection is a process of managing the heap efficiently;
that is, reclaiming memory occupied by objects that are no longer needed and ma
king the heap memory available for new objects.
*The automatic garbage collector calls the finalize() method in an object that i
s eligible for garbage collection before actually destroying the object.
*The finalize() method is defined in the Object class.
protected void finalize() throws Throwable
* Since there is no guarantee that the garbage collector will ever run, there is
also no guarantee that the finalizer will ever be called.
*A class that is declared within another class or interface, is called a nested
class. Similarly, an interface that is declared within another class or interfac
e, is called a nested interface.
*A top-level class or a top-level interface is one that is not nested.
*There are four categories of nested classes and one of nested interfaces, defin
ed by the context these classes and interfaces are declared in:
-static member classes and interfaces
-non-static member classes |
-local classes |--> inner classes
-anonymous classes |
*They differ from non-inner classes in one important aspect:
that an instance of an nested class may be associated with an instance of the e
nclosing class.
*The instance of the enclosing class is called the immediately enclosing instanc
e.
An instance of an inner class can access the members of its immediately enclosi
ng instance by their simple name.
*Static member class / interface
A static member class or interface is defined as a static member in a class or
an interface.
Such a nested class can be instantiated like any ordinary top-level class, usin
g its full name.
No enclosing instance is required to instantiate a static member class.
Note that there are no non-static member, local, or anonymous interfaces. Inter
faces are always defined either at the top level or as static members.
*Non-static member classes
Non-static member classes are defined as instance members of other classes, jus
t like fields and instance methods are defined in a class.
An instance of a non-static member class always has an enclosing instance assoc
iated with it.
*Local Classes
Local classes can be defined in the context of a block as in a method body or a
local block, just as local variables can be defined in a method body or a local
block.
*Anonymous Classes
Anonymous classes can be defined as expressions and instantiated on the fly.
An instance of a local (or an anonymous) class has an enclosing instance associ
ated with it, if the local (or anonymous) class is declared in a non-static cont
ext
*String
String is a class in Java and not a data-type. (Note S is capital)
Most of the programming languages treat String as array of characters but in Ja
va, Strings are objects.
String objects are immutable
The String class is final It can not be extended and its methods can t be overrid
den.

*StringBuffer
Like String, StringBuffer is also final class.
It provides some additional methods for string operations, which are not presen
t in String class.
StringBuffers are mutable - they can change without creating a new object.
In StringBuffer class, equals() is not overridden; it doesn t compare values.

You might also like