Teja
Teja

Reputation: 13534

Private Access Specifier usage in Java Inheritance

We can access the Super Class methods which consists of operations on private data members and print the results.But why can't I print the private data members of Super Class with the SubClass object calling them in my main function? Someone please explain me.

Here is the example below.

class SuperClass1
{
    private int a;
    private int b;

    SuperClass1(int p,int q)
    {
        a=p;
        b=q;
    }
    SuperClass1(SuperClass1 obj)
    {
        a=obj.a;
        b=obj.b;
    }
    SuperClass1()
    {
        a=-1;
        b=-1;
    }

    int Vol()
    {
        return a*b;
    }
}

class SubClass1 extends SuperClass1
{
    int c;

    SubClass1(int p,int q,int r)
    {
        super(p,q);
        c=r;
    }
    SubClass1(SubClass1 obj)
    {
        super(obj);
        c=obj.c;
    }
    SubClass1()
    {
        super();
        c=-1;
    }
}


public class Super 
{
    public static void main(String[] args) 
    {
        SubClass1 obj1=new SubClass1();
        //System.out.println("The values of obj1 are:"+obj1.a+""+obj1.b+""+obj1.c);
        int vol=obj1.Vol();
        System.out.println("The volume is :"+vol);
    }
}

Upvotes: 0

Views: 1663

Answers (4)

Sanjeev
Sanjeev

Reputation: 1575

Private members are not inherited; only the protected and public members are.

If possible, you can do one of the following:

  1. Make the private properties of the superclass protected
  2. Make public getters (and setters if needed) for the private properties

Upvotes: 0

amit
amit

Reputation: 178451

security and encapsulation

The superclass is letting its subclasses use only the public and protected methods/fields.

This allows the designer of the superclass to change the implementation of these methods if he sees it better, without breaking the subclass's correctness.

A text book example is a complex number class.
The programmer using this class only needs its functionality, he doesn't care if the implementation is with imaginary and real fields or with radius and theta fields [two distinct ways to represent complex number].

It allows the designer of the ComplexNumber class more freedom if he wants to change the class in later versions, and it also allows the user less worries: he doesn't need to take care for all the details, some are being taken care of for him.

Bonus: note you can break this behavior and access private fields and methods by using reflection - but when you do so - all bets are off, and you do it on your own responsibility.

Upvotes: 2

zengr
zengr

Reputation: 38899

JLS says:

Members of a class that are declared private are not inherited by subclasses of that class. Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared.

So, to answer you question. No, private members are not accessible by subclasses.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500535

Your question isn't very clear without an example, but I suspect that the "methods which consist of operations on private data members" aren't private. It doesn't matter that they work by accessing private data - they're not private themselves. It would be pretty pointless having access modifiers if public methods could only access other public members etc.

The whole point of encapsulation is that only the class itself should care about implementation details such as the fields in question, but can expose a contract in terms of its public (and protected) API. Code outside the class shouldn't care about the private implementation details.

Upvotes: 1

Related Questions