• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem understanding private

 
charu shila
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Bond James",

Please check your private messages.

Thanks
 
Bridget Kennedy
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
charu shila
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic