Wednesday, June 20, 2007

Diving Deep into Java (I):

I thought, let’s highlight some concepts of Java which many of us may know or may not. But those are important and interesting. So let dive deep into Java.


1. Can you declare a class abstract & final at the same time?

The answer is no. Because when you declare a class abstract, it means you can’t create an instance of that class directly. First you need to create a subclass of that class and then you can create an instance of that subclass. So, abstract class is made to be sub classed. On the contrary, final class can’t be sub classed. So, I think now it is clear that a class can’t be abstract & final at the same time.

2. We all know that private members are invisible to any code outside the member’s own class. So, you are free to declare a method with the same name as in the super class. This will not fall in the overriding or overloading category.
You all know that in case of overloading you can’t change only the return type of an overloading method. You have to change the signature of the method.
But in case of the above scenario you don’t need to worry about the overloading rule.

If the point is still not clear let’s take an example –
class A
{
private void printName(String nm)
{
System.out.println(nm);
}
}

class B extends A
{
public int printName(String nm)
{
System.out.println(nm);
return nm.length();
}
}

So, if you look at the above example it seems that it is breaking the overloading rule. But as the “printName” method is private in class A, class B is free to declare a method with the same name. To class B, it is as if class A does not have any such method.

3. What type of access do you have to a protected member of a class inside its subclass which belongs to a different package?

Protected members are inherited inside it’s subclass out side the package. So inside that subclass you can access that member using “IS A” relationship. But you can’t access it using a “HAS A” relationship.

Let me make it clear with an example –

package pkg1;
public class A
{
protected int count = 10;
}

package pkg2;
import pkg1;
public class B extends A
{
A a1 = new A ();
System.out.println (count);
System.out.println (a1.count); // this line is wrong
}

The line number 7 inside the class B is incorrect. We can’t use the (.) operator on an instance of class A to access the member “count” from inside class B. The member “count” is only inherited. So, we can think as if “count” is a member of class B. That’s why line number 6 is correct.

4. A very small point – never declare a local variable, inside any method, with public-private-protected access modifiers. You can only use “final” with local variables.

5. What can’t be changed and can be changed when you declare an object reference variable as final?

You can’t assign a new instance to the reference variable. But you can change the values of the members of that object.

6. We know that declaring a variable with final keyword makes it impossible to reinitialize that variable. But do we know that this is only true when we declare it with an explicit value? Please note, I have said explicit value, not default value.

7. One interesting point to remember – variables declared inside an interface is implicitly “public static final”. You can also explicitly declare it as “public static final”. No problem in that.
But methods declared inside an interface can’t be final, private, static, protected, synchronized, native or strictfp.

No comments: