2
public class Shape
{

final private void print() 
{
    System.out.println("in class Shape");
}

  public static void main(String[] args)
  {

    Shape shape=new Rectangle();
    shape.print();
    //calling shape class function 
    //giving output in class shape


  }

}

 public class Rectangle extends Shape
 {
    public void print() 
    {
    System.out.println("in class Rectangle");
    //super.print();

    }
  }

Ques: why private function don't show polymorphic behaviour ? and we are still overriding final method? its calling base class funtion why?

Santhosh
  • 8,181
  • 4
  • 29
  • 56

6 Answers6

5

A Private function is not visible nor callable from its children; hence these are two completely different functions. There is nothing to overwrite from the perspective of the child class, because it is not aware that the parent even has a print() function.

Erik
  • 3,598
  • 14
  • 29
  • 2
    Furthermore, your `print` function in the parent class is declared `final`, which explicitly implies that it cannot be overridden even if it were not private. – Zoltán Nov 27 '14 at 09:59
1

Making it final private void print() is to prevent it from overriding in sub-classes.

As final prevents overriding and private makes the method invisible to the sub-classes so that it cant be accessed

See also :

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • They don't both imply the same thing. Making it `private` means that the child classes cannot even call it. Declaring it `final` and `protected`, package-private, or `public` would allow child classes to see it and call it, but not to override it. – Zoltán Nov 27 '14 at 10:01
  • Your edit is even more confusing. Yes, they both mean that they cannot be overridden, but they are most certainly not the same. `final void print()` cannot be overridden, but it can be invoked from any other class in the same package. `private void print()` cannot be overridden **because** it cannot be **seen** from any other class. – Zoltán Nov 27 '14 at 10:05
  • Thanks for updating the answer, I removed my downvote :) – Zoltán Nov 27 '14 at 10:05
1

You are not actually over-ridden the print method because of private. They are completely different.

More over you cannot override a final method.

This is the place where @override annotation helps you better. If you try to place the annotation, then you realize the behaviour at compile time itself.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

In addition to Eriks answer from the Java Language Specification:

A class C inherits from its direct superclass all concrete methods m (both static and instance) of the superclass for which all of the following are true:

  • m is a member of the direct superclass of C.
  • m is public, protected, or declared with package access in the same package as C.
  • No method declared in C has a signature that is a subsignature (§8.4.2) of the signature of m.

and

An instance method mC declared in or inherited by class C, overrides from C another method mA declared in class A, iff all of the following are true:

[...] One of the following is true:

  • mA is public.
  • mA is protected.

So your subclass doesn't inherit the private methods, hence there is nothing to override.

Community
  • 1
  • 1
André Stannek
  • 7,773
  • 31
  • 52
0

Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. In other words, polymorphism allows you define one interface and have multiple implementation. This is one of the basic principles of object oriented programming.

The method overriding is an example of runtime polymorphism. You can have a method in subclass overrides the method in its super classes with the same name and signature. Java virtual machine determines the proper method to call at the runtime, not at the compile time.

But if you will think as print() is instance method and at runtime why it is not calling from Rectangle print() method.

The reason is as print() of subclass is not a overriden method as parent class method is final which cannot be overriden.

  Shape shape=new Rectangle();
  shape.print(); // prints in the shape class

  ((Rectangle)shape).print(); //prints in the rectangle class

As parent's class method is private so it is not visible for outside world and as it is final it cannot be overriden.

Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70
0

In your example it is shown as private final method so this method is not visible out side the class. So Rectangle can't see the method defined inside Shape class.

public class A{
  final private method1(){
  }
  final public method2(){
  }
  public method3(){
  }
}

public class B extends A{
  public method1(){
   //it is legal. but it is not a override. this method can't see the method1 defined in A
  }
  public method2(){
   //throw error. because final method can't be overriden
  }
  public method3(){
   //legal override method
  }
}
Bruce
  • 8,609
  • 8
  • 54
  • 83