SlideShare a Scribd company logo
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.
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
Abstraction
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()
}
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
} }
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
}
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.
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.
Difference Between Traits and
Abstract Classes in Scala
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.
Packages
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..
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
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}
}
} }
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.
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
Case Study - DIY
Access Modifiers
 we can restrict the members from being accessible to certain areas of the
code.
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.
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.
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/

More Related Content

Similar to Unit-4 Day1.pptx

Class properties
Class propertiesClass properties
Class properties
Siva Priya
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
Praveen M Jigajinni
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
Vishnu Suresh
 
Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
raksharao
 
CHAPTER 3 part1.pdf
CHAPTER 3 part1.pdfCHAPTER 3 part1.pdf
CHAPTER 3 part1.pdf
FacultyAnupamaAlagan
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078
Aravind NC
 
Oops
OopsOops
Object oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxObject oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
Packages in java
Packages in javaPackages in java
Packages in java
SahithiReddyEtikala
 
12th ip CBSE chapter 4 oop in java notes complete
12th ip CBSE  chapter 4 oop in java notes complete12th ip CBSE  chapter 4 oop in java notes complete
12th ip CBSE chapter 4 oop in java notes complete
Harish Gyanani
 
Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basicsvamshimahi
 
4th_class.pdf
4th_class.pdf4th_class.pdf
4th_class.pdf
RumiHossain5
 
Nitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptx
NitishChaulagai
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorials
cesarmendez78
 
Use Classes with Object-Oriented Programming in C++.ppt
Use Classes with Object-Oriented Programming in C++.pptUse Classes with Object-Oriented Programming in C++.ppt
Use Classes with Object-Oriented Programming in C++.ppt
manishchoudhary91861
 
C++ Inheritance.pptx
C++ Inheritance.pptxC++ Inheritance.pptx
C++ Inheritance.pptx
XanGwaps
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
bindur87
 
C# structure
C# structureC# structure
C# structure
ElangovanTechNotesET
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
Arindam Ghosh
 

Similar to Unit-4 Day1.pptx (20)

Class properties
Class propertiesClass properties
Class properties
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
 
CHAPTER 3 part1.pdf
CHAPTER 3 part1.pdfCHAPTER 3 part1.pdf
CHAPTER 3 part1.pdf
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078
 
Oops
OopsOops
Oops
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxObject oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptx
 
Packages in java
Packages in javaPackages in java
Packages in java
 
12th ip CBSE chapter 4 oop in java notes complete
12th ip CBSE  chapter 4 oop in java notes complete12th ip CBSE  chapter 4 oop in java notes complete
12th ip CBSE chapter 4 oop in java notes complete
 
Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basics
 
4th_class.pdf
4th_class.pdf4th_class.pdf
4th_class.pdf
 
Nitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptx
 
LectureNotes-02-DSA
LectureNotes-02-DSALectureNotes-02-DSA
LectureNotes-02-DSA
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorials
 
Use Classes with Object-Oriented Programming in C++.ppt
Use Classes with Object-Oriented Programming in C++.pptUse Classes with Object-Oriented Programming in C++.ppt
Use Classes with Object-Oriented Programming in C++.ppt
 
C++ Inheritance.pptx
C++ Inheritance.pptxC++ Inheritance.pptx
C++ Inheritance.pptx
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
 
C# structure
C# structureC# structure
C# structure
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
 

More from Revathiparamanathan

UNIT 1 NOTES.docx
UNIT 1 NOTES.docxUNIT 1 NOTES.docx
UNIT 1 NOTES.docx
Revathiparamanathan
 
Unit 3,4.docx
Unit 3,4.docxUnit 3,4.docx
Unit 3,4.docx
Revathiparamanathan
 
UNIT II.docx
UNIT II.docxUNIT II.docx
UNIT II.docx
Revathiparamanathan
 
UNIT V.docx
UNIT V.docxUNIT V.docx
UNIT V.docx
Revathiparamanathan
 
COMPILER DESIGN.docx
COMPILER DESIGN.docxCOMPILER DESIGN.docx
COMPILER DESIGN.docx
Revathiparamanathan
 
UNIT -III.docx
UNIT -III.docxUNIT -III.docx
UNIT -III.docx
Revathiparamanathan
 
UNIT -IV.docx
UNIT -IV.docxUNIT -IV.docx
UNIT -IV.docx
Revathiparamanathan
 
UNIT - II.docx
UNIT - II.docxUNIT - II.docx
UNIT - II.docx
Revathiparamanathan
 
UNIT -V.docx
UNIT -V.docxUNIT -V.docx
UNIT -V.docx
Revathiparamanathan
 
UNIT - I.docx
UNIT - I.docxUNIT - I.docx
UNIT - I.docx
Revathiparamanathan
 
CC -Unit3.pptx
CC -Unit3.pptxCC -Unit3.pptx
CC -Unit3.pptx
Revathiparamanathan
 
CC -Unit4.pptx
CC -Unit4.pptxCC -Unit4.pptx
CC -Unit4.pptx
Revathiparamanathan
 
CC.pptx
CC.pptxCC.pptx
Unit 4 notes.pdf
Unit 4 notes.pdfUnit 4 notes.pdf
Unit 4 notes.pdf
Revathiparamanathan
 
Unit 3 notes.pdf
Unit 3 notes.pdfUnit 3 notes.pdf
Unit 3 notes.pdf
Revathiparamanathan
 
Unit 1 notes.pdf
Unit 1 notes.pdfUnit 1 notes.pdf
Unit 1 notes.pdf
Revathiparamanathan
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
Revathiparamanathan
 
Unit 5 notes.pdf
Unit 5 notes.pdfUnit 5 notes.pdf
Unit 5 notes.pdf
Revathiparamanathan
 
CC.pptx
CC.pptxCC.pptx
Scala Introduction.pptx
Scala Introduction.pptxScala Introduction.pptx
Scala Introduction.pptx
Revathiparamanathan
 

More from Revathiparamanathan (20)

UNIT 1 NOTES.docx
UNIT 1 NOTES.docxUNIT 1 NOTES.docx
UNIT 1 NOTES.docx
 
Unit 3,4.docx
Unit 3,4.docxUnit 3,4.docx
Unit 3,4.docx
 
UNIT II.docx
UNIT II.docxUNIT II.docx
UNIT II.docx
 
UNIT V.docx
UNIT V.docxUNIT V.docx
UNIT V.docx
 
COMPILER DESIGN.docx
COMPILER DESIGN.docxCOMPILER DESIGN.docx
COMPILER DESIGN.docx
 
UNIT -III.docx
UNIT -III.docxUNIT -III.docx
UNIT -III.docx
 
UNIT -IV.docx
UNIT -IV.docxUNIT -IV.docx
UNIT -IV.docx
 
UNIT - II.docx
UNIT - II.docxUNIT - II.docx
UNIT - II.docx
 
UNIT -V.docx
UNIT -V.docxUNIT -V.docx
UNIT -V.docx
 
UNIT - I.docx
UNIT - I.docxUNIT - I.docx
UNIT - I.docx
 
CC -Unit3.pptx
CC -Unit3.pptxCC -Unit3.pptx
CC -Unit3.pptx
 
CC -Unit4.pptx
CC -Unit4.pptxCC -Unit4.pptx
CC -Unit4.pptx
 
CC.pptx
CC.pptxCC.pptx
CC.pptx
 
Unit 4 notes.pdf
Unit 4 notes.pdfUnit 4 notes.pdf
Unit 4 notes.pdf
 
Unit 3 notes.pdf
Unit 3 notes.pdfUnit 3 notes.pdf
Unit 3 notes.pdf
 
Unit 1 notes.pdf
Unit 1 notes.pdfUnit 1 notes.pdf
Unit 1 notes.pdf
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Unit 5 notes.pdf
Unit 5 notes.pdfUnit 5 notes.pdf
Unit 5 notes.pdf
 
CC.pptx
CC.pptxCC.pptx
CC.pptx
 
Scala Introduction.pptx
Scala Introduction.pptxScala Introduction.pptx
Scala Introduction.pptx
 

Recently uploaded

Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
ChristineTorrepenida1
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
veerababupersonal22
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 

Recently uploaded (20)

Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 

Unit-4 Day1.pptx

  • 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.
  • 10. Difference Between Traits and Abstract Classes in Scala
  • 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/