2

I am curious as to how the concept of "inheritance" takes place in the world of object oriented programming. Let me explain my dilemma (I came across this dilemma while studying Java but I hope that my question is generic in nature) :

Suppose there is a class A and a class B. Class A "inherits" Class B. What does this actually mean? Does the compiler make a "new" class, which is THEN instantiated into an object which contains the elements of both the classes A and B behind the scenes? If that's the case, then how are the access restrictions implemented according to the access specifiers?

For a moment, I wondered if it happens in the following manner :

An object of class A is created and then an object of class B is created. Java then somehow "link" the members of A to the members of B and make it appear as if they belonged to the same class and it does that according to the access specifiers.

But then, it occurred to me that there is a fault with this theory. Suppose two different classes, B and C are inheriting class A. Then, if we are going to make objects of class B and class C, then they'll have their OWN copies of the elements of class A. So this theory fails too.

I was just trying to explain the confusion about inheritance that I have in my mind. It's giving me headache. Please help.

EDIT : This is a link to a discussion related to my question which i found on this site.

Do subclasses inherit private fields?

Community
  • 1
  • 1
finitenessofinfinity
  • 989
  • 5
  • 13
  • 24
  • I can't really understand your question, which is not bad, it just shows that you're trying to understad inheritance. I would suggest your to read something about inheritance ([wikipedia](http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29)). – Augusto Sep 22 '12 at 18:00
  • Not sure if question related to understanding inheritance or understanding how does a language like JAVA implements inheritance.. – gtgaxiola Sep 22 '12 at 18:01
  • I think you should edit your question to replace "Class A inherits Class B" with "Class B inherits Class A" or "Class B is inheriting Class A" – Mani Sep 22 '12 at 18:01
  • @Mani What's wrong with Class A inheriting Class B? – finitenessofinfinity Sep 22 '12 at 18:04
  • @finitenessofinfinity There is nothing wrong in doing that. But keeping in mind your second scenario where B,C are inherited from A I just thought it would be better to be consistent and keep A as your super class through out the question – Mani Sep 22 '12 at 18:06
  • You'll need to look up JVM implementation details (if you're interested in Java specifically) or a reference to whatever other language you care about. Any answer short enough to appear here isn't going to describe it in language you'll understand, if you're talking about "their OWN copies of the elements of class A", whatever that means. – Dave Newton Sep 22 '12 at 18:07
  • @DaveNewton Does that mean that JVM, and let's say another language, like C# (CLR), implement inheritance differently? Even to the level of the very concept of inheritance? – finitenessofinfinity Sep 22 '12 at 18:09
  • @finitenessofinfinity What do you mean by the "very concept of inheritance"? Every language means something specific by "inheritance". IIRC Java and C# mean essentially the same thing, although I don't really know C#. How it's *implemented* at the VM/language level will vary wildly, even if the "concept" of inheritance is identical. – Dave Newton Sep 22 '12 at 18:12
  • @finitenessofinfinity does [this](http://stackoverflow.com/questions/7672109/memory-allocation-for-a-class-in-java) help? – Mani Sep 22 '12 at 18:17
  • 1
    @DaveNewton Let me rephrase my confusion. It is said that a class cannot "inherit" the private members of another class. But, let's suppose we try to access that particular private member through an object of the subclass. Then, in that case, the compiler complains of the visibility of the private member. So, does the object of the subclass "have" it but still is not allowed to touch it or it doesn't "have" it at all? – finitenessofinfinity Sep 22 '12 at 18:21
  • @Mani Thanks for the link. It does give an insight. – finitenessofinfinity Sep 22 '12 at 18:22
  • @finitenessofinfinity It's "there" but not directly accessible. Superclass methods may use the private property, the subclass may use accessible superclass methods that use the private property, the subclass may *not* use the private property directly. (Ignoring reflection etc., obviously.) – Dave Newton Sep 22 '12 at 18:39
  • @DaveNewton It's this use of double quotes ("there") which I am curious about. In terms of objects and memory. What does it mean for the private member to be "there"? But if it's not possible for the answer to be given correctly here (being to complicated and language/technology dependent) then I guess my question was futile. – finitenessofinfinity Sep 22 '12 at 18:47
  • @finitenessofinfinity The subclass object has the property--this should be self-evident, since the object can call superclass methods, and those methods work. The subclass may not access that property directly. – Dave Newton Sep 22 '12 at 18:48
  • @DaveNewton Who "has" it? What does "has" even mean? Is it the object of the subclass? That is, separate memory allocated for the members of class A (including the private members) which are inherited by the different subclasses? – finitenessofinfinity Sep 22 '12 at 18:53
  • I don't really know what you're saying. We're talking about instance properties; each instance has their own blob of memory. The *physical* memory will depend on any number of factors, from the spec to the implementation. Access rights are managed by the vm in the case of java. – Dave Newton Sep 22 '12 at 20:51

2 Answers2

2

I may fail, but I'm going to take a stab at an explanation on this for you.

In honesty, I think you may be making what is really a very classic mistake in how you conceive object programming - and that's making the distinction between objects and classes. Object creation in virtually any object-oriented language is one of construction based on a class definition. From your question, it sounds as though you are mentally modeling an OO language in general and object inheritance in particular as aggregating discrete objects, where in reality the objects are being defined in terms of aggregated class definitions.

public class A
{
    public A();
}

public class B:A
{
    public B();
}

public class C:B
{
    public C();
}

In your A->B->C model, C's definition is in terms of its own unique properties plus all the members of its immediate ancestor, B, which, in turn, is defined in terms of its own unique properties plus all the members of its immediate ancestor, A. The process of creating the object is still a unique and discrete event, and the object, despite its multi-layered heritage, is still only one object at instantiation time.

Regarding the visibility of certain members: When a class author designs and builds a class, he makes certain decisions about what he makes available in two distinct perspectives: that which is exposed to consumers of the class, and that which is exposed or available to subclasses. Members and fields declared private are every bit a part of descendant classes, even if they are "contractually" forbidden to be accessed by subclasses. You could make a crude analogy that a TV has a "public" interface of an on/off button, volume control, and color controls, but has "internal" controls not intended for the consumer such as the internal electronic components, or the power supply. They're still very much there even though they are not "visible" or "available" to consumers or subclasses.

Now, that said, there are constructs in most OO languages that reflect the properties you describe - multiple objects - and that involve a design pattern known as Composition (or, sometimes, Aggregation. This is where a class isn't derived from an ancestor class - typically because the class is declared "sealed" (C#) or "final" (Java) (or other designation that prohibits inheritance). That forces a developer interested in using the class to make it a member of another class, such that when the an object of the class is instantiated, you DO have discrete objects of both classes.

You might have the following:

public final class InterestingThing
{
    //definitions
    public InterestingThing()
}

public final class MyThing
{
    InterestingThing _interestingThing = new InterestingThing();

    public MyThing()
}

This is very much the kind of scenario you were describing in your original question, in which the creation of MyThing implies the distinct creation of an InterestingThing. Keep in mind, too this structure is generally forced by the design and definition of the original class of interest.

Ultimately, objects are defined by their classes, and multiply-inherited classes are still just a class, but in a refined, hopefully increasingly robust, hierarchy based on good, incremental object design.

I hope, in some way, this explanation helps to answer your question.

David W
  • 10,062
  • 34
  • 60
  • This question is tagged `java`. What is `sealed`? It's not a java keyword. Do you mean `final`? – Bohemian Sep 22 '12 at 19:07
  • My apologies. "sealed" is a C# keyword. I will change accordingly right now. Thanks for the catch. – David W Sep 22 '12 at 19:07
  • @DavidW Ohh no you didn't fail at all :) So, it's like the compiler aggregates the content of classes involved in inheritance (in whichever way it implements this ) and then make an object of this aggregated "new" class in the memory, while at the same time, respecting the wishes of the original author of the superclass for the accessibility of individual members. So, if someone asks the question whether or not private members are "inherited", we are going to say they are a "part" of the subclass but are not "inherited". – finitenessofinfinity Sep 22 '12 at 19:16
1

Class A "inherits" Class B. What does this actually mean?

If class A extends class B, it inherits all members (fields and methods) of B, i.e. class A will have these members even through they are not declared in the body of class A.

Whether class A is permitted to access a particular member is an unrelated matter.

An object of class A is created and then an object of class B is created. Java then somehow "link" the members of A to the members of B and make it appear as if they belonged to the same class and it does that according to the access specifiers.

No. A single object of class A is created. That object just happens to have all inherited members, too. For instance, if you have:

class B {
    private int x;
}

class A extends B {
    private int y;
}

The runtime will store, for every object of class A, the value of x and the value of y.

That code in class A does not access the value of x is enforced by verifying that the source code of A does not use the name x upon compilation, and by verifying that the bytecode of A does not refer to the field x upon loading the class at runtime. Put differently, access modifiers are independent of memory layout.

meriton
  • 68,356
  • 14
  • 108
  • 175
  • "All members" include the private members, is that right? If that's the case, then it all boils down to the interpretation of the word "inherits". As the JLS states -> "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." Thus, it seems that according to their interpretation of "inherits", the definition requires the property of "having" AS WELL AS "allowed to access". – finitenessofinfinity Sep 23 '12 at 05:01