SlideShare a Scribd company logo
1 of 39
Chapter 5
Concepts of OOP in java
Basic Concepts of OOP in java
3/29/2024 Object-Oriented Programming 2
• Object
• Class
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism
Object vs Class
3/29/2024 Object-Oriented Programming 3
• An object is any entity that
has a state and behavior.
• State: It is represented by
attributes of an object. It
also shows properties of
an object.
• Behavior: It is represented
by methods of an object. It
shows response of an
object with other objects.
3/29/2024 Object-Oriented Programming 4
Creating object
ClassName objectName=new className
3/29/2024 Object-Oriented Programming 5
Accessing class members: to
access the class members we
use the dot operator.
Object
• objectName.variableName;
• objectName.methodName();
3/29/2024 Object-Oriented Programming 6
• Java access modifier often called access specifier.
• An access modifier is a keyword in oop languages used to
set the accessibility(visibility) of a variables, methods,
constructors classes and interfaces.
3/29/2024 Object-Oriented Programming 7
Access Modifier
3/29/2024 Object-Oriented Programming 8
Constructors in java
• It is a special type of method which is used to initialize
the instance variable of a class.
• Constructor name must be the same name as its class
name.
• A constructor must have no explicit return type.
• A java constructor cannot be abstract, static, and final.
• We can have private , protected, public or default
constructor in java.
3/29/2024 Object-Oriented Programming 9
• There are two types of constructors in java.
3/29/2024 Object-Oriented Programming 10
Default Parameterized
Has no any parameter Has at least one parameter
Generated automatically by the
compiler if the programmer
does not defined it.
Created by the programmer with
one or more parameters.
Known as implicit constructor Known as explicit constructor
Initializes 0 for integrals, null for
strings and false for Boolean
type instance variables.
The programmer provides an
initial value for the instance
variables.
3/29/2024 Object-Oriented Programming 11
3/29/2024 Object-Oriented Programming 12
Encapsulation
• Encapsulation: is the process of binding/enclosing the
instance variables and methods under a single entity/unit.
• Encapsulating or wrapping up of data is called
Encapsulation.
3/29/2024 Object-Oriented Programming 13
• One way to implement encapsulation in Java is to use
‘private’ access specifiers on all the variables inside the
class so that no other class can access it.
• These variables can access only by the methods in which
declared in the same class. It is also known as data
hiding.
• So we have to declare the methods as public to access the
private variables.
3/29/2024 Object-Oriented Programming 14
• Rules for encapsulation in Java
1. Class Variable should be private
2. To access the values of the variables, create public
setter and getter methods.
3/29/2024 Object-Oriented Programming 15
Setter Methods:
• Public methods that set a value for the private instance variables .
• Returns void (i.e nothing).
• Also called “mutator”methods.
Getter methods:
• Public method that displays value of private private variables.
• Also called “accessor” methods
3/29/2024 Object-Oriented Programming 16
• we can’t access the
restricted class attributes
of Employee class.
• Let’s resolve this problem, to do so we have to utilize the
get and set methods for each attribute.
3/29/2024 Object-Oriented Programming 17
3/29/2024 Object-Oriented Programming 18
Inheritance
• Inheritance in java: is a mechanism in which one object
acquires all the properties/variables and
behaviors/methods of parent object.
• Inheritance means creating new classes from existing
classes.
• When you inherit from an existing class , you can reuse
methods and variables of parent class, you can add new
methods and variables also.
• Parent class is known as base class/super class.
• Child class is known as derived class/sub class.
• We use a java keyword extends to make inheritance
relationships. The derived class extends from superclass.
3/29/2024 Object-Oriented Programming 19
Types of Inheritance
3/29/2024 Object-Oriented Programming 20
Is-A: concept of inheritance. So to
make inheritance the classes should
have this relationships. Example:
vehicle is a car.
Has-A: is concept of composition.
The object of one class created in
another class as a member.
Example: car has engine.
Relationships b/n classes in
java
Examples of types of inheritance
3/29/2024 Object-Oriented Programming 21
2. Multilevel
1. Single 3. Hierarchical
Abstraction
• The process of hiding complex internal implementation
details from users and providing only necessary
functionality is called abstraction.
• It removes all non-essential things and shows only
important things to users.
3/29/2024 Object-Oriented Programming 22
3/29/2024 Object-Oriented Programming 23
We all use an ATM machine for cash withdrawal,
money transfer, retrieve min-statement, etc. in
our daily life.
But we don’t know internally what things are
happening inside ATM machine when you insert
an ATM card for performing any kind of
operation.
When you need to send SMS from your mobile,
you only type the text and send the message.
But you don’t know the internal processing of
the message delivery.
• Similarly, happens in Java OOPs. You only need to call the
specific classes or methods to implement specific
program logic, but you don’t know how these classes or
methods function. This concept is known as abstraction
in Java.
• There are two ways to achieve or implement abstraction
in Java program. They are as follows:
1. Abstract class (50%)
2. Interface (100%)
3/29/2024 Object-Oriented Programming 24
Rules of abstract class
1. Class must be declared with abstract keyword to make an abstract class.
2. We can not create an object for an abstract class.
3. If any method is abstract in a class, the class must be declared as abstract.
4. To use methods declared in an abstract class, the abstract class must be extended by an ordinary class and
must implement (override) all abstract methods in that ordinary class.
5. Inside the abstract class, we can create any number of constructors. If you do not create a constructor, the
compiler will create a default constructor.
3/29/2024 Object-Oriented Programming 25
Rules of abstract method
Abstract method can only be declared in an abstract class. The implementation is defined in the subclass.
A non-abstract class cannot have an abstract method, whether it is inherited or declared in Java.
It must not provide a method body/implementation in the abstract class for which it is defined.
Method name and signature must be the same as in the abstract class.
Abstract method cannot be static or final.
It cannot be private because the abstract method must be implemented in the subclass. If we declare it
private, we cannot implement it from outside the class.
3/29/2024 Object-Oriented Programming 26
3/29/2024 Object-Oriented Programming 27
Example
3/29/2024 Object-Oriented Programming 28
• Interfaces in Java:
• In Java, an interface is a blueprint or template of a class.
• It is much similar to the Java class but the only difference
is that it has abstract methods and static constants.
• There can be only abstract methods in an interface, that is
there is no method body inside these abstract methods.
• Unlike a class, you cannot instantiate or create an object
of an interface.
3/29/2024 Object-Oriented Programming 29
• An interface does not contain any constructors, but a
class can.
• An interface cannot contain instance fields. It can only
contain the fields that are declared as both static and
final.
• An interface can not be extended or inherited by a class;
it is implemented by a class.
• Syntax of declaring Interfaces in Java:
3/29/2024 Object-Oriented Programming 30
Advantages of an Interface in Java
• Use interfaces to achieve data abstraction.
• We also use them to support the functionality of multiple
inheritances in Java.
3/29/2024 Object-Oriented Programming 31
3/29/2024 Object-Oriented Programming 32
• Multiple inheritance Implementation in Java by interface
3/29/2024 Object-Oriented Programming 33
• Example
3/29/2024 Object-Oriented Programming 34
Polymorphism in Java
• Polymorphism: is implementing the same thing in
different ways.
• A single object behaves in different ways.
3/29/2024 Object-Oriented Programming 35
• In java, polymorphism is achieved in two different ways.
1. Method overloading(compile time polymorphism)
2. Method overriding(run time polymorphism)
3/29/2024 Object-Oriented Programming 36
3/29/2024 Object-Oriented Programming 37
3/29/2024 Object-Oriented Programming 38
•Thank you!!!
3/29/2024 Object-Oriented Programming 39

