0% found this document useful (0 votes)
10 views

Chapter 04_Encapsulation and Abstraction

Uploaded by

adikari.seww
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Chapter 04_Encapsulation and Abstraction

Uploaded by

adikari.seww
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Chapter – 04

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

• It allows you to group several related classes as siblings.


• Abstraction helps to reduce the complexity of the design and implementation process of
software.

6
Abstract class in Java

A class that is declared as abstract is known as abstract class. It needs to be extended


and its method implemented. It cannot be instantiated.
– Example abstract class
abstract class A{}

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.

• An interface is not a class. Writing an interface is similar to writing a class, but


they are two different concepts. A class describes the attributes and behaviors
of an object. An interface contains behaviors that a class implements.

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 NameOfInterface {


// Any number of final, static fields
// Any number of abstract method declarations
}
11
Example:
public class MammalInt implements
Animal{
public void eat(){
System.out.println("Mammal eats");
interface Animal { }
public void eat(); public void travel(){
System.out.println("Mammal travels");
public void travel(); }
} public static void main(String args[]){
MammalInt m = new MammalInt();
m.eat(); m.travel();
} 12

}
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

//Save with the name Sports.java


Similarities between a Class and Interface

• An interface can contain any number of methods.


• An interface is written in a file with a .java extension, with the name of the
interface matching the name of the file.
• The byte code of an interface appears in a .class file.
• Interfaces appear in packages, and their corresponding byte code file must be in
a directory structure that matches the package name.

14
Difference between a Class and Interface

• Cannot instantiate an interface.


• An interface does not contain any constructors.
• All of the methods in an interface are abstract.
• An interface cannot contain instance fields. The only fields that can appear in
an interface must be declared both static and final.
• An interface is not extended by a class; it is implemented by a class.
15
• An interface can extend multiple interfaces.
Chapter 05

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy