wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes problem understanding private Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem understanding private" Watch "problem understanding private" New topic
Author

problem understanding private

charu shila
Greenhorn

Joined: Nov 24, 2001
Posts: 11
Hi All,
Reading about private here.It says that you can't use a private variable declared in your superclass in your subclass.I wrote a code :

class ParentClass{
private String name;

public ParentClass(){
this.name="sam";
}
public String getName(){
return name;
}
}

public class ChildClass extends ParentClass{

public String getFullName(){
return getName()+" de Souza";
}


public static void main(String[] args){
ChildClass cc=new ChildClass();
System.out.println(cc.getFullName());

}
}

Looking at the above code I can see that the ChildClass inherits the private variable and uses it to return the full name.

Can somebody please clear my doubt?
Regards
james
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

What you can't do is this:



You don't inherit it in the sense that you don't inherit the name of the variable; you can't use its name in any of the child class's code. It's still there, though, and code from the parent class can certainly still access it.


[Jess in Action][AskingGoodQuestions]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10905
    
  12

"Bond James",

Please check your private messages.

Thanks


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Bridget Kennedy
Ranch Hand

Joined: Nov 30, 2004
Posts: 84
I once had a Java instructor tell my class that "derived classes only inherit public and protected methods and data members from their base class". This is simply not true. What is true is that the derived class only has direct visibility to the base class public and protected methods and data members.

As EF-H suggests, a derived class inherits everything from its parent - and from all other classes in its inheritance hierarchy.
[ December 13, 2007: Message edited by: Bridget Kennedy ]
charu shila
Greenhorn

Joined: Nov 24, 2001
Posts: 11
Hi All,
Thanks Ernest and Bridget for your prompt reply.But the specification says that it is not inherited.Now I have modified the program thus

class ParentClass{
private String name;

public ParentClass(){
this.name="sam";
}
public String getName(){
return name;
}

public void changeName(String newName){
this.name=newName;
}
}
public class ChildClass extends ParentClass{

public String getFullName(){
return getName();
}

public void changeName(String changedName){
super.changeName(changedName);
}


public static void main(String[] args){
ChildClass cc= new ChildClass();
System.out.println(cc.getFullName());
cc.changeName("James Bond");
System.out.println(cc.getFullName());

}
}


Now if the private variable is not inherited then it is not a property of the subclass.But the program suggests that the private variable is a property of the subclass and changeable.

Regards
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[BK]: I once had a Java instructor tell my class that "derived classes only inherit public and protected methods and data members from their base class". This is simply not true.

[BJ]: But the specification says that it is not inherited.

Correct - the definition of "inherited" used in the specification is different from what Bridget is thinking of. In the JLS, a field or method is only considered inherited if it is accessible. That's how the term is defined. That doesn't mean that private fields from a superclass don't exist internally as part of the object - but if they can't be accessed directly in the subclass, the JLS does not refer to them as inherited.


"I'm not back." - Bill Harding, Twister
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Actually, what you are doing is proving how encapsulation works.

You can't touch the variable directly from the subclass. Your code is not doing that.... if you tried it would most likely not compile.

What you are doing is calling an accessor method that is public and it is returning a copy of the value of the variable to you. You can not manipulate it directly.

Encapsulation says, declare your variables as private and provide public "setter" and "getter" methods to get and set the value. You are using the getter to retrieve the value.
[ December 13, 2007: Message edited by: Bob Ruth ]

------------------------
Bob
SCJP - 86% - June 11, 2009
charu shila
Greenhorn

Joined: Nov 24, 2001
Posts: 11
Hi All
Thanks for all the replies.Now if we go here it says that the private field of a superclass might be accessible to a subclass if both classes are members of the same class .What does that mean?

Regards
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
What does that mean? It means you have strayed into complicated territory.

You can have class Foo with an inner class, Bar, and another inner class BarBar extends Bar. At least I think that is what it means!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem understanding private
 
Similar Threads
Overide Question
Why the interviewer challenged me?
How to get the class name of the subclass from a static method in the superclass?
Can we extend multiple classes ?
Compile time polymorphism