Wednesday, September 5, 2012
Today I am going to post how the access-specifier protected is related with inheritance in Java. It is less restricted than default but restricted than public.
How protected works and is different from default:Default and protected behavior differ only when we talk about subclasses.If the
protected keyword is used to define a member,any subclass of the class declaring the member can access it through inheritance.It doesn't matter if the superclass and subclass are in different packages,the protected superclass member is still visible to the subclass in a very specific way.This is in contrast to the default behavior,which doesn't allow a subclass to access a superclass member unless the subclass is in the same package as the superclass.
 Whereas default access doesn't extend any special consideration to subclasses (you're either in the package or you're not), the protected modifier respects the parent-child relationship, even when the child class moves away (and joins a new package). So, when you think of default access, think package restriction. No exceptions. A class with a protected member is marking that member as having package-level access for all classes, but with a special exception for subclasses outside the package. But what does it mean for a subclass-outside-the-package to have access to a superclass (parent) member? It means the subclass inherits the member. It does not, however, mean the subclass-outside-the-package can access the member using a reference to an instance of the superclass. In other words, protected = inheritance. Protected does not mean that the subclass can treat the protected superclass member as though it were public. So if the subclass-outside-the-package gets a reference to the superclass (by, for example, creating an instance of the superclass somewhere in the subclass' code), the subclass cannot use the dot operator on the superclass reference to access the protected member. To a subclass-outside-the-package, a protected member might as well be default (or even private), when the subclass is using a reference to the superclass. The subclass can see the protected member only through inheritance.

Showing accessibility of protected
The code below will explain it further

package package1;
public class Parent {
protected int x = 20;
// protected access
}



package package2;
import package1.Parent;
class Child extends Parent {
   public void testIt() {
      System.out.println("x is " + x);
// No problem;Child inherits x
      Parent p = new Parent();
// Can we access x using p reference?
      System.out.println("X in parent is " + p.x);
// Compiler error!
   }
}

The last line will give an error as following
package2/Child.java: x has protected access in
package1.Parent
System.out.println("X in parent is " + p.x);
1 error

Hope this helps. Keep looking for more posts.
Sunday, September 2, 2012
The answer to this question is NO. You cannot override static methods in Java. In order to show this,have a look at code below :

public class SuperClass{
   public static void show(){
      System.out.println("Static SuperClass Method");
   }
}

public class SubClass extends SuperClass{
    public static void show(){
       System.out.println("Static SubClass Method");
    }
}

public class Demo{
    public static void main(String[] args){
        SuperClass ob1=new SuperClass();
         SuperClass ob2=new SubClass();
        SubClass ob3=new SubClass();
         ob1.show();
         ob2.show();
         ob3.show();
    }    
}
The above code will give output as below :
Static SuperClass Method
 Static SuperClass Method
 Static SubClass Method
 If the static method had been overridden then the second line of the output would have been same as that of third line. So the method has not been overridden and a brand new method has been created in sub class. To understand it more deeply just rewrite the sub class as shown below :
public class SubClass extends SuperClass{
    @Override
    public static void show(){
       System.out.println("Static SubClass Method");
    }
}
If you write the latter one you won't be able to compile it. The compiler will tell that there is no such method in super class. So it is recommended that you always use the @Override annotation to verify that whether you have correctly overridden the method or not. So instead of getting an unexpected output you will get a compile time error which is better and easy for debugging. This is possible only with jdk1.5.0 and above.
REASON : The reason why static methods cannot be overridden is that the concept of inheritance is a concept of object. Now static methods and variables are class specific features and not object specific while non-static things are object specific features and for that reason only you can access static things directly using class name. Though static things can be accessed through objects yet they are related to classes and bound with them. Many of you may confuse with this. That's why C# does not allow you to access any static things with objects and thus have removed this confusing feature of Java.

Total Pageviews

Subscribe via Email

Followers


Popular Posts

About Me

My photo

I am a student of BTech Computer Science Engineering from RCCIIT,Kolkata. I am a crazy lover of Java and wants to settle as a Java developer. I have a seven years Java experience with an application developer experience for 2 years. Recently from March 2012 I am a registered S40 app developer for Nokia and has corrected an app of them. I am currently writing blogs to encourage and grow interest in all those who don't know or learning Java.