• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MOCK help

 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am feeling MOCK answer is incorrect.On object o1 of class A member of class B can be available only thorough inheritance not using dot.Please help which one is correct mine or mock.
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shalini,

Mock answer seems to be correct. Because if you don't specify any access specifier it's default i.e package level. Within the same package it can be accessed by any means. Doesn't matter whether it's inherited or not. Correct me if I am wrong.
 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the object o2 of class B in class A we can access class B members without inheritance using dot but without inheritance how members of class B would be accessible on object o1 of class A ?
 
Roel De Nijs
Sheriff
Posts: 10591
140
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A little bit code to show the mock answer is absolutely correct. Class A accesses a member (without access modifier) of class B. class A and B are in same package and no inheritance is needed.

This code compiles successfully.




Move class B to another package and see what happens...
 
Roel De Nijs
Sheriff
Posts: 10591
140
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shalini Srivastav wrote:but without inheritance how members of class B would be accessible on object o1 of class A ?

By simply creating an instance, like shown in my previous post.
 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My imagination was ..
 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you think same ?
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the above code has go anything to do with access specifier. It won't compile only because the variable is not member of that class.
 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:I don't think the above code has go anything to do with access specifier. It won't compile only because the variable is not member of that class.


Yes, so it will be possible only through inheritance in same pkg,which was my answer.
 
Roel De Nijs
Sheriff
Posts: 10591
140
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shalini Srivastav wrote:Yes, so it will be possible only through inheritance in same pkg,which was my answer.

That's indeed true for the code sample you have created,

But lines 12 and 13 from your code snippet show that you can access from class A a pacakge-private (no access modifier) member of class B. If you need to access the member of class B through an instance of class A your answer (same package + inheritance) would be correct
 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Shalini Srivastav wrote:Yes, so it will be possible only through inheritance in same pkg,which was my answer.

That's indeed true for the code sample you have created,

But lines 12 and 13 from your code snippet show that you can access from class A a pacakge-private (no access modifier) member of class B. If you need to access the member of class B through an instance of class A your answer (same package + inheritance) would be correct


I got confused because i thought that there must be some use of object o1 of class A because it is mentioned so i was using it to access class B's members on it using inheritance but i think it has no mean here.We are simply accessing members on Object o2 of class B in class A.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic