aspose file tools*
The moose likes Beginning Java and the fly likes Private variables and get/set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Private variables and get/set" Watch "Private variables and get/set" New topic
Author

Private variables and get/set

Alessandro Camel
Ranch Hand

Joined: Jul 03, 2009
Posts: 35
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!



Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
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

Joined: Jul 03, 2009
Posts: 35
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

Joined: Aug 13, 2007
Posts: 80
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Private variables and get/set
 
Similar Threads
Polymorphism and useBean
jaxb marshalling problem
Clarifications/Suggestions in Java
tester class and then some.
access to superclass's private instance variables