More Related Content

Similar to chapter 5 concepts of object oriented programming

software engineer interview questions.pdf
software engineer interview questions.pdfsoftware engineer interview questions.pdf
software engineer interview questions.pdfRaajpootQueen
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardHari kiran G
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxanguraju1
 
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdfmarkbrianBautista
 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptxmrxyz19
 
Introduction to OOP concepts
Introduction to OOP conceptsIntroduction to OOP concepts
Introduction to OOP conceptsAhmed Farag
 
Dev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDevLabs Alliance
 
Top 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETTop 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETDevLabs Alliance
 
Dev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetDev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetdevlabsalliance
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingRatnaJava
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.docJoyce Thomas
 
Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questionsVinay Kumar
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and InterfacesJamsher bhanbhro
 

Similar to chapter 5 concepts of object oriented programming (20)

Chap02
Chap02Chap02
Chap02
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
software engineer interview questions.pdf
software engineer interview questions.pdfsoftware engineer interview questions.pdf
software engineer interview questions.pdf
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptx
 
OOPS Characteristics
OOPS CharacteristicsOOPS Characteristics
OOPS Characteristics
 
Introduction to OOP concepts
Introduction to OOP conceptsIntroduction to OOP concepts
Introduction to OOP concepts
 
Dev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdet
 
