• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

inheritance and private instance variables

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If private class members (instance variables and methods for example) are not inherited, then if I have a class who's superclass has public getter and setter methods with a private instance variable, then how does the subclass call a setter method on an instance variable that it does not inherit?

Example:


public class A {
private String name;

public void setName (String name) {
this.name = name;
}
}

--------------

public class B extends A {

private String name2;

public void setName2(String name) {
this.name2 = name;
}
}

--------

Now, when I say

....
String myName = "Blah";
B b = new B();
b.setName(myName);
....

How does that work if name (from class A) is not extended to B since it's private but the setter is public? I must be missing something....

Thanks,
Scott
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the subclass is aware of the private member of the superclass, but it can't access it directly. But the public getter methods from the superclass allow it to interact with its private members.
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic