Java .

Java Accessability

The four types of Accessability for 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: accessability has the same basic rules for classes, methods, and variables.

Test class "Cat"

package com.fluffycat.feline;

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

Test class "Siamese"

package com.fluffycat.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.fluffycat.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.fluffycat.canine;

import com.fluffycat.feline.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.fluffycat.canine;

import com.fluffycat.feline.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 } }
Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate.
Comment by brknt on 2013-05-22 Rate this Comment

Thank you. They are really good examples, however, I guess there are little mistakes in "Test class "Dog"" and "Test class "SmallCatLikeDog": the line "import com.fluffycat.cat.Cat;" should replace with the line "import com.fluffycat.feline.Cat;".

 
Sign in to comment on Java Accessability.