Oop
OopOop
Oop
 
Top 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETTop 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDET
 
Dev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetDev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdet
 
core_java.ppt
core_java.pptcore_java.ppt
core_java.ppt
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.doc
 
Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questions
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
 

Recently uploaded

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 

Recently uploaded (20)

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 

chapter 5 concepts of object oriented programming

  • 1. Chapter 5 Concepts of OOP in java
  • 2. Basic Concepts of OOP in java 3/29/2024 Object-Oriented Programming 2 • Object • Class • Abstraction • Encapsulation • Inheritance • Polymorphism
  • 3. Object vs Class 3/29/2024 Object-Oriented Programming 3
  • 4. • An object is any entity that has a state and behavior. • State: It is represented by attributes of an object. It also shows properties of an object. • Behavior: It is represented by methods of an object. It shows response of an object with other objects. 3/29/2024 Object-Oriented Programming 4
  • 5. Creating object ClassName objectName=new className 3/29/2024 Object-Oriented Programming 5 Accessing class members: to access the class members we use the dot operator. Object • objectName.variableName; • objectName.methodName();
  • 7. • Java access modifier often called access specifier. • An access modifier is a keyword in oop languages used to set the accessibility(visibility) of a variables, methods, constructors classes and interfaces. 3/29/2024 Object-Oriented Programming 7
  • 9. Constructors in java • It is a special type of method which is used to initialize the instance variable of a class. • Constructor name must be the same name as its class name. • A constructor must have no explicit return type. • A java constructor cannot be abstract, static, and final. • We can have private , protected, public or default constructor in java. 3/29/2024 Object-Oriented Programming 9
  • 10. • There are two types of constructors in java. 3/29/2024 Object-Oriented Programming 10 Default Parameterized Has no any parameter Has at least one parameter Generated automatically by the compiler if the programmer does not defined it. Created by the programmer with one or more parameters. Known as implicit constructor Known as explicit constructor Initializes 0 for integrals, null for strings and false for Boolean type instance variables. The programmer provides an initial value for the instance variables.
  • 13. Encapsulation • Encapsulation: is the process of binding/enclosing the instance variables and methods under a single entity/unit. • Encapsulating or wrapping up of data is called Encapsulation. 3/29/2024 Object-Oriented Programming 13
  • 14. • One way to implement encapsulation in Java is to use ‘private’ access specifiers on all the variables inside the class so that no other class can access it. • These variables can access only by the methods in which declared in the same class. It is also known as data hiding. • So we have to declare the methods as public to access the private variables. 3/29/2024 Object-Oriented Programming 14
  • 15. • Rules for encapsulation in Java 1. Class Variable should be private 2. To access the values of the variables, create public setter and getter methods. 3/29/2024 Object-Oriented Programming 15 Setter Methods: • Public methods that set a value for the private instance variables . • Returns void (i.e nothing). • Also called “mutator”methods. Getter methods: • Public method that displays value of private private variables. • Also called “accessor” methods
  • 16. 3/29/2024 Object-Oriented Programming 16 • we can’t access the restricted class attributes of Employee class.
  • 17. • Let’s resolve this problem, to do so we have to utilize the get and set methods for each attribute. 3/29/2024 Object-Oriented Programming 17
  • 19. Inheritance • Inheritance in java: is a mechanism in which one object acquires all the properties/variables and behaviors/methods of parent object. • Inheritance means creating new classes from existing classes. • When you inherit from an existing class , you can reuse methods and variables of parent class, you can add new methods and variables also. • Parent class is known as base class/super class. • Child class is known as derived class/sub class. • We use a java keyword extends to make inheritance relationships. The derived class extends from superclass. 3/29/2024 Object-Oriented Programming 19
  • 20. Types of Inheritance 3/29/2024 Object-Oriented Programming 20 Is-A: concept of inheritance. So to make inheritance the classes should have this relationships. Example: vehicle is a car. Has-A: is concept of composition. The object of one class created in another class as a member. Example: car has engine. Relationships b/n classes in java
  • 21. Examples of types of inheritance 3/29/2024 Object-Oriented Programming 21 2. Multilevel 1. Single 3. Hierarchical
  • 22. Abstraction • The process of hiding complex internal implementation details from users and providing only necessary functionality is called abstraction. • It removes all non-essential things and shows only important things to users. 3/29/2024 Object-Oriented Programming 22
  • 23. 3/29/2024 Object-Oriented Programming 23 We all use an ATM machine for cash withdrawal, money transfer, retrieve min-statement, etc. in our daily life. But we don’t know internally what things are happening inside ATM machine when you insert an ATM card for performing any kind of operation. When you need to send SMS from your mobile, you only type the text and send the message. But you don’t know the internal processing of the message delivery.
  • 24. • Similarly, happens in Java OOPs. You only need to call the specific classes or methods to implement specific program logic, but you don’t know how these classes or methods function. This concept is known as abstraction in Java. • There are two ways to achieve or implement abstraction in Java program. They are as follows: 1. Abstract class (50%) 2. Interface (100%) 3/29/2024 Object-Oriented Programming 24
  • 25. Rules of abstract class 1. Class must be declared with abstract keyword to make an abstract class. 2. We can not create an object for an abstract class. 3. If any method is abstract in a class, the class must be declared as abstract. 4. To use methods declared in an abstract class, the abstract class must be extended by an ordinary class and must implement (override) all abstract methods in that ordinary class. 5. Inside the abstract class, we can create any number of constructors. If you do not create a constructor, the compiler will create a default constructor. 3/29/2024 Object-Oriented Programming 25
  • 26. Rules of abstract method Abstract method can only be declared in an abstract class. The implementation is defined in the subclass. A non-abstract class cannot have an abstract method, whether it is inherited or declared in Java. It must not provide a method body/implementation in the abstract class for which it is defined. Method name and signature must be the same as in the abstract class. Abstract method cannot be static or final. It cannot be private because the abstract method must be implemented in the subclass. If we declare it private, we cannot implement it from outside the class. 3/29/2024 Object-Oriented Programming 26
  • 29. • Interfaces in Java: • In Java, an interface is a blueprint or template of a class. • It is much similar to the Java class but the only difference is that it has abstract methods and static constants. • There can be only abstract methods in an interface, that is there is no method body inside these abstract methods. • Unlike a class, you cannot instantiate or create an object of an interface. 3/29/2024 Object-Oriented Programming 29
  • 30. • An interface does not contain any constructors, but a class can. • An interface cannot contain instance fields. It can only contain the fields that are declared as both static and final. • An interface can not be extended or inherited by a class; it is implemented by a class. • Syntax of declaring Interfaces in Java: 3/29/2024 Object-Oriented Programming 30
  • 31. Advantages of an Interface in Java • Use interfaces to achieve data abstraction. • We also use them to support the functionality of multiple inheritances in Java. 3/29/2024 Object-Oriented Programming 31
  • 33. • Multiple inheritance Implementation in Java by interface 3/29/2024 Object-Oriented Programming 33
  • 35. Polymorphism in Java • Polymorphism: is implementing the same thing in different ways. • A single object behaves in different ways. 3/29/2024 Object-Oriented Programming 35
  • 36. • In java, polymorphism is achieved in two different ways. 1. Method overloading(compile time polymorphism) 2. Method overriding(run time polymorphism) 3/29/2024 Object-Oriented Programming 36