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

Private variables and get/set

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
I'm a little bit confused about this:
I know that private istance variables are non heritaged but public method yes.
So if I had two classes like this:






Of course, I can use setName() e getName() on Employee, but why I can't use the notation emp_1.name ?
Employee owns or not the variable name ?
Why I can access directly to name in Person but not in Employee ? What's the point?
I know it's a primer question but I'm really confused about this!

Thanks!



 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alessandro Camel wrote:Of course, I can use setName() e getName() on Employee, but why I can't use the notation emp_1.name ?
Employee owns or not the variable name ?


Because the member variable name is private in class Person - this means you can only access it directly from class Person. If you would make it protected instead of private, you could also access it from class Employee. See Controlling Access to Members of a Class in Sun's Java tutorials.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public : Accessible everywhere.
protected : Accessible by any class in the same package as its class, and accessible only by subclasses of its class in other packages.
default : Only accessible by classes, including subclasses, in the same package as its class (package accessibility).
private : Only accessible in its own class and not anywhere else.

so you cant access the private varibale .

let me know if you any quires .
 
Alessandro Camel
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swapnl patil wrote:
public : Accessible everywhere.
protected : Accessible by any class in the same package as its class, and accessible only by subclasses of its class in other packages.
default : Only accessible by classes, including subclasses, in the same package as its class (package accessibility).
private : Only accessible in its own class and not anywhere else.

so you cant access the private varibale .

let me know if you any quires .



OK, the modifiers are clear, I think my problem it's a OO problem.
Employee IS-A person, so it must have a name.
I understand the encapsulation in this way: for example, in the set method I can insert all input validation checks and I avoid that somewhere
I could have something like person.name = "-1".

What I don't get is this :" if private variables are not inherited, in this case, how an Employee can have a name ?"
What point am I missing???



 
swapnl patil
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, Employee is a person.

so your employee class can use the setter & getter method of person class to set a name & get a name.
this is what inheritance is.



so for emp_1 , his name is "Marcus" & his salary is 5000.

whatever you done is right.

always expose your variable to other through setter & getter method. we call ed this as a encapsulation.People always prefer this way so that other people code will never break. Take example of this person class. Your variable name is "name" . tomorrow you want to change this to personName. so if you expose it directly then employee class will break. so just change person code & other people don't need to change their code.

I hope this will help you.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alessandro Camel wrote:OK, the modifiers are clear, I think my problem it's a OO problem.
Employee IS-A person, so it must have a name.

What I don't get is this :" if private variables are not inherited, in this case, how an Employee can have a name ?"


An Employee does have a name, but it's hidden in the Person part of the Employee object. Member variables and methods that are private, are only accessible inside the class itself - they are hidden from the outside, even from subclasses. By making the variable 'name' private in class Person, you are expressing that nobody outside class Person, not even the subclass Employee, needs to access the variable directly.

That's often beneficial (the principle of encapsulation): it means that the code in which the name variable is accessed, is limited to just class Person. If anything needs to change with regard to the variable (for example, you'd want to give the variable a different name or type), you only have to look in class Person and nowhere else, because you already know that class Person is the only place where 'name' is accessed directly. It makes your code easier to understand and easier to change in the future.

Subclasses in general don't need to know all the exact details of the inner workings of their superclasses.
 
Let's get him boys! We'll make him read this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic