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

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
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
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 

Recently uploaded (20)

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
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
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 

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