473,662 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Private Members Not Inherited!

22 New Member
Beginners beware!

Usually in lessons on inheritance, we learn that when a class extends another, the former inherits all the members of the latter. This statement is only partly true (in fact it is misleading!). The complete truth is that private members are not inherited.

I wish to restate the principle: "The access modifier 'private' is not just an access modifier. It also means that the member who has this modifier will not be inherited."
Feb 6 '08 #1
9 6456
r035198x
13,262 MVP
Beginners beware!

Usually in lessons on inheritance, we learn that when a class extends another, the former inherits all the members of the latter. This statement is only partly true (in fact it is misleading!). The complete truth is that private members are not inherited.

I wish to restate the principle: "The access modifier 'private' is not just an access modifier. It also means that the member who has this modifier will not be inherited."
That's why you should be particular about the books/tutorials you read. A general rule of thumb is to try the horse's mouth first.
Feb 6 '08 #2
ghd
22 New Member
That's why you should be particular about the books/tutorials you read. A general rule of thumb is to try the horse's mouth first.
Hey! wait a minute. I did look up the reference you mentioned. I think private members are inherited after all! I am quoting from The Java Tutorials:
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.

If the derived class can access the private member of its parent through an inherited public method does it not imply that the derived class does indeed also have the private member as its own? I wrote a simple java program to illustrate this principle. See below (-:
Expand|Select|Wrap|Line Numbers
  1. class Animal {
  2.     private String name;
  3.  
  4.     String getName() {
  5.         return name;
  6.     }
  7.  
  8.     void setName(String n) {
  9.         name = n;
  10.     }    
  11. }
  12.  
  13. class Cat extends Animal {
  14.     public static void main(String[] args) {
  15.         Cat aCat = new Cat();
  16.         aCat.setName("Catalina");
  17.         System.out.println("Hi cat! " + aCat.getName());
  18.     }
  19. }
The result of running this is...
Hi cat! Catalina

If the private member 'name' of the class 'Animal' is not a member of the class 'Cat' then where is the string 'Catalina' getting stored? Hence it makes sense to conclude that 'name' is also a member of 'Cat', although it cannot be accessed using the '.' notation.

What do you think?
Feb 6 '08 #3
JosAH
11,448 Recognized Expert MVP
If the private member 'name' of the class 'Animal' is not a member of the class 'Cat' then where is the string 'Catalina' getting stored? Hence it makes sense to conclude that 'name' is also a member of 'Cat', although it cannot be accessed using the '.' notation.

What do you think?
Yep that's correct. It isn't much though: what would be the reason for having a
private member that gets exposed to the world through public accessors and
mutators? (getters and setters).

The reason is hiding the implementation details of the class. As a 'side effect'
a subclass can access the private member of the super class just as the entire
world can do.

kind regards,

Jos
Feb 6 '08 #4
ghd
22 New Member
Yep that's correct. It isn't much though: what would be the reason for having a
private member that gets exposed to the world through public accessors and
mutators? (getters and setters).
So can we restate the principle: Private members are inherited but cannot be accessed in the subclass using the "." notation.

kind regards,
ghd
Feb 6 '08 #5
JosAH
11,448 Recognized Expert MVP
So can we restate the principle: Private members are inherited but cannot be accessed in the subclass using the "." notation.

kind regards,
ghd
Yup you can think of it that way; I prefer to think of those private members as
space taken up in the subclass objects that can not be reached in the subclass
objects without things discussed in this thread before (at least protected getters
and/or setters).

This raises the question: should a class meant to be subclassed have private
data members? I'd say yes but I'm open for discussion about this.

kind regards,

Jos
Feb 6 '08 #6
krishnabhargav
24 New Member
Private members are not inherited.

Being able to access private members through accessor methods like getters and setters do not mean they are inherited but cannot be accessed with a "." operator.

Inheritance has to do with the visibility of the members. Take this case, if you have a private method say doSomething(), you cannot invoke it from subclass. If it was inherited there should be a way to invoke it, right? It does not make sense to write a new method in the super class like

public void pleaseDoSomethi ng()
{
doSomething();
}

Remember there is a reason for having "protected","pr ivate" and "public" modifiers.
Feb 7 '08 #7
ghd
22 New Member
This raises the question: should a class meant to be subclassed have private
data members? I'd say yes but I'm open for discussion about this.

kind regards,

Jos
I find one good reason for having private members in a class irrespective of whether it can be subclassed or not. The reason is that the author of the class may not want other classes (including the subclasses) to get or set the member by mere assignment operator. In other words, the author may want to run some code whenever the member is got or set.

Another reason could be, as JosAH has pointed out - encapsulation - confirming to certain conventions like the ones we follow when designing a java bean. In this case the introspection process looks for methods that begin with 'set' and 'get'. Please correct me if i am wrong.

Inheritance has to do with the visibility of the members. Take this case, if you have a private method say doSomething(), you cannot invoke it from subclass. If it was inherited there should be a way to invoke it, right?
The fact is that a class indeed has (owns) the private members of its parent. You can see for yourself by trying out the code that i showed before in this thread. Now, whether you call such behaviour as 'inheritance' or otherwise left for leaders of the Java community to decide.

kind regards,

ghd
Feb 7 '08 #8
krishnabhargav
24 New Member
I find one good reason for having private members in a class irrespective of whether it can be subclassed or not. The reason is that the author of the class may not want other classes (including the subclasses) to get or set the member by mere assignment operator. In other words, the author may want to run some code whenever the member is got or set.

Another reason could be, as JosAH has pointed out - encapsulation - confirming to certain conventions like the ones we follow when designing a java bean. In this case the introspection process looks for methods that begin with 'set' and 'get'. Please correct me if i am wrong.



The fact is that a class indeed has (owns) the private members of its parent. You can see for yourself by trying out the code that i showed before in this thread. Now, whether you call such behaviour as 'inheritance' or otherwise left for leaders of the Java community to decide.

kind regards,

ghd
so I inherit a JFrame, so I own the private members? Also Java community leaders do not decide what inheritence is. It has been around right since the 60's (thats when Gosling was probably in primary school at that time).

Try this out.

Expand|Select|Wrap|Line Numbers
  1. class Super
  2. {
  3.     private Super()
  4.     {
  5.  
  6.     }
  7. }
  8. class Sub extends Super
  9. {
  10.  
  11. }
  12.  
It is an error...
Feb 7 '08 #9
BigDaddyLH
1,216 Recognized Expert Top Contributor
Alright, all this hullabaloo about private+inherit s made me crack open my link to the JLS, to the chapter on classes: http://java.sun.com/docs/books/jls/t...s.html#8.2.1.3

Here are some relevant quotes on private+inherit s (and a few on constructors):
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.
Constructors, static initializers, and instance initializers are not members and therefore are not inherited.
A class inherits from its direct superclass and direct superinterfaces all the non-private fields of the superclass and superinterfaces that are both accessible to code in the class and not hidden by a declaration in the class.
Note that a private field of a superclass might be accessible to a subclass (for example, if both classes are members of the same class). Nevertheless, a private field is never inherited by a subclass.
It is a compile-time error for a private method to be declared abstract. It would be impossible for a subclass to implement a private abstract method, because private methods are not inherited by subclasses; therefore such a method could never be used.
Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.
Feb 7 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

19
2332
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
2
2515
by: MJ | last post by:
Hi I have a following sample code class base and class derived. I have inherited the base class as private and tried to compile the code its giving an error "conversion from 'class derived *' to 'class base *' exists, but is inaccessible" If I inherit using public it works well ... I am not very clear about the private inheritance
8
7827
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By scope they meant public/protected/private access modifiers :) Obviously all members of the base privately inherited class will be private, and that was my answer. However, the teacher checked my answers when I handed in, and the problem was that I had...
2
1509
by: Bob | last post by:
Is this a bug? Expected behavior? Did I get the binding flags wrong? Bob -------------------- Imports System.Reflection Module Main
2
2971
by: lovecreatesbeauty | last post by:
I'm disturbed on this question on a long time. I think if I finally get understand it with your kind help, I will get close to a excellent C++ programmer. And I only can rely on your expertise and help. I've read some books, but no book focuses on this. Don't you think it is a very important thing of C++ programming language? So your knowledge is of great benefit to others, especially me. 1. Which (How many) members will be created...
3
332
by: bob | last post by:
is this true of private inheritance? public stuff becomes private private stuff becomes inaccessible to the new class protected stays protected and is accessible to the new class
6
1701
by: Siam | last post by:
Hi all, I read somewhere that private members of some instance of class A are accessible from another instance of the same class. In other words, access to private members is done 'on a class by class basis, not on an instance by instance basis'. I couldn't quite believe it, and tried it out for myself, and was shocked to see it was true... What is the reasoning behind this? Why should one Person object be able to see another Person...
14
2626
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming Language, but there isn't a detail and complete description on all the class members, aren't they important to class composing? Could you explain the special class behavior in detail? Thank you very much.
13
1718
by: PragueExpat | last post by:
I (think) that I've come up with a pattern that I haven't seen in any publications so far and I would like some feedback. Basically, I was looking for a way to inherit private functions and I came up with this: //base private object constructor function priv(){ this.a = 1; this.b = 2;
0
8435
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8547
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1754
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.