A class can be extended by another class and hence reuse the methods and variable of the class extended. This is called Inheritance.
class A
{
…..
}
class B extends A
{
…….
}
A class that contains at least one abstract method must be marked abstract. The class
That is marked abstract is called abstract class. Abstract class may or may not contain
abstract method. It’s important to remember that when a class is abstract, it can not be instantiated. An abstract class must be subclass to make it concrete and hence it can be instantiated.
For example:
abstract class A
{
abstract void aMethod();
}
class B entends A
{
void aMethod()
{
……..
}
}
A anInstance = new A(); // will fail on compile
B anInstance = new B(); //will compile and execute
A anInstance = new B(); // will compile and execute
A class can also contain another class as its member. Such class is called Nested Class or Inner class.
class A
{
class B
{
…..
}
}
In this case the class B shares with an instance of class A. The inner class can access all members of the outer class even those marked as private.
To create instance of class B inside class A
B instanceB = new B();
To create instance of class B outside class A
A.B instanceB = new A().new B();
Important point to remember is that inner class can not have static members. Similarly the inner class can not instantiated from a static method of the outer class. Don’t forget, in static code there is no this reference (reference to the current instance). The only way to create instance of inner class in static method is to outer class.
A.B instanceB = new A().new B();
Even though the inner class can access all members of outer class, how can the inner class reference the outer class instance? Inside inner class this references the inner class instance, so this can not be used to reference the outer class instance. In that case the outer class instance can be referenced as A.this.
class A
{
class B
{
System.out.println(“Outer class ref :”+A.this);
}
}
The modifiers that can be applied to an inner class are final, abstract, static, strictfp, public, private, protected and default.
It’s now clear that a class can have another class in it. Do you that a method can also have a class inside it. Yes, such class is called Method Local Inner class.
class A
{
void doTest()
{
class B
{
……….
}
}
}
A method-local inner class can be instantiated only within the method where the inner class is defined. This type of inner class can also access all members of outer class.
But the method-local inner class can not access the local variable of the method the inner class is in. The reason is that the local variables of the method are placed in stack and hence it exists only till the method has scope. Even when the method completes, the instance of the method-local inner class is still alive on heap. When the local variable is marked final, then the inner class can access the local variable of the method. The modifiers that can apply to method-local inner class are abstract and final. Remember, it can either be abstract or final not both.
Next let us go through what is anonymous inner class? An inner class declared without any class name is called Anonymous Inner class.
class A
{
public void aMethod()
{
…..
}
}
class B
{
A aInstance = new A() { //anonymous inner class
public void aMethod(){
……
}
};
}
Another example of anonymous inner class is an anonymous implementer of the specified interface type.
interface SampleInterface
{
public void aMethod();
}
class SampleClass
{
SampleInterface si=new SampleInterface() {
public void aMethod()
{
…..
}
};
}
Here it’s not instantiating a SampleInterface object; it’s not creating an instance of a new anonymous implementer of SampleInterface. A class can extend only one class and implement more than one interface. But in case of anonymous inner class can be implementer of only one interface. If the anonymous inner class is a subclass of a class type, it automatically becomes an implementer of any interface implemented by the superclass.
Thursday, May 1, 2008
Declarations, initialization, and scoping Continued...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment