This document discusses abstraction and encapsulation in Scala. It defines abstraction as hiding internal details and showing only functionality. Abstraction can be achieved through abstract classes and interfaces/traits. Abstract classes can contain abstract and non-abstract methods, while interfaces contain only abstract methods. The document also discusses packages, which provide namespaces, and access modifiers like private, protected, and public, which restrict access to class members.
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Scala Abstraction and Encapsulation
1.
2. UNIT - IV
Abstraction and Encapsulation
Abstraction: Abstract Methods - Abstract Class and its importance –
implementation of abstraction – Encapsulation: Packages - Access
Modifiers – application of encapsulation - Type Class and Conversions.
3. Abstraction
⮚Abstraction is the process to hide the internal details and showing only the
functionality.
⮚In Scala, abstraction is achieved by using an abstract class.
⮚an abstract class is constructed using the abstract keyword.
⮚Abstraction can be achieved in two ways.
⮚Abstract method
⮚Non-abstract method
⮚Interface
5. Abstract Methods
• The abstract methods of abstract class are those methods which do not
contain any implementation. Or in other words, the method which does
not contain body is known as an abstract method.
Syntax:
abstract class class-name{
def method-name()
}
6. Non-abstract Methods
• These methods are those methods that define their implementation as well at the
time of declaring themselves. They contain their body that means the actual logic
inside them, so we can say that this logic is not hiding with the user because we
are providing the implementation so they cannot give us full abstraction.
• Syntax:
abstract class class-name {
def method-name(){
// you can write your logic here
} }
7. Interface
• using the interface we can achieve 100% abstraction. In Scala, we use trait
keyword to define an interface
• Syntax:
trait class-name {
def method-name(): Unit
def method-name(): Unit
}
8. Examples of Scala Abstract Class
• We have one Bank as a parent bank for all its sub banks. But the benefits
they are providing to their customers vary from each other like the rate of
interest they are giving is different from bank to bank. so, in this case, we
can have our print class as Bank and all other child classes are like SBI,
ICICI, IDBI, BOI, and many more.
9. Difference Between Traits and
Abstract Classes in Scala
• In Scala, an abstract class is constructed using the abstract keyword.
• It contains both abstract and non-abstract methods and cannot support
multiple inheritances.
• Traits can have methods(both abstract and non-abstract), and fields as its
members.
• Traits are just like interfaces in Java.
• But they are more powerful than the interface in Java because in the traits
we are allowed to implement the members.
11. Packages
Package in Scala is a mechanism to encapsulate a group of classes, sub
packages, traits and package objects.
It basically provides namespace to put our code in a different files and
directories.
Packages is a easy way to maintain our code which prevents naming
conflicts of members of different packages.
13. Declaration of Package
Packages are declared as a first statement at the top of a Scala file.
Syntax :
package package_name
// Scala classes
// traits
// objects..
14. Defining a package
Defining a package can be done in different ways:-
Chained methods
package x.y.z
// members of z
or can be used as:-
package x
package y
package z
// member of z
15. Defining a package
Nesting packages
package x{
// members of x {as required}
package y{
// members of y{as required}
package z{
// members of z{as required}
}
} }
16. How Package works
Packages binds together the data in a single file or works as data
encapsulation, when a file is saved it comes under default package or
under a package name as specified at the top of the file.
For example if a package name is college.student.cse, then there will be
3 directories, college, student and cse. Such that cse is present in student
and student is present in college.
17. Different ways to use import statements
1. Importing all public members of package.
import college._
//imports all college members students, faculties, houseKeeping etc.
2. Imports only selected members of a package.
import college.{faculty, houseKeeping}
//member student is not selected hence cannot be used in current file
3. Imports and rename the members.
import college.{student => stu}
//stu is used in-place of student
19. Access Modifiers
we can restrict the members from being accessible to certain areas of the
code.
20. Private Modifiers
Private members are available only inside the class or to the object
containing the member definition.
The private access modifier can be used for variables and methods as
well.
21. Protected Modifiers
Protected members can be accessed from subclasses of the class in which
the member is defined (inherited class).
This can also be used for methods as well as variables.
22. Public Modifiers
The default access modifier is public.
When the keywords private or protected is not specified then the
members are treated as public by the compiler and we don’t have to
explicitly specify the keyword “public”.
https://www.simplilearn.com/tutorials/java-tutorial/java-encapsulation
https://www.scientecheasy.com/2020/07/encapsulation-in-java.html/