-
Access control is a way
to limit the availability of the code to other entities of the program.
-
class modifiers: public,
final and abstract are the class modifiers.
-
public class modifier allows
the class to be accessed by any object from any package.
-
public class serves as
a template
-
a public class must be
in its own java source file
-
there can be only one public
class per java source file
-
abstract class modifier
forces the class to be extended, so that it cannot be instantiated.
-
A class can be abstract
eventhough no methods are declared as abstract.
-
A class must be declared
as abstract if any of the method is an abstract method.
Ex: Double, Integer, Long,
Float.
-
As with methods, you control
which other classes have access to a method using access levels.
-
private member: The most
restrictive access level private.
-
A private member is accessible
only to the class in which it is defined .
-
private members are like
secrets you never tell anybody .
-
These members are not inherited
by subclasses .
-
private modifier provides
no access by package or an outside package.
.protected
method: This is access level specifier is next to private.
-
This allows the class itself,
subclasses and all classes in the same package to access the members.
-
the protected access level
is appropriate for a class's subclasses to have access to the member,
but not unrelated classes.
-
protected members are like
family secrets , you don't mind if the whole family knows, and even
a few trusted friends but you don't want any outsiders to know.
.public
method: Any class in any package has access to a class's public
method.
.package
access:The package access level is what you get of you don't explicitly
set a member method access to one public , private or protected access.
*A part from the above a method
can also be declared abstract , final, static and synchronized
abstract
method: An abstract method has no implementation.
-
An abstract method must
be a member of an abstract class.
-
Defines the method signature,
return value and the exceptions it throws.
-
These methods must be overridden.
- An abstract method cannot be private, static,
final, native or synchronized.
final methods:
A final method cannot be overridden.
-
you can declare a method
final if it has an implementation that should not be changed and
it is critical to the consistency of the object.
-
final methods optimizes
code for speed.
-
if a method is declared
final it cannot be abstract.
static methods:
static methods are class methods rather than an instance method.
-
These are used to add behaviour
to a class that isn't tied to any particular instance.
-
the main method of any
java application is static.
-
the static avoids the overhead
of creating an object.
-
class methods (i.e, methods
declared as static) cannot access the instance variables declared
within the class.
-
You cannot use 'this' keyword
from a static method.
-
class methods cannot be
abstract.
-
static methods are implicitly
final, because overriding is done based on the type of object, and
static methods are attached to class, not an object.
-
A static method in a superclass
can be shadowed by another static method in a subclass, as long
as the original method was not declared final.
-
You can't override a static
method with a nonstatic method ( i.e., you can't change a static
method into an instance method in a subclass).
-
static method can only
directly access other static field.
synchronized
methods: Concurrent running threads often invoke methods
that operate on the same data. The synchronization ensure that the
threads access information in a method in a safe manner.
-
synchronized method can
only be invoked by one thread at a time.
-
synchronization applies
to class methods, instance methods or even code blocks.
-
if nonstatic no other thread
can invoke any other synchronized instance method for that object.
-
if static no other thread
can invoke any other synchronized static method for that class.
native methods:
These allow significant library of functions written in other languages
like 'c' to use in java.
*Variable
access:The private, public, protected and package access
are same as the above method access.
-
final variables: These
variables are constants i.e, their value cannot be changed.
-
static variables: These
belongs to the class.
-
There is just one copy
of static variable for all instances of the class.
-
if an instance changes
the value, the other instances see that new value.
-
static variables must be
declared in the section just after the class statement and before
a method or constructor clause.
-
A local variable cannot
be declared static.
*volatile variable: A variable
member may change asynchronously with threads .
*transient variable: instance
variables that are transient will not be serialized.
*Variables cannot be abstract,
native or synchronized.
*The following table summarizes
access modifiers for class methods and variables:
|
Modifier
|
Class
|
Method
|
Variable
|
|
final
|
Y
|
Y
|
Y
|
|
abstract
|
Y
|
Y
|
N
|
|
static
|
N
|
Y
|
Y
|
|
native
|
N
|
Y
|
N
|
|
transient
|
N
|
N
|
Y
|
|
volatile
|
N
|
N
|
Y
|
|
synchronized
|
N
|
Y
|
N
|
*The following table summarizes
the member visibility to same class and different packages:
| |
Visibility
|
|
Accessible to
|
public
|
protected
|
default
|
private
|
|
same class
|
Y
|
Y
|
Y
|
Y
|
|
class in same package
|
Y
|
Y
|
Y
|
N
|
|
subclass in different package
|
Y
|
Y
|
N
|
N
|
|
Non subclass in different package
|
Y
|
N
|
N
|
N
|
*Constructors:
These are the member functions that are called automatically when
a class is instantiated.
-
these have the same names
as their respective class.
-
these don't have a return
type.
-
If there is a return type
it is considered as a method not a constructor.
-
constructors are always
called by the new keyword.
- There can be more than one constructors
i.e., constructors can be overloaded.
-
There is a default constructor
that has no parameters.
-
Default constructor is
called when no constructor is explicitly declared.
-
constructors can't be declared
as abstract, native or final.
-
When no explicit constructor
is declared the class calls only the no args constructor of the
super class.
-
this or super keywords
are used to call other constructors.
-
this or super may be used
only as the first statement of a constructor .
**static constructors: Though
constructors can't be declared as static java provides a special syntax
to allow for static initialization.
-
use the static keyword
followed by a method body.
-
more than one static constructors
can be declared.
-
All the static constructors
are executed according to their order of appearance.
Ex : static{
// any code }
*this keyword: hidden member
fields are accessed by using this ketword.
clas testthis{
int var;
public testthis(int var){
this.var=var;
}
}
ex: this("another");
//calls a constructor with a String argument
-
classes nested within classes
are called inner classes.
-
compiler also creates a
class file for an inner class, it names the file by concatenating
name of the outer file , $, and followed by the name of the inner
class.
-
An inner class in class
scope can have any accessibility including private.
-
inner class declared local
to a block must not have any access modifier. Such a class is effectively
private to the block.
-
Inner classes defined local
to a block may not be a static.
*classes defined in methods
can be anonymous in which they must be instantiated at the same point
they are defined.
*Inner classes unless static
have an implicit reference to the enclosing instance. The enclosing
instance must be provided with the new call that constructs the inner
classes.
*Inner classes, unless static
have access to the variables of the enclosing class instance. Additionally,
inner classes defined in method scope have ready access to final variables
of the enclosing method.
*The anonymous class is instantiated
and declared in the same place.
*The declaration and instantiation
takes the form
new Xxxx () { //body}
where Xxxx is an interface
name.
*Anonymous inner classes may
implement interfaces or extend other classes.
*Anonymous inner classes cannot
have any explicit constructors. Since you do not specify a name for
the class. you cannot use that name to specify a constructor.
*Inner classes can inherit
from outer class.
*Anonymous classes are great
for event handling.
take
an exam on this topic here
|
Please
submit your ideas on this topic here
|
|
|
|
Copyrights anilbachi.8m.com
All rights reserved
for queries contact anil_kuchana@yahoo.com
|
|