• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

protected keyword

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[code]
package p1;

public class AccessModifiers{
int n_pack =1;
private int n_pri = 2;
protected int n_pro = 3;
public int n_pub = 4;

public AccessModifiers(){
System.out.println("Base Constructor");
System.out.println(" n_package = "+n_pack);
System.out.println("n_private = "+n_pri);
System.out.println("n_protected = "+n_pro);
System.out.println("n_public = "+n_pub);
}
public static void main(String[] args){
AccessModifiers am = new AccessModifiers();
}

}


package p2;

import p1.AccessModifiers;

class AccessModifiers03 extends AccessModifiers {
public static void main(String args[]){
AccessModifiers am = new AccessModifiers();
//System.out.println(" n_package = "+n_pack);
//System.out.println("n_private = "+n_pri);
System.out.println("n_protected = "+n_pro);
System.out.println("n_public = "+am.n_pub);

}

}
[\code]
i am getting error while printing n_pro even if it is declared as protected, please can you help me...
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The protected keyword means that the variable defined in super class will also be visible in the objects of subclass.

In your case there is a Static reference to the Non-Static variable "n_pro".

This will cause the compiler to throw an error. Also to print it in subclass you have to instantiate an object of subclass and call it in the format obj.variable.

See the modified code below for subclass:



Hope this helps
 
Marshal
Posts: 76432
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Suraj Berwal:
The protected keyword means that the variable defined in super class will also be visible in the objects of subclass.

. . . and to other classes in the same package.
 
Suraj Berwal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
. . . and to other classes in the same package.



Oh yess...

Thanks for the addition...

Kind Regards,
Suraj
[ March 12, 2008: Message edited by: Suraj Berwal ]
 
Let's go to the waterfront with this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic