Skip to content
March 7, 2013 / Danii Oliver

Encapsulation & Access Level Controls

Discussion topics:

Private vs Protected vs Public & Constructors

Encapsulation

  1. Encapsulation hides or protects data declared and implemented within a function

void foo(){

int x;

}

int Incr(int a){

int x;

a++;

}

Encapsulation puts a brick wall around whatever it is doing.

All things in a Class once declared can’t be accessed unless it’s inside the Class itself. Encapsulation happens to a Class by default.

**members or fields of a Class are private by default.

class Student {

private string name;

public int age;

//get/set methods

}

Student s = new Student();

s.name = “John”; → complier error

s.age = -20; → no error b/c it is open to altering

**With Dynmaic memory allocation: From “s.name” the [.] is a member selector. Before the [.] is a reference to the object. After the [.] is the member of the object.

Stack: memory allocated at the beginning for any variable declaration – Compile Time

Heap: memory allocated later at runtime when the key word “new” is used. Can also destroy when no longer needed in some languages. not Java – Runtime

**You need to return memory to the Heap if not being used. You don’t want to run out and crash your program.

Instantiation

  1. to Instantiate : means to go and get memory for something. The something has to know where the memory is that is the purpose of the variable used.

Private

  1. Access Specifier. It qualifies the declaration of a var or method to be private and thus inaccessible by anything outside of the class. Private is default in a Class and default within the scope of a method a variable or method is declared in.
  2. Make methods private when it is part of the creation/definition of a Class’s default actions.

Public

  1. The complete opposite of private. Anyone who has access to an object can set values on member field or data. Public methods can also be called externally. Yes you can make fields public but DON’T
  2. Only const can be public because they can’t be changed anyway.
  3. Make methods public if the method is an action that needs to be called later by another class to complete a requirement.

class Printer {

public void Print()

{

// print functionality

}

class Student (){

Print p = new Printer();

p.Print() → no error b/c it is open to be called to execute

}

**the print method HAS to be public because it will never be called within itself.

***The responsibility of your Class should be to do one thing.

**Association needs to have method calling!

Protected

  1. Balance between Private and Public
  2. Protected means it can only be accessed by a subclass.
  3. A subclass does not have access to private members of its superclass. Everyone has access to all public…but…
  4. Protected means only the subclass that extends a superclass has the ability to have access to members it would need and no other class may edit.

public class Rectangle {

protected int side;

}


public class Square:Rectangle {

public int Area(){

return side*side

}

}

class Student (){

Rect r = new Rect();

r.side = 10 → compiler error

Square s = new Square();

s.side = 15; → compiler error

}

*Internal

  1. Less strict than private. It allows package access. in the same folder and more framework based.

UML

– private

+ public

#protected


Getters/Setters

public class Rectangle {

protected int _side;

}


public class Square:Rectangle {

public int Area(){

return side*side

}

public int getSide(){

return _side;

}

public void setSide(int side){

/*here you can check for meeting

requirements i.e*/

if(side >=0){

this.side = side; //if _ not used SAFER

_side = side; //if declared with _

}

}

}

class Student (){

Square s = new Square();

s.setSide( 15 ); → no error b/c a public method was called to setSide

}

*the keyword [this] is a self reference.

left of assignment with this is declaration inside the class

right of assignment is the var coming in to be set.

*using [this] prevents errors later. This is Defensive programming

**the leading _ is another way of visualizing this if it was declared as such. _ is a coding standard it helps to see the scope.

only getter = read only

only setter = write only

* PUBLIC fields and methods can be accessed anywhere.

* PRIVATE fields and methods can only be accessed ONLY from within the class that declared or defined them. i.e these fields and methods can only be activated by the class’s normal functionality or via setting from getter/setter or a constructor.

Developer thinks to solve a problem

Architect realizes there are two problems : its not simply the solution for ABC but you also need a framework that will allow you to leverage that solution elsewhere in the future. “The class you make today could be a superclass in the future”. You can’t make one big class that does everything. It needs to be able to be inherited. The should be lean as you would inherit something that has 15 fields and 50 methods.

** PROTECTED is used for when you foresee a superclass forming and you know you want subclasses to be able to have access.

PUBLIC, PRIVATE & PROTECTED have no meaning inside the class that it’s defined. Once outside or instantiating an object then it make all the difference.

Subclass get private fields and methods (or members) from it’s superclass. While the subclass gets the information, it does not have access to change any of those private members. Inheritance is all or nothing period.


Constructor

  1. Constructors don’t construct they initialize. A programmer would initialize like this int x=0;
  2. Variable is not initialized by default in every language, some set string to =“ ” or int to 0 but another may not thus making int = NaN;

Why not initialize a user defined Type? You don’t want to set a member after you may get errors.

  1. The constructor initializes before the first use. You want the constructor to be complete before student s = new Student(); is complete.
  2. The constructor can only initialize based on the types of fields in the class => this is a default constructor.

Options: Make your own constructor…

Make a Constructor

  1. The constructor has the name of your Class. The name of the constructor is the name of the Class. It does not need a return type. Thus public Student(){}
  2. new” in the class making an object of another Class is what initializes the fields. it will be set by passing data or by code defined in the constructor
  3. The constructor is inside of the Classes self. Once the Class is instantiated it has been constructed and initialized properly.
  4. Constructor with no arguments is a Default Constructor. If the compiler has to make it it will have no arguments
  5. The constructor is there to preserve your RULES. Use it well.

External set: nothing is hard coded

Set @ Instantiation

class Student {

stringfname;

stringlname;

public Student(string:fname, string:lname) {

this.fname = fname;

this.lname = lname;

}

}

class College (){

Student s = new Student(“Joe”, “Smith”);

string a = s.GetFname();

Student t = new Student(“Bill”,”Withers”);

string b = t.GetFname();

}

Self setroom for problem or the need to reset every instance

NOT WELL EXECUTED

class Student {

stringfname;

stringlname;

public Student() {

fname = Joe;

lname = Smith;

→ problematic b/c every instance is set to these values which may be inappropriate

}

}

class College (){

Student s = new Student();

string a = s.GetFname();

Student t = new Student();

string b = t.GetFname();

}

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: