• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

problem understanding private

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13047
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Bond James",

Please check your private messages.

Thanks
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 75617
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic