0

The code in question is:

class Student {
    private void study() {
        System.out.println("Student is studying");
    }

    public static void main(String args[]) {
        Student student = new Sam();
        student.study();
    }
}

public class Sam extends Student {
    void study() {
        System.out.println("Sam is studying");
  }
}

Output:

Student is studying

An object of class Sam is created and as Student is the superclass we can assign Sam object to a reference variable of type Student. When it is calling the method study, it should be obvious that the object has the study method that prints "Sam is studying". I see people saying that as the Reference type is of Superclass and as the method was hidden it calls the superclass method but I cant digest this because of my understanding of the object and reference type. i.e reference variable is only a pointer to a object(remote control analogy of head first java)

Saumyaraj
  • 1,220
  • 3
  • 15
  • 37

4 Answers4

2

As mentioned in the comments, a private method is automatically final and hidden. You are therefore not able to override any private methods. Therefore the derrived method study will become a brand new method, and will not override the Student's study.

See this link.

  • I know that the method is not overriden. I am just not able to understand that how can a parent class method exceuted without an object of parent class – Saumyaraj Oct 10 '16 at 06:18
  • When you have a private access modifier, the study() will create new method rather that overriding study in Student. But you have still extended Student, so Sam will inherit the properties from Student class (just not the private once). If you where to add a method with non-private access modifier in the Student class, Sam would inherit those. – André Kramer Orten Oct 10 '16 at 06:34
  • correct , nut as the Sam has not inherited the private method of student, there is no way that method of student can be on sam(object on heap) so when we call study from sam how can a superclass method gets called? – Saumyaraj Oct 10 '16 at 07:05
1

I am explaining method hiding and method overriding concept through a simple example

/* Java program to show that if static method is redefined by
   a derived class, then it is not overriding. */

// Superclass
class Base {

    // Static method in base class which will be hidden in subclass 
    public static void display() {
        System.out.println("Static or class method from Base");
    }

     // Non-static method which will be overridden in derived class 
     public void print()  {
         System.out.println("Non-static or Instance method from Base");
    }
}

// Subclass
class Derived extends Base {

    // This method hides display() in Base 
    public static void display() {
         System.out.println("Static or class method from Derived");
    }

    // This method overrides print() in Base 
    public void print() {
         System.out.println("Non-static or Instance method from Derived");
   }
}

// Driver class
public class Test {
    public static void main(String args[ ])  {
       Base obj1 = new Derived();

       // As per overriding rules this should call to class Derive's static 
       // overridden method. Since static method can not be overridden, it 
       // calls Base's display() 
       obj1.display();  

       // Here overriding works and Derive's print() is called 
       obj1.print();     
    }
}

Output:
Static or class method from Base
Non-static or Instance method from Derived

Since we know that we can't override static method ,but if we override the static method then it is known as method hiding not method overriding .In case of simple method it is known as method overriding .I hope i have cleared your doubt

in the above example

Since method hiding is happening in above code and in case of method hiding , you have to remember one important point that reference variable will decide that which method will be invoked

If we are doing this

Student student = new Sam();

Output will be

Student is studying

if we are doing this

Sam student = new Sam();

output will be

sam is studying

In case of method overriding the object will decide that which method will be called.

Ketan G
  • 507
  • 1
  • 5
  • 21
  • Thanks for the explanation on static method overides but I am clear on static part. I am not clear on the instance method marked as private as asked in question – Saumyaraj Oct 10 '16 at 06:21
  • I have edited my answer according to your question see it and tell me if u have any doubt – Ketan G Oct 10 '16 at 06:35
  • @Saumyaraj got it ?? – Ketan G Oct 10 '16 at 06:51
  • If we remove private from the superclass method then also we will get the clild class method printed, so my doubt is that having private method on the superclass results in superclass method being printed on a sub class object – Saumyaraj Oct 10 '16 at 07:03
  • if u remove private from superclass method then it will become method overriding then method will be called on the basis of object creation not reference variable and if method hiding is taking place then method will be called on the basis of refernce variable, not object creation. – Ketan G Oct 10 '16 at 07:10
  • Yes, this should be documented somewhere. My upvote for this! – Saumyaraj Oct 10 '16 at 07:11
1

I will try to explain this in different words. Using Student reference variable, you can only call methods which are visible to Student class. Since Student has study method defined as private, and you are calling the study method from main method of same class, it is visible only inside the class and you are able to call study method of Student.

To clarify the concept, try these variations

  • Use the same code in main into a different class, i.e. make an object of Student and try to call its study method. It wont compile as its not visible
  • Even if the object is of type Sam, study is still not visible since you are using student reference variable.

The only reason you could call study was because it was called from the same class. Hope this helps.

Rishi Goel
  • 670
  • 4
  • 10
0

It would call the method from Sam, only if you make it virtual so it can have polymorphic behavior, by then an object of type Sam as a Student it is indeed a reference to Student, and it will call the methods from Student.

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • How can a Student's method be invoked without even creating a Student's object? For invoking a method of a class you should have its object created unless it is static and in this case we have not even created a object of student – Saumyaraj Oct 10 '16 at 06:04
  • 1
    @Saumyaraj we have a student, it is Sam (which indeed is a student and treated as a student). – meJustAndrew Oct 10 '16 at 06:12
  • So shall I think in a way that Sam Object has two study methods one hidden method of the parent and one its new method and it will invoke based on the reference type? – Saumyaraj Oct 10 '16 at 06:17
  • @Saumyaraj indeed – meJustAndrew Oct 10 '16 at 06:18