Let us first consider the following Java program as a simple example of Overriding or Runtime Polymorphism.
The program prints “Derived fun”.
The Base class reference ‘obj’ refers to a derived class object (see expression “Base obj = new Derived()”). When fun() is called on obj, the call is made according to the type of referred object, not according to the reference.
Is Overiding possible with private methods?
Predict the output of following program.
We get compiler error “fun() has private access in Base” (See this). So the compiler tries to call base class function, not derived class, means fun() is not overridden.
An inner class can access private members of its outer class. What if we extend an inner class and create fun() in the inner class?
An Inner classes can access private members of its outer class, for example in the following program, fun() of Inner accesses private data member msg which is fine by the compiler.
Accessing Private Member of Outer: GeeksforGeeks Outer fun()
In the above program, we created an outer class and an inner class. We extended Inner from Outer and created a method fun() in both Outer and Inner. If we observe our output, then it is clear that the method fun() has not been overriden. It is so because private methods are bonded during compile time and it is the type of the reference variable – not the type of object that it refers to – that determines what method to be called.. As a side note, private methods may be performance-wise better (compared to non-private and non-final methods) due to static binding.
Comparison With C++
1) In Java, inner Class is allowed to access private data members of outer class. This behavior is same as C++ (See this).
2) In Java, methods declared as private can never be overridden, they are in-fact bounded during compile time. This behavior is different from C++. In C++, we can have virtual private methods (See this).
This article is contributed by Chandra Prakash. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Can we Overload or Override static methods in java ?
- Private and final methods in Java
- Private Methods in Java 9 Interfaces
- How to overload and override main method in Java
- finalize() method in Java and how to override it
- Why to Override equals(Object) and hashCode() method ?
- Static methods vs Instance methods in Java
- Private Constructors and Singleton Classes in Java
- Replacing 'public' with 'private' in "main" in Java
- How to call private method from another class in Java with help of Reflection API?
- Why can't static methods be abstract in Java?
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.io.BufferedWriter class methods in Java
- java.lang.Character class methods | Set 1
- Java Interface methods
- Default Methods In Java 8
- ArrayList and LinkedList remove() methods in Java with Examples
- HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
- Hashmap methods in Java with Examples | Set 2 (keySet(), values(), containsKey()..)
- BitSet class methods in Java with Examples | Set 3