Sie sind auf Seite 1von 15

Encapsulation

Encapsu..What?
 Encapsulation
 Encapsulation describes the ability of an object
to hide its data and methods from the rest of the
world - one of the fundamental principles of
OOP (Object Oriented Programming).
http://orafaq.com/glossary/faqglose.htm
What is..
 Object A thing or concept (usually the
Class object)
 Data  The way in which that object is
defined – its attributes
 Method  The things that that object can do
- actions
Mouse class
Attributes

 Size
 Colour
 Shape
 Buttons
 ConnectionType
 Cost
Mouse Class
Methods

 Leftclick
 Right click
 Scroll
 Move pointer
Exercise

In groups of 2/3 using the provided sheets


write down what the attributes and
methods of the following are:
 Ball
 Dog
 Cash Machine
Secret Potion
Public v Private Classes
 Public classes are accessible to both the
member class and any other calling class
 Private classes are only accessible by
other members of its Class.
Example
 package simpleclassproject;

 /**
 *
 * @author Annamarie Kelly
 */
 public class SimpleClass {

 private int m_number;

 public int getNumber()
 {
 return m_number;
 }

 public void setNumber(int _newValue)
 {
 if(_newValue<0 || _newValue>10)
 m_number = 0;
 else
 m_number = _newValue;
 }
Protect the data
 Data is now Safe

 But how do you get to


it?
Exercise

On the S: Drive within the Java drive you will


find a file. Run it and take note of how the

SimpleClass.java
SimpleClassTestHarness.java

Now re-create the simpleclassproject


package for yourself
Need For An Interface
Have a method which
Gets and Sets the data
for you
In Other Words-

“The best code is very shy. Like a four year


old hiding behind a mothers skirt, code
shouldn’t reveal to much of itself and it
shouldn’t be too nosy about others
affairs..”
Exercise
 Usingthe mouse example make a list of
the public and private classes which you
might put into the mouse class.

Das könnte Ihnen auch gefallen