-1

I know that private methods are hidden in Derived class and they cant be overridden but my question is different.. Please go through the following problem. My question is mentioned in the problem stated below:

TestPolymorphism.java

class CheckParent {
    private void display() {
        System.out.println("This is parent class");
    }
}

class CheckChild extends CheckParent {
    void display() {
        System.out.println("This is child class");
    }
}

public class TestPolymorphism  {
    public static void main(String[] args) {
        CheckParent cp = new CheckChild();
        cp.display();        // This will throw error as display() method id private
        //  and invisible to child class
    }

However, in following code snippet no exception is thrown.

CheckParent.java

  public class CheckParent {
       private void display() {
            System.out.println("This is parent class");
        }
        public static void main(String[] args) {
            CheckParent cp = new CheckChild();

            cp.display();   /*This will print "This is parent class" without any error
            * my question is why no error is thrown like above case
            * as here too display() method is private and invisible to
            * derived class */
        }
    }

    class CheckChild extends CheckParent {
        void display() {
            System.out.println("This is child class");

        }
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
priyank
  • 11
  • 1
  • 5
  • before answering anything can yo please mention how many files are you creating? i mean to say do you write everything in one .java file or are they different? because that also makes difference in scope of the method – madteapot Jul 31 '14 at 14:54
  • hey...i have made it more clear by classifying as first program and second program..... My question is, error was thrown because of visibility problem due to word PRIVATE, which is quite understandable, but why no error was thrown in second program though there is still visibility issue due to word PRIVATE as i am making Parent type reference to child object.. – priyank Jul 31 '14 at 17:07

2 Answers2

1

For constructor, private, final or static methods static(early) binding is used. For all others dynamic(late) binding is used. Check this out:

http://geekexplains.blogspot.co.uk/2008/06/dynamic-binding-vs-static-binding-in.html

alobodzk
  • 1,284
  • 2
  • 15
  • 27
0

If you would like to override a parent class method please consider placing the @Override annotation in front of the overriding method, so that compiler can warn you of possible mistakes.

I do not think you are overriding the display() method of a parent class with display() method of a child class, because the parent class's method is private. You are rather defining an new method.

class CheckParent {
    //remove private access modifier to have the method overriden in child class
    private void display() {
        System.out.println("This is parent class");
    }
}

class CheckChild extends CheckParent {
    @Override //IDE or compiler will warn you that you are not overriding anything        
    void display() {
        System.out.println("This is child class");
    }
}

Please see Bloch's Effective Java. 2nd Edition, Item 36.

In the second code snippet you create CheckChild instance but store is as CheckParent accessing the private void display() of CheckParent instance method inside the CheckParent class's static method. Class members (static method in your case) can access it's private methods and fields, so this code is valid.

If you want to access the instance void display() method of the CheckChild class instance in the second code snippet you should cast the CheckParent instance to CheckChild first.

public class CheckParent {
    private void display() {
        System.out.println("This is parent class");
    }
    public static void main(String[] args) {
        CheckParent cp = new CheckChild();
        cp.display();   //"This is parent class"

        if (cp instanceof CheckChild){
            ((CheckChild) cp).display(); //"This is child class"
        }
     }
}
class CheckChild extends CheckParent {
    void display() {
        System.out.println("This is child class");
    }
}

Anyway such coding approach looks like a misuse of inheritance to me.

  • ur right..there is no overriding going on......hey...i have made it more clear by classifying as first program and second program..... My question is, error was thrown because of visibility problem due to word PRIVATE, which is quite understandable, but why no error was thrown in second program though there is still visibility issue due to word PRIVATE as i am making Parent type reference to child object.. – priyank Jul 31 '14 at 17:09