Chapter 04_Encapsulation and Abstraction
Chapter 04_Encapsulation and Abstraction
Encapsulation, Abstraction
and Interface
1
Encapsulation
• Encapsulation is a process of wrapping code and data together into a single unit, for
example, a capsule which is mixed of several medicines.
• We can create a fully encapsulated class in Java by making all the data members of the
class private.
• A private data field cannot be accessed by an object from outside the class that defines
the private field.
• To make a private data field accessible, provide a getter method to return its value.
• To enable a private data field to be updated, provide a setter method to set a new value.
• A getter method is also referred to as an accessor and a setter to a mutator.
2
Example of encapsulation in java
public class Student{
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name=name
}
} 3
Advantages of Encapsulation
• Better control of class attributes and methods
• Class variables can be made read-only (if you omit the set method), or write-only (if you omit
the get method)
• Flexible: the programmer can change one part of the code without affecting other parts
• Increased security of data
• It is a way to achieve data hiding in Java because other class will not be able to access the data
through the private data members.
4
Abstraction
• Abstraction is a process of hiding the implementation details and showing only functionality
to the user.
• It shows only important things to the user and hides the internal details.
• Abstraction lets you focus on what the object does instead of how it does it.
5
Advantages of Abstraction
6
Abstract class in Java
7
Abstract method
A method that is declared as abstract and does not have implementation is
known as abstract method.
– Example abstract method
abstract void printStatus(); //no body and abstract
8
Example
abstract class Bike{
abstract void run();
}
class Honda extends Bike{
void run(){
System.out.println("running safely..");
}
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
}
9
}
Interfaces
• An interface is a collection of abstract methods. A class implements an
interface, thereby inheriting the abstract methods of the interface.
10
Interface Declaration
• Define an interface by using the keyword interface
• The methods declared in an interface are implicitly public and abstract
• The data variables declared in an interface are inherently constants and are
visible from all the instances of the class that implements the interface
}
public interface Sports {
public void setHomeTeam(String name); Example
public void setVisitingTeam(String name);
}
interface Football extends Sports {
public void homeTeamScored(int points);
public void visitingTeamScored(int points);
public void endOfQuarter(int quarter);
}
interface Hockey extends Sports {
public void homeGoalScored();
public void visitingGoalScored();
public void endOfPeriod(int period);
public void overtimePeriod(int ot);
} 13
14
Difference between a Class and Interface
16
Difference between Abstraction and
Encapsulation
Abstraction Encapsulation
Abstraction solves the issues at the design Encapsulation solves it implementation
level. level.
Abstraction is about hiding unwanted
Encapsulation means hiding the code
details while showing most essential
and data into a single unit.
information.
Encapsulation means hiding the internal
Abstraction allows focussing on what the
details or mechanics of how an object
information object must contain
does something for security reasons. 17