Friday, March 11, 2011

Inheritance Explained

Everyone is aware of what is inheritance if they are in the world of Java. Let me dig some what deeper into that with casting objects and things like that.
Classes can be derived in java by deriving members of other class. Here members are public/protected fields, methods and nested classes. Constructors are not members of the class, so they are not inherited, but they can be invoked from the subclass.

Here the subclass is the class which derives members of the other class. It is also called derived class, child class, or extended class. The class from which the subclass is derived is called base class or super class or parent class.

What all we can do in subclass?
  1.  We can also inherit the members of a class (super class) with no modifier (package-private) if the subclass is in the same package. (modifiers restrict access to members of a class. Ex: public, private, protected and no modifier or package-private)
  2. We can use members of the super class in sub class like as they are defined in sub class
  3. We can declare new methods and fields in sub class. They don't affect super class in no way
  4. We can declare a new field in the sub class with the same name as in the super class, thus hiding the one in super class (Its not recommended) 
  5. We can create a new instance method in the sub class with the same method signature as in the super class, thus overriding it
  6. Sub class can inherit private fields and no package-private fields if the super class  has public or protected methods to access these fields
  7. If the super class has a public or protected nested class, it has access to all the members of it enclosing class. If the sub class is derived from the nested class, it has access to all the members of the class, which encloses the nested class
There will be a little bit confusion when we cast objects, if there exists inheritance.

Let us take an example, there are two classes A and B. A is the super class and B is the sub class. We can always cast sub class object to super class object, but not vice-versa. If we try to do it, we'll get ClassCastException, which is a run-time exception.

Let us take an example of casting.

public class A {
    public void display()
    {
        System.out.println("Dispay in A");
    }
}
public class B extends A {
    public void display() {
        System.out.println("Display in B");
    }
}

Class Test with main method to test the above scenario

public class Test {
    public static void main(String[] args) {
        A a = new A();
        B b = (B)new A(); //ClassCastException is thrown at run-time
        a.display();
        b.display();
    }
}


If we change B b = (B)new A(); to A b = new B(); and call b.display, guess which display method is called? Its the display method in B class. Because we created object for B class and casted it to object of type A. So it depends on which class is instantiated and not on to which object its casted when there is inheritance while calling methods.

No comments:

Post a Comment