Tutorial #11
Friends lets start this tutorial with an unusual topic called as Autoboxing, Unboxing & Annotation .
** Autoboxing & Unboxing
These features are being introduced in J2SE 5.0, & facilitates the process of handling primitive data types in
collections.We can use this feature to convert primitive data types to wrapper class types automatically.
The compiler generates a code implicitly to convert primitive type to the corresponding wrapper class type &
vice-versa.
For example
Double d_object = 100.2;
double d_primitive = d_object.doublevalue( );
Using autoboxing or unboxing feature, we can rewrite the baove code as :
Double d_object = 100.2;
double d_primitive = d_object ;
But , java compiler provides restrictions to perform the following conversation:-
- Convert from null type to any primitive type.
- Convert to the null type other than the identify conversation.
- Convert from any class type C to any array type if C is not object.
** Annotation
It is also known as metadata. This feature is mainly used to merge additional Java elements with programming elements such as class, methods, local variable, package & fields.
Metadata is stored in java class file by the compiler & these class files are used by the JVM or by
the program to find the metadata for interacting with the programming elements. A nice definition is given by
Wikipedia about annotation http://en.wikipedia.org/wiki/Java_annotation .This link provide a nice concept
about annotation with various examples related to each types of annotation .
While using annotations, we need to follow some guidelines:
- Do not use extends clause, It automatically extends the marker interface java.lang.annotation.Annotation
- Do not use any parameter for a method
- Do not use generic methods
- Do not use throws clause.
____________________________________________________________________________________
____________________________________________________________________________________
import
java.lang.annotation.*;
import
java.lang.reflect.*;
@Retention
(RetentionPolicy.RUNTIME)
@interface
MySingle
{
int
value() ;
//this variable name must be value
}
public
class
single
{
//annotate a method using a marker
@MySingle
(
100
)
public
static
void
MyMeth ()
{
single ob =
new
single ();
try
{
Method m = ob.getClass().getMethod(
"MyMeth"
) ;
MySingle anno = m.getAnnotation(MySingle.
class
) ;
System.out.println(
"The value is :"
+anno.value());
//displays 100
}
catch
(NoSuchMethodException exc)
{
System.out.println(
"Method not found"
);
}
}
public
static
void
main (String args[])
{
MyMeth();
}
}
To download this example in .java format that can run on eclipse by just pasting
the file in .src directory is given below:-
https://www.dropbox.com/s/u8y1qlay6mdsgw9/single.java
___________________________________________________________________________________
___________________________________________________________________________________
** Interfaces : Multiple inheritance:-
An interface is basically a kind of class. Like classes , interfaces contain methods & variables but with a
major difference. The difference is that interfaces define only abstract methods & final fields. This means
that interfaces do not specify any code to implement these methods & data fields contain only constants.
Therefore, it is responsibility of the class that implement an interface to define the code for implementation of
this method.
Syntax-
interface Interface_name
{
varaible declaration;
method declaration;
}
Here variables are declared as follows:-
static final type Variable_name = value ;
& method is defined as given below :-
return-type methodName1 (parameter_list) ;
Note :- Differences between Class & Interface:-
|
|
|
|
|
|
|
|
Like classes interfaces can also be extended. That is , an interface can be sub-interfaced from other
interfaces. The new sub-interface will inherit all the members of the superinterface in a manner simplar to the
subclasses.
Syntax :-
interface name2 extends name1
{
body of name2
}
For eaxmple =>
interface ItemConstants
{
int code = 100 ;
string name = "fan" ;
}
interface item extends ItemConstants
{
void display( ) ;
}
Note that variable name & code are declared like simple variables because all the variables in an inetrface
are treated as a constants although the keyword final & static are not present.
We can also combine several interfaces together into one interface. For example the following
declaration is valid:-
interface ItemConstants
{
int code = 100 ;
string name = "fan" ;
}
interface itemMethods
{
void display( ) ;
}
interface Item extends ItemConstants , itemMethods
{
-----------------------
-----------------------
}
While interfaces are allowed to extend other interfaces, sub-interfaces cannot be defined the methods
declared in the superinterface.
After all , subinterfaces are still interfaces , not classes. It is the responsibility of any class that
implement the derived interfaces to define all the methods.
It is important to note that interface cannot extend class as it would violate the rule that an interface
can have only abstract methods & constants.
** Implementing interface
Interfaces are used as "superclasses" whose properties are inherited by class who implement the interface
Syntax:-
class class_name implements interface_name
{
body of class_name
}
Here the class class_name is implementing the interface interface_name.
A more general form of implementation may look like this:-
class class_name extends supercalss
implements interface1 , interface2, ........
{
body of class_name
}
Take one example to understand the above explained concept :-
__________________________________________________________________________________
__________________________________________________________________________________
interface
area
{
final
static
float
pi =
3
.14F ;
float
compute(
float
x,
float
y);
}
class
rectangle
implements
area
{
public
float
compute (
float
x,
float
y)
{
return
(x*y);
}
}
class
elipse
implements
area
{
public
float
compute (
float
x,
float
y)
{
return
(pi*x*y);
}
}
class
InterfaceTest
{
public
static
void
main(String args[])
{
rectangle rect =
new
rectangle();
elipse cir =
new
elipse();
area area1;
area1 = rect;
System.out.println(
"Area of rectangle="
+ area1.compute(
10
,
20
));
System.out.println(
"Area of elipse="
+ area1.compute(
10
,
35
));
}
}
To download this example in .java format that can run on eclipse by
just pasting the file in .src directory is given below:-
__________________________________________________________________________________
__________________________________________________________________________________
** Note:-
Any number of dis-similar classes can implement an interface. However to implement the methods, we
need to refer to the class objects as type of the interface rather than of their respective classes.
Note that if a class that implement an interface does not implement all the methods of the interface, then
the class becomes an abstract class & cannot be instantiated.
No comments:
Post a Comment