Sullivan Market

a web based full-circle development system



My Java Tutorials - Accessibility


Here is some example code.... Copy and paste it to your page if you wish to use it.


The four types of Accessibility of classes, methods, and variables


public - Available to all classes.
protected - Available to the package & subclasses.
package - (aka friendly) (implied default, no modifier) - Available to package
private - Available only to own class, not inherited.
Note: accessibility has the same basic rules for classes, methods, and variables.


Test class "Cat"

package com.projt1.feline;

class Cat {
public String publicString = "public";
protected String protectedString = "protected";
String packageString = "package";
private String privateString = "private";
}


Test class "Siamese"

package com.projt1.feline;

class Siamese extends Cat {
public void findOutAboutCat(Cat catToTest) {
String testPublic = catToTest.publicString;
String testProtected = catToTest.protectedString;
String testPackage = catToTest.packageString;
//String testPrivate = catToTest.privateString; !can't access this
}
}


Test class "Cheeta"
package com.projt1.feline;

class Cheeta {
public void findOutAboutCat(Cat catToTest) {
String testPublic = catToTest.publicString;
String testProtected = catToTest.protectedString;
String testPackage = catToTest.packageString;
//String testPrivate = catToTest.privateString; !can't access this
}
}


Test class "Dog"

package com.projt1.canine;

import com.projt1.cat.Cat;

class Dog {
public void findOutAboutCat(Cat catToTest) {
String testPublic = catToTest.publicString;
//String testProtected = catToTest.protectedString; !can't access this
//String testPackage = catToTest.packageString; !can't access this
//String testPrivate = catToTest.privateString; !can't access this
}
}


Test class "SmallCatLikeDog"
package com.projt1.canine;

import com.projt1.cat.Cat;

class SmallCatLikeDog extends Cat{
public void findOutAboutCat(Cat catToTest) {
String testPublic = catToTest.publicString;
String testProtected = catToTest.protectedString;
//String testPackage = catToTest.packageString; !can't access this
//String testPrivate = catToTest.privateString; !can't access this
}
}

Back To Java Tutorials

©2012 SullivanMarket.com