Logout

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:

Repeat from D.3.1: Really, all you need as "canned" IB exam prepared answers is simple definitions as follows, but look below for more details, as covered in the "old school" white board lecture. Note that with the 2015 video of that, the sound was unfortunately not captured. :-( But come to think of it, that's just as well for this assessment statement, since you are dealing with written communication here; you need to be able to define these in words, and conversely understand what is being described by these words. (Though adding a quick little diagram never hurt a definition kind of exam answer.)


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 Details:

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.

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 class which uses extends to inherit from another class:

public class Car extends Vehicle{
    ...
}


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.
And looked at the other way, 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. Here's an example:

Non-static situation: (So length must be non-static (whether or not it is private).)

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

Static situation: (So pi must be a static variable (and it also must not be private).)

System.out.println(Math.pi);