• 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

Access Modifiers

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Could you pls tell me whats wrong in my code ? Any help really appreciated.

package spack;
class A
{
public int a = 10;
protected int b = 20;
int c =30;
private int d = 40;
public void printa()
{
System.out.println("value of public variable in Class A is " + a);
System.out.println("value of protected variable in Class A is " + b);
System.out.println("value of default variable in Class A is " + c);
System.out.println("value of private variable in Class A is " + d);
}
}

public class B
{

public void printab()
{
System.out.println("value of public variable from class A in class B is " +a);
System.out.println("value of protected variable from class A in class B is" + b);
System.out.println("value of default variable from class A in class B is " +c);
System.out.println("value of private variable from class A in class B is " +d);
}

public static void main(String args[])
{
B bex = new B();
bex.printab();

}
}


when I compiled, it gave the following errors......

Cannot find symbol a,b,c and d

Waiting for your Answers with explanation
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you meant to make B a subclass of A, but you still can't see d in B if you do.
 
Sathya Shanmugam
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith for your reply,

but I want to write a code to test (from KS scjp book)

Determining access to Class Members

visibility Public Protected Default Private
From any class
in the same package yes yes yes No


Its working fine for extended class (subclass)

Need help...
 
Sathya Shanmugam
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From any class inside the same package not using inheritance

Public ----- yes
Protected ----- yes
Default ----- yes
Private------- NO
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since d is a private variable in A, it is not inherited by subclasses, and not visible to classes other than instances of A.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks to me like you're trying to access variables of a class of which you do not have an instance.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It think if you modify the program as follows you can test access modifiers as you intend to do..

package spack;
class A
{
public int a = 10;
protected int b = 20;
int c =30;
private int d = 40;
public void printa()
{
System.out.println("value of public variable in Class A is " + a);
System.out.println("value of protected variable in Class A is " + b);
System.out.println("value of default variable in Class A is " + c);
System.out.println("value of private variable in Class A is " + d);
}
}

public class B
{
A a = new A();
public void printab()
{
System.out.println("value of public variable from class A in class B is " +a.a);
System.out.println("value of protected variable from class A in class B is" + a.b);
System.out.println("value of default variable from class A in class B is " +a.c);
System.out.println("value of private variable from class A in class B is " +a.d);
}

public static void main(String args[])
{
B bex = new B();
bex.printab();

}
}
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, there will still be the same problem with d. It is private to A.
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right...
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more problem Might be using same variable a.

I guess we can use

A a1 = new A();
instead of
A a = new A();
 
Sathya Shanmugam
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joshi and Keith

Now its working well without using inheritance as per your code when I create a Object for Class A in class B.
 
reply
    Bookmark Topic Watch Topic
  • New Topic