Can a java subclass declare a private method available in its java superclass?

Last modified on October 15th, 2014 by Joe.

Yes. A java private member cannot be inherited as it is available only to the declared java class. Since the private members cannot be inherited, there is no place for discussion on java runtime overloading or java overriding (polymorphism) features.

Comments on "Can a java subclass declare a private method available in its java superclass?"

  1. Joe says:

    Again I reiterate the answer is YES. Thanks Gyani. Please have a look at the following source code sample.

    I think the previous (Anonymous) reader misinterpreted it. He might have thought, that the base class having a method as xyz() and the same method coming in derived class as private xyz(). Then it is a error. It will result in compilation error “Cannot reduce the visibility of the inherited method from Light”. But I didn’t mean this. I meant the other way around. But I am sorry for the vague title of this post, which is the cause for all this issues. Anyhow, thanks to all.


    package com.javapapers.sample;

    class Light {
    private void glow() {
    System.out.println("Glow glow");
    }
    }

    class SunLight extends Light {
    public void glow() {
    System.out.println("Call me in your day time!");
    }
    }

    public class SubclassPrivateExample {
    public static void main(String args[]) {
    SunLight sL = new SunLight();
    sL.glow();
    }
    }

  2. Manju says:

    No We cant have this. We cannot reduce the scope of the method thats in the inherited class.

    The question is to have the private method in the derived class thats there in the super class.

    If the method in the super class is also private then its possible.
    As the topic of discussion is over riding its assumed that the super class method is not private.

  3. Shaunak says:

    thank you sir, really helpful…..could you suggest some more important questions?

  4. soheb says:

    java subclass can declare a private method available in its java superclass provided that the method in the superclass is also private.

    If the method in superclass is not private then we cannot declare the method in subclass as private.

  5. Vishal says:

    I Agree to what Soheb said above, this is an addition.

    If you use the annotation @Override in sub class method, compiler will complain. So without this annotation, it will work just fine.

    e.g.public class SuperClassA {

    private void checkPrivateOverride(){
    //Prints I am in SUper class
    }

    }
    ———————-
    public class SubClassB extends SubClassA {

    @Override
    private void checkPrivateOverride(){
    //Prints I am in SUB class
    }

    //Complier will complain here, remove @Override and it will work

    Vishal
    }

  6. Mayank says:

    Hi, Please don’t confuse and waste time of the readers by unnecessary lengthy arguments.
    Yes, subclass can have private method same as in superclass but it can not declare it as overridden method as superclass method will not be visible after being declare as private.
    @Gr8 Job Joseph
    Thanks,
    Mayank

  7. Trupti says:

    good job:)thanks a ton Joe;-)

  8. Srinivas says:

    Yes,we can override with public methods for private methods of base class,but we can’t with private methods in public methods of super class.

  9. Rakesh K says:

    @Vishal,Private methods are not inherited by subclasses and hence there is no point of overriding.

    But instead a subclass can declare a method with the same signature as a private method in one of its Superclasses

  10. Shinie Mathew says:

    Please execute this program.Can we say that “The private members cannot be inherited”?

    import javax.swing.*;
    import java.awt.event.*;
    class SuperFrame extends JFrame implements ActionListener
    {
    private JButton b;
    private JPanel p;
    SuperFrame()
    {
    b =new JButton(“OK”);
    p = new JPanel();
    b.addActionListener(this);
    p.add(b);
    getContentPane().add(p);
    setSize(300,300);
    setVisible(true);
    }
    public void actionPerformed(ActionEvent ae)
    {
    JOptionPane.showMessageDialog(this,”clicked”);
    }
    }
    class PrivateDemo extends SuperFrame
    {
    public static void main(String args[])
    {
    new PrivateDemo();
    }
    }

  11. Vijay Bhaskar says:

    Small correction in my previous comment.
    —————————————-

    Here we need to consider the 2 types of the class members separately.

    1.Attributes : Attributes in the superclass can be overridden in the subclass by reducing the visibility.
    eg: Public attibute in the superclass can be declared again in the subclass as private.

    This is because the variables are not binded dynamically to the runtime object instead they belong to the reference type.

    2.Methods : Methods in the superclass can not be overridden in the subclass by reducing the visibility.
    eg: Public method in the superclass can not be declared again in the subclass as private/protected or default.

    This is because if the subclass object is refered by the parent class, then the method which is public is visible for the compiler and if it accepts during compilation, then during the runtime the method of the actual object ie, the subclass should be called (in this case it is private) which is not correct (since you can not access the private methods outside the class).

  12. Raj says:

    Joe,
    First commentor said about a mistake..read ur question?? Answr is correct.

  13. Harish Yadav says:

    child can access every thing from parents but child’s property is not available
    to the its parent so here there is not issue of making a private method in its child class. i.e any method that is declared in child class is not accesable/visible to its parent (supper class).

  14. srikanth says:

    A superclass variable can refer child class variable but child cant y?
    ex;
    Parent d=new Child();//possible
    Child k=new Parent();//not possible y?

  15. Abhishek Tripathi says:

    I think the question should be like this:

    Can a java subclass declare a method, which is private and is available in its java superclass?

  16. Diwakar says:

    No, We can not override private method in Java, just like we can not override static method in Java. We can declare it. private not accessible outside…..

Comments are closed for "Can a java subclass declare a private method available in its java superclass?".