Wednesday 20 July 2011

brother difference between private and final ...

look i am with java ... and want to explain that private method are never in state of overridden ...

but why ... if i say ... that grandfather has a old dress and they built this dress only for him ... do you thik that after declaring this method as a private father is ready to wear or use this dress ...

so look at this code ....
public class Baseclass
{
    private void method()
    {
        System.out.println("base class called");
    }

    public static void main(String[] args)
    {
        Baseclass b = new Subclass();
        b.method();
    }
}

 class Subclass extends Baseclass
{
    protected void method()
    {
        System.out.println("sub class called");
    }
}


look if you say that private method also inherited than ... i will never support you ...
because private is defined for a class ... and if you make a same method in another class as private than you never face a problem and if you declare a method also as a public than u will never face a problem

means private class is only visible to its class .....
if you want to try or check .... that ,,, i am wrong than declare this baseclass method as a final and if it compiles than i am correct and if not than  i am wrong .

now its time to the output ...
                                              Baseclass b = new Subclass();
                                                             b.method();

what this statement says that first goes in Subclass for search of method() ,, if you not found that method than come in Baseclass and search here .... ok now you find the method () and o/p is  " base class " ....
nethod () is not found in subclass means ... because your method is private and thats why private method has it unique signature and thats why this method is not inherited in subclass ... but why the protected method is not used thats the question .....

but look the code closely ... because ...in the declaration of this syantx ... you are saying that ok i am providing all subclass properties but  + baseclass properties ... but at the compile time synatx ( baseclass b)  we are defining that we have a method so you have to access it ... means at the compile time means
you can only access those methods which are present in baseclass .. so in baseclass there is only private with unique signature ...thats why here protected method is not working ...

want to implement some best things with your computer   use this for improving your thinking about computer

No comments: