Logout

Home OOP Option Last Next

D.3.3

Define the terms: private, protected, public, extends, static.

 

Teaching Note:

These are generally related to the OOP features described in D.2. See JETS.

 

Sample Question:

sdfsdfsf

JSR Notes:

Introduction

Note that this can be treated on two levels; one is a basic definition kind of understanding, but one is quite deep, particularly with static, but also extends, and to a lesser extent protected. But to take all three of these to coding understanding is a lot. But understanding of them is good stuff for helping to grasp many simpler OOP coding principles.


Basic Definitions

private - (We'll assume Java-specific implementations for all of these.) A keyword which limits the access of an attribute or method to be within that class only.

protected - A keyword which limits the access of an attribute or method to be within a package. So one class in a certain package can access that attribute or method if and only if it is in the same package.

public - A keyword which declared (explicitly) that an attribute or method can be directly accessed by any other class. If private, nor protected, nor public is written, public is assumed by default.

extends - A keyword which indicates that a class inherits from another class all of its attributes and methods.

static - A keyword which limits the attribute or method to being only one instance; i.e. multiple instances of it cannot be made. This is in contrast to any attributes or methods of "template" classes which by the very nature of being template classes can be made multiple instances. Things called from static methods (such as what main always is) must also themselves be static.

 

More Detailed Definitions and Coding Examples

Private, Protected & Public

private - (We'll assume Java-specific implementations for all of these.) A keyword which limits the access of an attribute or method to be within that class only.
So this is a level of access that prohibits other classes to access a particular attribute or method. Attributes should always be private in true Object Oriented Programming programing, so as to obey the concept of encapsulation. Often methods are also made private, since they are only intended to be used within that class as "helper methods".

protected - A keyword which limits the access of an attribute or method to be within a package. So one class in a certain package can access that attribute or method if and only if it is in the same package.
So this is a level of access that permits access only within a given package. We don't often use protected because we don't often use multiple packages. But if/when you do in your dossier or beyond, it's a good idea to use protected for your methods, but still not your attributes to keep them encapsulated.

public - A keyword which declared (explicitly) that an attribute or method can be directly accessed by any other class. If private, nor protected, nor public is written, public is assumed by default.
So this is a level of access that permits access from anywhere else to the particular attribute or method. We want to use public for everything that other classes can see, and so the methods we intend to be used by other classes. All other attributes and methods should be private and protected. One other note is that class definitions themselves also use the public keyword.

 

1    package zooanimals; 
2     
3    public class Zebra extends ZooAnimal{ 
4        private int numStripes;  
5        //private, so can only be accessed in *** this class *** 
6     
7         
8        public Zebra(){ 
9             
10       } 
11       //public, so can be accessed by any class in the *** whole project *** 
12    
13        
14       protected int getNumStripes() { 
15           return numStripes; 
16       } 
17       //protected, so can be accessed by any class in the *** same package *** 
18   }

1    package zooanimals; 
2     
3    public class MainForZoo { 
4        public static void main(String[] args) { 
5            Zebra zoeTheZebra = new Zebra(); 
6            //accessible because the Zebra() constructor is public 
7     
8     
9            System.out.println(zoeTheZebra.getNumStripes()); 
10           //ACCESSIBLE because getNumStripes() is protected, 
11           //and we're in the same package, zooanimals. 
12    
13    
14           System.out.println(zoeTheZebra.numStripes); 
15           //not possible, since numStripes is private in the Zebra class 
16       } 
17   }


import zooanimals.Zebra; 
2     
3    public class AnotherMain { 
4        public static void main(String[] args) { 
5            Zebra zoeTheZebra = new Zebra(); 
6            //accessible because the Zebra() constructor is public 
7     
8     
9            System.out.println(zoeTheZebra.getNumStripes()); 
10           //NOT accessible because getNumStripes() is protected, 
11           //and we're NOT in the same package. 
12    
13    
14           System.out.println(zoeTheZebra.numStripes); 
15           //not possible, since numStripes is private in the Zebra class 
16       } 
17   }

 

Extends as Used in Inheritance

extends - A keyword which indicates that a class inherits from another class all of its attributes and methods.
Here is a quick example of a couple of classes which uses extends to inherit from another class:

The Super Class

1    package shoesmanufacturing; 
2     
3    public class ShoeMaterial { 
4        private String materialName = "ABCD-9999"; 
5         
6        public ShoeMaterial(String materialName){ 
7            this.materialName = materialName; 
8        } 
9     
10       public String getMaterialName() { 
11           return ACMEShoesID; 
12       } 
13   }


One subclass, made via extends.

1    package shoesmanufacturing; 
2     
3    public class Suede extends ShoeMaterial{ 
4        private double darkness; 
5     
6        public Suede(String material, double darkness){ 
7            super(material); 
8            this.darkness = darkness; 
9        } 
10    
11       public double getDarkness() { 
<12           return darkness; 
13       } 
14   } 
15   


Another subclass made via extends

1    package shoesmanufacturing; 
2     
3    public class Canvas extends ShoeMaterial{ 
4        private String color = "not set yet"; 
5     
6        public Canvas(String material, String color){ 
7            super(material); 
8            this.color = color; 
9        } 
10    
11       public String getColor() { 
12           return color; 
13       } 
14   } 
15   


Now, looking at a main class using objects of the classes which extend the super class.

1    package shoesmanufacturing; 
2     
3    public class MainForShoes { 
4        public static void main(String[] args) { 
5            Canvas bBallCanvas = new Canvas("BBall Classic 148","white"); 
6            System.out.println(bBallCanvas.getColor()); 
7            //possible because from the actual Canvas class. 
8             
9            System.out.println(bBallCanvas.getMaterialName()); 
10           //possible because from the super class of Canvas, ShoeMaterial 
11            
12       } 
13   } 
14   

 


Static "Class" Variables

static - A keyword which allows a data attribute or method to be applied class-wide; i.e. all instances of that class share the attribute or method, or in fact there can only ever be one "instance" of the class, as with the Math class, by the way of not even having a constructor.

static and main( )

The first time you encounter static in the main method of a class, rather than allowing something, it seems to be limiting you, and making your life a tiny bit harder; you realize that any attributes or methods used/called from main have to also be static, and you cannot make multiple instances of any of them. And this is in contrast to any attributes or methods of "template" classes which by the very nature of being template classes can be made multiple instances.

But this all comes from the fact that main has to be one and only one copy; there cannot be multiple instances of Photoshop, for example running. And so a second "useful feature" of static is it preventing accidental multiple copies of things that can only have one copy.


So back to the class-wide nature of static variables and methods - also, appropriately called class variables and methods - things that are defined as static need not have an instance variable made to be able to work with them. Rather static variables can be directly accessed via dot notation from the class name.

 

Quick Example

Non-static situation: Here, getLength( )must be non-static, because it is used through an instance of Marker.

Marker m = new Marker();
System.out.println(m.getLength());

Static situation: Here, PI must be a static class variable, able to be directly accessed from the class.

System.out.println(Math.PI);

 

Full Example

1    package grocerystore; 
2     
3    public class Employee { 
4        private static int nextEmployeeNumber = 100001; 
5        //static allows it to be used as a class variable, by all instances of the class 
6        private int employeeNumber = -999; 
7        private String employeeName = "Not set yet"; 
8     
9        public Employee(){ 
10           employeeNumber = nextEmployeeNumber; 
11           nextEmployeeNumber++; 
12       } 
13    
14       public Employee(String employeeName){ 
15           this.employeeName = employeeName; 
16           employeeNumber = nextEmployeeNumber; 
17           nextEmployeeNumber++; 
18       } 
19    
20       public static int getNextEmployeeNumber() { 
21           return nextEmployeeNumber; 
22       } 
23    
24       public String getEmployeeName() { 
25           return employeeName; 
26       } 
27    
28       public int getEmployeeNumber(){ 
29           return employeeNumber; 
30       } 
31   } 
32   



1    package grocerystore; 
2     
3    public class MainForGroceryStore { 
4        public static void main(String[] args) { 
5            Employee emma = new Employee("Emma Watson"); 
6            Employee barb = new Employee("Barbara George"); 
7            Employee charlie = new Employee("Charles Goodfellow"); 
8     
9            System.out.println(charlie.getEmployeeNumber()); 
10           //prints out 100003 
11    
12           System.out.println("The next employee number will be " + Employee.getNextEmployeeNumber()); 
13           //prints out: The next employee number will be 100004 
14           //Note that this was not from an **instance**, like charlie. or barb., 
15           //it was from the class, Employee.get... 
16           //nextEmployeeNumber is a CLASS VARIABLE (and static keeps it that way) 
17    
18    
19    
20           //Here is another example of using static/class variables. The Math class has a couple 
21           //of static variables, and many static/class methods: 
22           System.out.println(Math.PI); 
23           System.out.println(Math.E); 
24           System.out.println(Math.ceil(1.12)); 
25           System.out.println(Math.abs(-234)); 
26    
27           //In fact all Math variables and methods are static; there is not even a 
28           //constructor to make a Math object. 
29           Math m = new Math(); // WILL NOT WORK; there is no Math class constructor; all it's attributes and methods are class attributes and methods 
30    
31    
32    
33           //Note also that methods called from a static context must also be static. 
34    
35           method1();  //This call WILL NOT WORK since method1(), below is not static and is 
36                       //being called from main which is static. 
37           method2();  //Will work; method2() is static 
38       } 
39    
40       public void method1(){ 
41           //... 
42       } 
43    
44       public static void method2(){ 
45           //... 
46       } 
47   } 
48