Object Oriented Programming Encapsulation and Abstraction
Object Oriented Programming Encapsulation and Abstraction
Today’s Agenda
2
Objectives
• Define abstraction
• Identify levels of abstraction
• Understand that a class is a form of abstraction
• Define a Java class
• Learn how to create objects by instantiating their class
• Identify class members
• Identify and define each modifier applicable to a class
• Identify and define each modifier applicable to class
members
• Define access scope for class and its members
• Know the purpose of constructors and how to create one
for a class
3
Objectives (continued)
• Define encapsulation
• Describe the principles of encapsulation
• Learn how to encapsulate a class
• Learn how to use setters and getters
4
Defining Abstraction
• Abstraction is the process of extracting common features
from specific examples
• Abstraction is a process of defining the essential concepts
while ignoring the inessential details
5
Different Types of Abstraction
• Data Abstraction
Programming languages define constructs to simplify the
way information is presented to the programmer.
• Functional Abstraction
Programming languages have constructs that ‘gift wrap’
very complex and low level instructions into instructions
that are much more readable.
• Object Abstraction
OOP languages take the concept even further and
abstract programming constructs as objects.
6
Everything is an Object
7
Defining an Object
8
Class as Abstraction
• A class is an abstraction of its
instances. It defines all the
attributes and methods that its
instances must also have.
Person
name
sex
age
tellSex()
tellAge()
9
Defining a Class
• A Class acts as the template from which an instance of an
object is created. The class defines the properties of the
object and the methods used to control the object's behavior.
• A Class specifies the structure of data as well as the
methods which manipulate that data. Such data and methods
are contained in each instance of the class.
• A Class is a model or template that can be instantiated to
create objects with a common definition, and therefore
common properties, operations and behavior.
• A Class provides a template for defining the behavior of a
particular type of object. Objects are referred to as
“instances” of a class.
10
Defining a Java Class
• A Java Class denotes a category of objects, and acts as a blueprint for creating
such objects.
• It defines its members referred to as fields and methods.
• The fields (also known as variables or attributes) refer to the properties of the
class.
• The methods (also known as operations) refer to behaviors that the class
exhibits.
class Person {
String name;
char sex;
int age;
void tellSex() {
if (sex=='M')
System.out.println("I'm Male.");
else if (sex=='F')
System.out.println("I'm Female.");
else System.out.println("I don't know!");
}
void tellAge() {
if (age<10)
System.out.println("I'm just a kid.");
else if (age<20)
System.out.println("I'm a teenager.");
else System.out.println("I'm a grown up.");
}
}
11
Class Members
• A class member refers to one of the fields or methods of a class.
• Static members are variables and methods belonging to a class where only a single copy
of variables and methods are shared by each object.
• Instance members are variables and methods belonging to objects where a copy of each
variable and method is created for each object instantiated.
class Person {
static int maleCount;
static int femaleCount;
String name;
char sex;
int age;
static void showSexDistribution() {
if (maleCount>femaleCount)
System.out.println("Majority are male.");
else if (femaleCount>maleCount)
System.out.println("Majority are female.");
else System.out.println("Equal number of male and female.");
}
void tellSex() {
if (sex=='M')
System.out.println("I'm Male.");
else if (sex=='F')
System.out.println("I'm Female.");
else System.out.println("I don't know!");
}
void tellAge() {
if (age<10)
System.out.println("I'm just a kid.");
else if (age<20)
System.out.println("I'm a teenager.");
else System.out.println("I'm a grown up.");
}
}
12
Instantiating a Class &
Accessing its Members
• Instantiating a class means creating objects of its own type.
• The new operator is used to instantiate a class.
Create Person objects
using the new operator.
class MainProgram { class Person {
public static void main(String[] args) { static int maleCount;
static int femaleCount;
// instantiating several objects String name;
Person p1 = new Person(); char sex;
Person p2 = new Person(); int age;
Person p3 = new Person();
static void showSexDistribution() {
// accessing instance variables // method body here
p1.name = "Vincent"; p1.sex = 'M'; p1.age = 8; }
p2.name = "Janice"; p2.sex = 'F'; p2.age = 19;
p3.name = "Ricky"; p3.sex = 'M'; p3.age = 34; void tellSex() {
// accessing static variables // method body here
Person.maleCount = 2; }
Person.femaleCount = 1;
void tellAge() {
// accesssing instance methods // method body here
p1.tellSex(); p1.tellAge(); }
p2.tellSex(); p2.tellAge(); }
p3.tellSex(); p3.tellAge(); Sample Output:
// accessing static method I'm Male.
Person.showSexDistribution(); I'm just a kid.
} I'm Female. Access class variables
}
I'm a teenager.
Access class methods by setting their values
I'm Male.
by invoking their
I'mnames
a grown up.
Majority are male.
13
Class Modifiers
• Class modifiers change the way a class can be used.
• Access modifiers describe how a class can be accessed.
• Non-access modifiers describe how a class can be
manipulated.
Modifier Description
14
Access Modifiers
• Member modifiers change the way class members can be used
• Access modifiers describe how a member can be accessed
Modifier Description
(no member is accessible within its package only
modifier)
public member is accessible from any class of any
package
15
Access Modifiers
private default
Private features of the Only classes that are in the
Private features of the Class Only classes that are in the
Sample class can only be package may access
Sample class can only be package may access
accessed from within the default features of classes
accessed from within the default features of classes
class itself. that are in the package
class itself. Package that are in the package
Sample
Class
Class Class
protected public
Classes that are in the All classes may access
Classes that are in the All classes may access
package and all its Class Class public features of the
package and all its public features of the
subclasses may access Sample class.
subclasses may access Sample class.
protected features of the
protected features of the
Sample class.
Sample class.
* Default is not a modifier; it is just the name of the access level if no access modifier is specified.
16
Member Modifiers
• Member modifiers change the way class members can be used
• Non-access modifiers describe how a member can be manipulated
Modifier Description
static member belongs to a class
final declares a constant variable or method
abstract method is declared with no implementation (applied to methods, cannot
be combined with other non-access modifiers )
strictfp method implements strict floating-point arithmetic (applied to methods)
synchronized method is executed by only one thread at a time (applied only to
methods)
native method implementation is written in other language (applied only to
methods)
transient an instance variable is not saved when its object is persisted or
serialized (applied only to variables)
volatile variable is modified asynchronously by concurrently running threads
(applied only to variables)
17
Accessibility Scope
• Accessibility scope defines the boundary of access to a
class and its members
Scope Access
static static code can access static members but not instance
members
non-static non-static code can access both static members and
instance members
package a class and its members can be accessed within the
package they are declared
class class members can be accessed within the class
18
Defining Encapsulation
19
Principles of Encapsulation
20
Encapsulating a Class
• Members of a class must always be declared with the
minimum level of visibility.
• Provide setters and getters (also known as
accessors/mutators) to allow controlled access to private
data.
• Provide other public methods (known as interfaces ) that
other objects must adhere to in order to interact with the
object.
21
Setters and Getters
• Setters and Getters allow controlled access to class data
• Setters are methods that (only) alter the state of an object
• Use setters to validate data before changing the object state
• Getters are methods that (only) return information about the state of
an object
• Use getters to format data before returning the object’s state
24