• Post Reply Bookmark Topic Watch Topic
  • New Topic

members with private specifier can be inherited?  RSS feed

 
David Wong
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Wong
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for information.thank you everyone pays your passion.
 
Anna Hazare
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!