The key words are
Class Basics: Below is the bare minimum requirement to create a
class and object
-
Defining a class
(default private) class Class_name //declaring class name
{
//class body starts within these two braces
//constants, method, fields,
}
public (accessor) class Entry_point //this is required
{
Class_name cn = new Class_name();
// instance of a class and object
//created with new keyword
}
-
Inheritance:
(public/private) class derived_class
extends super_class
implements custome_interface
{
//field, constructor, and method declarations
}
-
derived class is like a child class,
can inherit from only one caring parent class
(public or private) class derived_class extends
super_class implements custome_interface
{
//field, constructor, and method declarations
private class_nested
{
my_parent.my_self my_object = new my_parent.my_self my();
}
}
- Inner class:
There are two additional types of inner classes.
Local and Anonymous Inner Classes
You can declare an inner class within the body of a method.
public class class_name
{
public void a_method(){ class A_local
{ //code implementation } }
}
Such a class is known as a local inner class.
You can also declare an inner class within the body
of a method without naming it.
These classes are known as
anonymous inner classes.
class Process
{
public Process()
{ System.out.println("Process outer constructor");}
public int n2; String name="";
public void a_method(String str_method)
{
{
//contents of anonymous class
name = this.getClass().getName();
System.out.println("Received at anonymous"
+" inner class" + str_method);
}//end of anonymous class
System.out.println("Retreive class name "+
"at inner class: " + name);
} //end method_1
}//end of class process
Class accessor : private, public and abstract, if a class is not
specified, default is private.
fields : that indicates the type of data is being hold by an
object. Also know as instance variable
constructor : keeps track of the object and evoked when an
object (new instances of class using new key word) is instantiated. The
constructors are created just like other method, but it is named after
the class.
- class-name Do_something(); // declaration
- public Do_something() {
// no parameter
}
- public Do_something(String str, int n1) {
// with two parameter to
handle two arguments from a caller function };
}
method : process client's request. One method can call
another with some rules and whether these method would be accessible out
side of the class or not, is determined by the accessor keywords
(access modifiers) like public, protected, private, final or
abstract.
- public methods are accessible with object created in the
client's routine, then makes a request using appropriate syntaxes.
- private: If a class is not
|