Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

While inheriting a class in C++, user can specify the access specifier like,

class Base
{
    public int mem1;
    protected in mem2;
};

class Derived1 : **private** Base
{
    // mem1 will be private here.
    // mem2 will be private here.
};

class Derived2 : **protected** Base
{
    // mem1 will be protected here.
    // mem2 will be protected here.
};

class Derived2 : **public** Base
{
    // mem1 will be public here.
    // mem2 will be protected here.
};

But the same is not possible in Java, i.e. extends in java is always like "public" inheritance in C++.

Could someone explain the reason for this?

share|improve this question
10  
One doesn't need a reason to omit a feature, one needs a reason (ideally, several good ones) to add it. – delnan Mar 17 at 16:37
This can only be answered speculatively, voting to close. – Jimmy Hoffa Mar 17 at 19:05

closed as not constructive by Jimmy Hoffa, Martijn Pieters, Dynamic, Michael Durrant, MichaelT Mar 17 at 23:44

As it currently stands, this question is not a good fit for our Q&A; format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.

2 Answers

up vote 3 down vote accepted

Any benefit that private / protected inheritance give you can be easily achieved through encapsulation. I don't see that there is any particular need to include it as a language feature.

I guess the developers of Java felt the same way.

share|improve this answer

As Java does not have multiple inheritance and everything has to be (publicly) inherited from Object, there are no places in Java where private or protected inheritance would yield a valid program.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.