Download presentation
Presentation is loading. Please wait.
Published byNoel McGee Modified over 9 years ago
1
Encapsulation
2
Encapsulation Encapsulation means the bringing together of a set of attributes and methods into an object definition and hiding their implementational structure from the object's users. Therefore, how an object structures and implements its attributes and methods is not visible to other objects using it.
3
Encapsulation Direct access to an object's attributes is not permitted and any changes to the object's data can only be effected indirectly via a set of publicly available methods. Analogy: Egg ◦ Yolk attribute ◦ White method
4
Encapsulation Access control specifiers facilitate encapsulation by controlling the visilibity of data and methods by other objects.
5
Bundling & Information Hiding Encapsulation is supported by two subordinate concepts: bundling and information hiding. Bundling is the act of associating a set of methods with a set of data as the only means of affecting the values of the data. Related data and methods are therefore brought together in bundling, thus increasing the cohesiveness of object definition.
6
Bundling & Information Hiding Information hiding refers to the hiding of internal representation of data and methods from the users of these data and methods. By exercising information hiding, data access on an object is limited to a set of publicly available methods.
7
Bundling & Information Hiding Information hiding enables the separation of the what from the how of object definition. What specifies what behavior an object is capable of and how specifies how the data and methods of an object are implemented.
8
Enhanced Software Maintainability By separating what from how, a client’s access to an object is not affected by changes in the internal implementation of the object. This enhances software maintainability. Ilustrasi stack
9
class Stack { private int contents[]; private int top, size=10; public int pop() { int x = 0; else x = contents[top--]; return(x); } public void push(int x) { if (full()) System.err.println( stack overflow ); else {contents[++top] = x; System.out.println( Pushed +x+ into Stack );} } public int size() { return(top+1); } public boolean empty() { return(size() == 0); } public boolean full() { return(size() == contents.length); } if (empty()) System.err.println("stack underflow"); Stack() { contents = new int[size]; top = -1; } public static void main(String argv[]) { int i, numberOfItem; numberOfItem=Integer.parseInt(argv[0]) ; Stack s = new Stack(); for (i = 0; i<numberOfItem; i++) s.push(i); System.out.println( \nDetails of Stack : ); for (i = numberOfItem; i>0; i--) System.out.println("Item popped = "+s.pop()); }
10
class Stack { private StackItem top, temp; private int size; public int pop() { int x = 0; if (empty()) System.err.println("stack underflow"); else {x = top.getItem(); top=top.getPrevious();size=size- 1;} return(x); } public void push(int x) { if (full()) System.err.println("stack overflow"); else {temp=top; top=new StackItem(); top.setPrevious(temp); top.setItem(x); size=size+1; System.out.println("Pushed "+x+" into Stack");} } public int size() { return(size); } public boolean empty() { return(size() == 0); } public boolean full() { return(false); } Stack() { top = null; size = 0; } public static void main(String argv[]) { int i, numberOfItem; numberOfItem=Integer.parseInt(argv[0]) ; Stack s = new Stack(); for (i = 0; i<numberOfItem; i++) s.push(i); System.out.println("\nDetails of Stack : "); for (i = numberOfItem; i>0; i--) System.out.println("Item popped = "+s.pop()); } class StackItem { private int item=0; private StackItem previous; public int getItem() {return item;} public void setItem(int x) {item=x;} public StackItem getPrevious() {return previous;} public void setPrevious(StackItem p) {previous=p;} StackItem() {previous=null;} }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.