| Author |
members with private specifier can be inherited?
|
David Wong
Greenhorn
Joined: Dec 31, 2007
Posts: 15
|
|
If private member variables can be inherited from the base class?I read some books describes as NO,they thought one class should be inherited all methods and attributes from the base class,otherwise there is an entire duplication from the base class in the derived class.I think the derived class has a copy of based class,it just can not be accessed when the base class member variables specified as private? public abstract class PhoneCard { private Long id; private String password; private Double balance; private String digits; public PhoneCard() {} public PhoneCard(Long id,String password,Double balance,String digits) { this.id = id; this.password = password; this.balance = balance; this.digits = digits; } //... } public class TwoHundredCard extends PhoneCard { public TwoHundredCard() {} public TwoHundredCard(Long id,String password,Double balance,String digits) { super(id,password,balance,digits); //methods was inherited from base type,but the private fields was not } //... @Override public void priceCount() { System.out.print("connection successful established ..."); this.balance -= 0.5;//balance can't be accessed, //but there is a whole copy of PhoneCard } } yours friend!
|
Bitter Java For Better Life.
|
 |
Anubhav Anand
Ranch Hand
Joined: May 18, 2007
Posts: 341
|
|
Daivd Wang, Welcome to JavaRanch. To avoid all confusion lets take it this way. When a class is inherited all the memebers of the base class are inherited by sub class. Now, the importatnt thing to know is visibility. Private memebers are visible only in the class they are defined. Thus, the private members are not visible in the sub class. So, we may say as private members are not inherited at all because they are visible only in the class they are defined in. The only way private variables of base class can be altered is if there are public getters and setters. Hope that helps.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24040
|
|
|
Everything that Anubhav Anand says is correct, but you should be aware that documentation from Sun generally says that "private members are not inherited", and that's intended to mean the same thing. Personally, I've always disagreed with this way of describing the situation -- it's needlessly confusing. But still, it's important to understand that if someone says "private members are not inherited," you should not argue with them; they're correct (at least for the official definition of "inherited" in Java.)
|
[Jess in Action][AskingGoodQuestions]
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
I do agree with EFH. Yes, whoever says so they also "mean" the same in the context of "inheritance" and that's indeed correct. It is all about the visibility and then accessibility.
|
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
|
 |
David Wong
Greenhorn
Joined: Dec 31, 2007
Posts: 15
|
|
|
thanks for information.thank you everyone pays your passion.
|
 |
Anna Hazare
Greenhorn
Joined: Mar 28, 2011
Posts: 2
|
|
For all these years I was mislead by the words in almost all the text books i referred. Today I realised while doing an assignment, that a copy of private members is also made. This forum confirmed it. with this I can say, I am still working on my basics....
Hope to improve through this forum. Thanks all for the posts
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
daffodil silver wrote:For all these years I was mislead by the words in almost all the text books i referred. Today I realised while doing an assignment, that a copy of private members is also made. This forum confirmed it. with this I can say, I am still working on my basics....
Hope to improve through this forum. Thanks all for the posts
I don't understand what you mean about "copy of private members"; please explain more fully.
|
 |
Anna Hazare
Greenhorn
Joined: Mar 28, 2011
Posts: 2
|
|
|
Objects of a class, when instantiated get their own set of instance variables, also called properties. When a private variable is initialized with a value, at its declaration step in a super class, then all sub classes which inherit from that superclass, will also copy the value of the instance variable initialized in super class. I guess, better term will be to say the instance variables are also initialized to the value mentioned in the super class declaration statement.
|
 |
 |
|
|
subject: members with private specifier can be inherited?
|
|
|