Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSG2H3 Object Oriented Programming

Similar presentations


Presentation on theme: "CSG2H3 Object Oriented Programming"— Presentation transcript:

1 CSG2H3 Object Oriented Programming
Encapsulation

2 “Driver” Class Class that contain main method (psvm)
To “Drives” the application Only a term, not necessarily that a main class is called “driver” To separate between class “model” and class “application”

3 “Model” Classes Try not to put any input/output mechanism in model class’ methods Use methods to process function or action by receiving parameters then return the result Let the “driver” class do the I/O interaction For this we learn “Encapsulation”

4 Encapsulation Accessor and mutators

5 Information Hiding Get me data from an object!
Don’t know how you do it Don’t care how you do it

6 Encapsulation FIRST LAW of OOP : Data must be hidden
Read access through read functions Write access through write functions For Every piece of data : 4 possibilities Read and write Read only Write only No access

7 Encapsulation Hides the behavior of an object from its implementation
Separates what an object looks like from how it does it implements its behavior

8 Modularity The source code for an object can be written and maintained independently of the source code for the other objects

9 Data/Information Hiding
An object has public interface that other objects can use to communicate with it. The object can maintain private information and method that can be changed at any time without affecting other objects that depend on it.

10 Setter and Getter To modify the value of a field, use setter
void setVarName(varName : varType) To get the value of a field, use getter varType getVarName()

11 In other words… Technique of making the fields in a class private and providing access to the field via public methods. If a field is declared private, it cannot be accessed by anyone outside the class  data hiding Prevents the code and data being randomly accessed by other code outside the class For that, we use the Access Modifiers

12 Access Modifiers Access Modifiers specify what classes “see” or “know about” They are the public, private, and protected Effects of visibility … you cannot: reference or instantiate classes which are “invisible” to you call methods that are invisible to you directly access instance variables which are invisible to you

13 Access Modifier Privilege
Same class Same Package Subclass Any class private Y default protected Public

14 public Fields, methods and constructors declared public (least restrictive) within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package.

15 private Fields, methods or constructors declared private are strictly controlled, which means they cannot be accessed by anywhere outside the enclosing class. A standard design strategy is to make all fields private and provide public getter methods for them.

16 protected Fields, methods and constructors declared protected in a superclass can be accessed only by subclasses in other/same packages. Classes in the same package can also access protected fields and methods as well, even if they are not a subclass of the protected member’s class. we will learn more of this in Inheritance

17 default no access modifier is present.
Any class, field, method or constructor that has no declared access modifier is accessible only by classes in the same package.

18 Example error: privateInt has private access
public class Encapsulation{ public int publicInt; private int privateInt; int defaultInt; } public class Driver{ public static void main(String args[]){ Encapsulation e = new Encapsulation(); e.publicInt = 10; e.privateInt = 20; e.defaultInt = 30; System.out.println(e.publicInt+" "+ e.privateInt+" "+e.defaultInt); } error: privateInt has private access

19 Example public class Encapsulation{ public int publicInt; private int privateInt; int defaultInt; public void setPrivateInt(int privateInt){ this.privateInt = privateInt; } public int getPrivateInt(){ return privateInt; public class Driver{ public static void main(String args[]){ Encapsulation e = new Encapsulation(); e.publicInt = 10; e.setPrivateInt(20); e.defaultInt = 30; System.out.println(e.publicInt+" "+ e.getPrivateInt()+" "+e.defaultInt); }

20 Example package test; public class Encapsulation{ public int publicInt; private int privateInt; int defaultInt; public void setPrivateInt(int privateInt){ this.privateInt = privateInt; } public int getPrivateInt(){ return privateInt; import test.Encapsulation; public class Driver{ public static void main(String args[]){ Encapsulation e = new Encapsulation(); e.publicInt = 10; e.setPrivateInt(20); e.defaultInt = 30; System.out.println(e.publicInt+" "+ e.getPrivateInt()+" "+e.defaultInt); } error: defaultInt is not public in Encapsulation; cannot be accessed from outside package

21 Question?

22


Download ppt "CSG2H3 Object Oriented Programming"

Similar presentations


Ads by Google