• Post Reply Bookmark Topic Watch Topic
  • New Topic

accessibility and inheritance  RSS feed

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class A {

private String name;
private String address;

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

}

public class B extends A {

private String phone;
..
..
}

In class A, attributes "name" and "address" are all "private", so they are not supposed to be accessible in class B. But are they still inherited by class B ? i.e. Does an object of B have varibles of "name" and "address" ? If yes, what does it mean that you can inherite variables but they are not accessible to you ?
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Accessibilty and Visibility are fungible but they are different from "existence". If the method or field is visible(via a reference or directly) it is accessible.

Private members of a class are "visible" only within the class where they are declared(security reasons).Private members of a class are not "inherited" by sub class. Reading JLS 8.2 and JLS-Example 6.6.8 might help here.

As regards physical existence of private fields, when you create an instance of sub class, i think that the memory is allocated for private member of super class. For Rationale , check this code(and comments!):



Note that "obj" and "this" refer to same Derived class object. Yet, different entities are visible/accessible to them depending on the context in which they are being used.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am bit confused. Look at the following example

class A {
private String name;
public void setName(String name) { name = name; }
public String getName() { return name; }
}

class B extends A {
private String title;
..
}

public class Test {
public static void main(String[] args) {
B b = new B();
b.setName("Martin");
System.out.println("b's name is " + b.getName());

}
}

You can see it prints "b's name is Mark". So, isn't "name" (a private variable of class A) inherited by class B ? otherwise, why can object b (type of B) prints out its name ? when you do b.setName("Martin"), doesn't mean "martin" is set as object b's object ?? See, I have never instantiated an object for class A. It seems "martin" belogs to "b" object. Confused, please help clarify.

thanks
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben oliver wrote:In class A, attributes "name" and "address" are all "private", so they are not supposed to be accessible in class B. But are they still inherited by class B ? i.e. Does an object of B have varibles of "name" and "address" ? If yes, what does it mean that you can inherite variables but they are not accessible to you ?

In this case, accessor and mutator (getter and setter) methods are inherited because they are public. It's not strange behavior. Are you clear with Inheritance?
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And yes Do lots of code practice with Inheritance and Polymorphism. They are really tricky.

Please use CODE tags to enclose any code. It makes easy to read.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class AA {
private String name;
public void setName(String name) {

System.out.println(this);
this.name = name;
}
public String getName() {
return this.name;
}
}

class BB extends AA {
private String id;
}


public class Test2 {
public static void main(String[] args) {
BB b = new BB();
System.out.println(b);
b.setName("steve");
System.out.println("b's name is " + b.getName());

}
}

Here is the print out:

BB@360be0
BB@360be0
b's name is steve

This means it is object "b" who calls inherited "setName" method, and you can see, when I do "this.name = name", it sets "b"'s name field, so object b can acces "name" !!

Can you explian ?

 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here is your code with tag. It makes sense to use it.

ben oliver wrote:This means it is object "b" who calls inherited "setName" method, and you can see, when I do "this.name = name", it sets "b"'s name field, so object b can acces "name" !!

Correct! b can access name of class AA. But it is being accessed using setter method (which has public visibility).
try doing something like this, b.name = "ben oliver";

Here, you have used encapsulation.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Let me try,

For Object reference b , "name" is not accessible, but for setName() methods is accessible, and for setName() , "name" is accessible. Hence, b can access "name".
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Pandya wrote:Ok. Let me try,

For Object reference b , "name" is not accessible, but for setName() methods is accessible, and for setName() , "name" is accessible. Hence, b can access "name".


you said "For Object reference b , name is not accessible", but look at my last example -- It shows the same "hashcode", that means, when you execute
"this.name = name", who is the "this" object ? based on my printout, it is the object "b" !

so doing this.name is exactly like doing "b.name" because the "this" shows the same hashcode and object "b" ! I underatnd what you said about accessing through public setter and getter. But please explain what confused me here.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was a similar topic discussed here earlier this week.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,
I have tried my best to convince you. But this is my limit I am sorry. As I have already suggested, please clear Inheritance and Encapsulation.

Anybody else please?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what vishal was trying to say is, you can't just say..

b.name = "steve";

you have to use the inherited 'setName(String name)' to access it's private instance variable(s).

When you extend a class you inherit all public/protected variables/methods as well as that classes
classes and so on.

I'm not sure about instance variables, but I would think that if it were public or protected the extendee would
inherit those instance variables also..

help me out if I wrong,

Thanks,

Justin
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,

You are confusing accessibility(visibility) with existence(in memory).

Access specifiers should be seen as means for enforcing security. You simply dont want private data members of a class to be tampered with outside of its class. This statement b.name = "steve" is therefore a violation of the security constraints imposed by the (super) class. Rule says, "Private data members are not inherited and are not accessible outside of the class in which they are declared". Private defines the context in which the field will be accesible. Paraphrase, this means that private fields are not directly accessible from sub classes. It doesnt imply that sub class object will not have this field in their objects memory.

What is a child Object after all?
When you say new Derived(), you are creating a single child object but you must have noticed the fact that constructor for super class is also called which, therefore, means that fields of parent class are also allocated memory and initialized.

So, the field will be there in memory when you create a child instance, but it simply wont be accessible by any code executing outside the parameters of the super class.

I hope I was able to (express my self here and) answer your question!

Cheers!

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Now let me just ask a simple question, for the example code I mentioned,

derived class object b can access private attributes of its super class via super class' public setter/getter methods, cool. Now when I do

B b = new B(); // B is the derived class of super class A
b.setName("john");
b.getName();

Now, couple questions --

1. when I do new B(), certainly I created an instance of class B in memory. Do I also create a specific instance of class A in memory ???

2. "name" is an instance varibale, when I do

b.setName("john");
b.getName();

to set the value for "name" field, do I set this variable value for object b ? or do I actually set this variable value for an object of class A ?? Which object does this "name" belong to exactly ??
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben oliver wrote: Now, couple questions --

1. when I do new B(), certainly I created an instance of class B in memory. Do I also create a specific instance of class A in memory ???

NO.And
If you really understand the Inheritance show me the object template(available variables,methods) when you do
B b = new B();

ben oliver wrote:do I set this variable value for object b ? or do I actually set this variable value for an object of class A ?? Which object does this "name" belong to exactly ??
If you have created object of class B then obviously it will set for object b only. Now don't say that it is private and how come it accessible in class B. It is accessible through getters/setters.
If you really understand the Inheritance show me the object template(available variables,methods) when you do
B b = new B();

If you really understand the Inheritance show me the object template(available variables,methods) when you do
B b = new B();

If you really understand the Inheritance show me the object template(available variables,methods) when you do
B b = new B();
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal,

1. You said when object b is created, it does NOT create an instance of its super class A. OK, Let me ask you this, when you do new B(), java automatically calls its super class' constructor implecitly, right ? So, when JVM does that new A() behind the scene, how can you say there is not an instance of class A get created when it does "new A()" ?

2. Follow what you said, only object b of class B is created and no instance of class A created, ok. But you did NOT explicitly answer my question --

when I do b.setName("john"); I set the value for attribute "name", so is "name" an attribute of object b ?? Please say YES or NO, no need to tell me the public setter/getter again, thanks. if you think "name" is an attribute of object b, then tell me how can object b have such an attribute since object b does not inherite this attribute at all. If it is NOT attribute of object b, then whose attribute it is (remember it is NOT for class A's object either since you said no class A object is ever created) ?

 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben oliver wrote: Now, couple questions --

1. when I do new B(), certainly I created an instance of class B in memory. Do I also create a specific instance of class A in memory ???

2. "name" is an instance varibale, when I do

b.setName("john");
b.getName();

to set the value for "name" field, do I set this variable value for object b ? or do I actually set this variable value for an object of class A ?? Which object does this "name" belong to exactly ??


1. yes, when you instantiate a child object you also create an instance of super class object.

2. Since, "name" was a field defined in the super class(template) it should be a member of super class object(Object A).

That said, also ask yourself this question...What is(or constitues) a child object?
A child object is a parent object plus something extra.So, Object B, in memory, can be viewed as collection of all the fields and methods of Object A plus those defined in Class B.
Using access specifiers we define security and control the context in which these fields/methods will be accessible.

I hope you get the picture.

Aloha!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!