SlideShare a Scribd company logo
•
•
•
•

https://www.facebook.com/Oxus20

oxus20@gmail.com

Classes and Objects
Encapsulation
Inheritance
Polymorphism

Object
Oriented
Programming
Abdul Rahman Sherzad
Agenda
» Object Oriented Programming
» Definition and Demo
˃ Class
˃ Object
˃ Encapsulation (Information Hiding)
˃ Inheritance
˃ Polymorphism
˃ The instanceof Operator
2

https://www.facebook.com/Oxus20
Object Oriented Programming (OOP)
» OOP makes it easier for programmers to structure
and form software programs.
» For the reason that individual objects can be
modified without touching other aspects of the
program.
˃ It is also easier to update and modify programs written in object-oriented
languages.

» As software programs have grown larger over the
years, OOP has made developing these large
programs more manageable.
3

https://www.facebook.com/Oxus20
Class Definition
» A class is kind of a blueprint or template that refer to the methods and
attributes that will be in each object.
» BankAccount is a blueprint as follow
˃ State / Attributes
+ Name
+ Account number
+ Type of account
+ Balance
˃ Behaviors / Methods
+ To assign initial value
+ To deposit an account
+ To withdraw an account
+ To display name, account number & balance.
Now you, me and others can have bank accounts (objects / instances) under the above BankAccount
blueprint where each can have different account name, number and balance as well as I can deposit
500USD, you can deposit 300USD, as well as withdraw, etc.
https://www.facebook.com/Oxus20

4
BankAccount Class Example
public class BankAccount {
// BankAccount attributes
private String accountNumber;
private String accountName;
private double balance;
// BankAccount methods
// the constructor
public BankAccount(String accNumber, String accName) {
accountNumber = accNumber;
accountName = accName;
balance = 0;
}
5

https://www.facebook.com/Oxus20
BankAccount Class Example (contd.)
// methods to read the attributes
public String getAccountName() {
return accountName;
}
public String getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}

6

https://www.facebook.com/Oxus20
BankAccount Class Example (contd.)
// methods to deposit and withdraw money
public boolean deposit(double amount) {
if (amount > 0) {
balance = balance + amount;
return true;
} else {
return false;
}
}
public boolean withdraw(double amount) {
if (amount > balance) {
return false;
} else {
balance = balance - amount;
return true;
}
}
7

}
https://www.facebook.com/Oxus20
Object Definition
» An object holds both variables and methods; one object
might represent you, a second object might represent
me.
» Consider the class of Man and Woman
˃ Me and You are objects

» An Object is a particular instance of a given class. When
you open a bank account; an object or instance of the
class BankAccount will be created for you.
https://www.facebook.com/Oxus20

8
BankAccount Object Example
BankAccount absherzad = new BankAccount("20120",
"Abdul Rahman Sherzad");

» absherzad is an object of BankAccount with Account Number of

"20120" and Account Name of "Abdul Rahman Sherzad".
» absherzad object can deposit, withdraw as well as check the

balance.
9

https://www.facebook.com/Oxus20
BankAccount Demo
public class BankAccountDemo {
public static void main(String[] args) {
BankAccount absherzad = new BankAccount("20120",

"Abdul Rahman Sherzad");

absherzad.deposit(500);
absherzad.deposit(1500);
System.out.println("Balance is: " + absherzad.getBalance()); //2000

absherzad.withdraw(400);
System.out.println("Balance is: " + absherzad.getBalance()); //1600
}
}
10

https://www.facebook.com/Oxus20
Encapsulation Definition
» Encapsulation is the technique of making the class
attributes private and providing access to the attributes
via public methods.
» If attributes are declared private, it cannot be accessed
by anyone outside the class.
˃ For this reason, encapsulation is also referred to as Data Hiding (Information Hiding).

» Encapsulation can be described as a protective barrier
that prevents the code and data corruption.
» The main benefit of encapsulation is the ability to
modify our implemented code without breaking the
code of others who use our code.
https://www.facebook.com/Oxus20

11
Encapsulation Benefit Demo
public class EncapsulationDemo {
public static void main(String[] args) {
BankAccount absherzad = new BankAccount("20120",
absherzad.deposit(500);

"Abdul Rahman Sherzad");

// Not possible because withdraw() check the withdraw amount with balance
// Because current balance is 500 and less than withdraw amount is 1000

absherzad.withdraw(1000); // Encapsulation Benefit

// Not possible because class balance is private
// and not accessible directly outside the BankAccount class
absherzad.balance = 120000; // Encapsulation Benefit
}
}
12

https://www.facebook.com/Oxus20
Inheritance Definition
» inheritance is a mechanism for enhancing existing
classes
˃ Inheritance is one of the other most powerful techniques of object-oriented
programming
˃ Inheritance allows for large-scale code reuse

» with inheritance, you can derive a new class from an

existing one
˃ automatically inherit all of the attributes and methods of the existing class
˃ only need to add attributes and / or methods for new functionality
13

https://www.facebook.com/Oxus20
BankAccount Inheritance Example
» Savings Account is a
bank account with
interest
» Checking Account is a

bank account with
transaction fees
14

https://www.facebook.com/Oxus20
Specialty Bank Accounts
» now we want to implement SavingsAccount and
CheckingAccount
˃ A SavingsAccount is a bank account with an associated interest
rate, interest is calculated and added to the balance periodically
˃ could copy-and-paste the code for BankAccount, then add an
attribute for interest rate and a method for adding interest

˃ A CheckingAccount is a bank account with some number of free
transactions, with a fee charged for subsequent transactions
˃ could copy-and-paste the code for BankAccount, then add an
attribute to keep track of the number of transactions and a
method for deducting fees
15

https://www.facebook.com/Oxus20
Disadvantages of the copy-and-paste Approach

» tedious and boring work
» lots of duplicate and redundant code
˃ if you change the code in one place, you have to change it
everywhere or else lose consistency (e.g., add customer

name to the bank account info)

» limits polymorphism (will explain later)
16

https://www.facebook.com/Oxus20
SavingsAccount class (inheritance provides a better solution)
» SavingsAccount can be defined to be a special kind of BankAccount
» Automatically inherit common features (balance, account #, account name,
deposit, withdraw)
» Simply add the new features specific to a SavingsAccount
» Need to store interest rate, provide method for adding interest to the
balance
» General form for inheritance:
public class DERIVED_CLASS extends EXISTING_CLASS {
ADDITIONAL_ATTRIBUTES
ADDITIONAL_METHODS
}

17

https://www.facebook.com/Oxus20
SavingsAccount Class
public class SavingsAccount extends BankAccount {
private double interestRate;
public SavingsAccount(String accNumber, String accName, double rate) {

super(accNumber, accName);
interestRate = rate;
}
public void addInterest() {
double interest = getBalance() * interestRate / 100;
this.deposit(interest);
}
}
18

https://www.facebook.com/Oxus20
SavingsAccount Demo
public class SavingsAccountDemo {
public static void main(String[] args) {
SavingsAccount saving = new SavingsAccount("20120",
"Abdul Rahman Sherzad", 10);
// deposit() is inherited from BankAccount (PARENT CLASS)

saving.deposit(500);
// getBalance() is also inherited from BankAccount (PARENT CLASS)

System.out.println("Before Interest: " + saving.getBalance());
saving.addInterest();
System.out.println("After Interest: " + saving.getBalance());
}
}

19

https://www.facebook.com/Oxus20
CheckingAccount Class
public class CheckingAccount extends BankAccount {
private int transactionCount;
private static final int NUM_FREE = 3;
private static final double TRANS_FEE = 2.0;
public CheckingAccount(String accNumber, String accName) {
super(accNumber, accName);
transactionCount = 0;
}
public boolean deposit(double amount) {
if (super.deposit(amount)) {
transactionCount++;
return true;
}
return false;
}

20

https://www.facebook.com/Oxus20
CheckingAccount Class (contd.)
public boolean withdraw(double amount) {
if (super.withdraw(amount)) {
transactionCount++;
return true;
}
return false;
}
public void deductFees() {
if (transactionCount > NUM_FREE) {
double fees = TRANS_FEE * (transactionCount - NUM_FREE);
if (super.withdraw(fees)) {
transactionCount = 0;
}
}
}
}
https://www.facebook.com/Oxus20

21
CheckingAccount Demo
public class CheckingAccountDemo {
public static void main(String[] args) {
CheckingAccount checking = new CheckingAccount("20120",
"Abdul Rahman Sherzad");
checking.deposit(500);
checking.withdraw(200);
checking.deposit(700);
// No deduction fee because we had only 3 transactions

checking.deductFees();
System.out.println("transactions <= 3: " + checking.getBalance());
// One more transaction

checking.deposit(200);
// Deduction fee occurs because we have had 4 transactions

checking.deductFees();
System.out.println("transactions > 3: " + checking.getBalance());
}

22

}
https://www.facebook.com/Oxus20
Polymorphism Definition
» Polymorphism is the ability of an object to take
on many forms.

» The most common use of polymorphism in OOP
occurs when a parent class reference is used to
refer to a child class object.
23

https://www.facebook.com/Oxus20
Polymorphism In BankAccount
» A SavingsAccount IS_A BankAccount (with some extra functionality)
» A CheckingAccount IS_A BankAccount (with some extra functionality)

» Whatever you can do to a BankAccount (i.e. deposit, withdraw); you can
do with a SavingsAccount or CheckingAccount
» Derived classes can certainly do more (i.e. addInterest) for
SavingsAccount)
» Derived classes may do things differently (i.e. deposit for
CheckingAccount)
24

https://www.facebook.com/Oxus20
Polymorphism In BankAccount Example
public class BankAccountPolymorphismDemo {
public static void main(String[] args) {
BankAccount firstAccount = new SavingsAccount("20120",
"Abdul Rahman Sherzad", 10);
BankAccount secondAccount = new CheckingAccount("20120",
"Abdul Rahman Sherzad");
// calls the method defined in BankAccount

firstAccount.deposit(100.0);
// calls the method defined in CheckingAccount
// because deposit() is overridden in CheckingAccount

secondAccount.deposit(100.0);
}
}
25

https://www.facebook.com/Oxus20
Instanceof Operator
» if you need to determine the specific type
of an object
˃ use the instanceof operator
˃ can then downcast from the general to the more specific type
˃ For example the object from BankAccount Parent Class will be
casted to either SavingsAccount and/or CheckingAccount

26

https://www.facebook.com/Oxus20
END

27

https://www.facebook.com/Oxus20

More Related Content

PPTX
Fragment
nationalmobileapps
 
PPTX
Crystal report
Everywhere
 
PDF
SPL 9 | Scope of Variables in C
Mohammad Imam Hossain
 
PPT
Inheritance and Polymorphism
BG Java EE Course
 
PDF
Arrays in Java
Naz Abdalla
 
DOC
programming in C++ report
vikram mahendra
 
PPTX
array of object pointer in c++
Arpita Patel
 
PDF
06. operator overloading
Haresh Jaiswal
 
Crystal report
Everywhere
 
SPL 9 | Scope of Variables in C
Mohammad Imam Hossain
 
Inheritance and Polymorphism
BG Java EE Course
 
Arrays in Java
Naz Abdalla
 
programming in C++ report
vikram mahendra
 
array of object pointer in c++
Arpita Patel
 
06. operator overloading
Haresh Jaiswal
 

What's hot (20)

PPT
Collections Framework
Sunil OS
 
PPTX
Exception handling
PhD Research Scholar
 
PPTX
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
PDF
Python lambda functions with filter, map & reduce function
ARVIND PANDE
 
PPT
JAVA OOP
Sunil OS
 
PPT
Object-oriented concepts
BG Java EE Course
 
PPTX
This pointer
Kamal Acharya
 
PPTX
Validation Controls in asp.net
Deep Patel
 
ODP
Datatype in JavaScript
Rajat Saxena
 
PPT
Queue implementation
Rajendran
 
PPT
android layouts
Deepa Rani
 
PDF
04. constructor & destructor
Haresh Jaiswal
 
PPT
JDBC
Sunil OS
 
PPTX
Call by value or call by reference in C++
Sachin Yadav
 
ODP
Android App Development - 05 Action bar
Diego Grancini
 
PPTX
Data types in python
RaginiJain21
 
PDF
Managing I/O in c++
Pranali Chaudhari
 
PPTX
Polymorphism in c++(ppt)
Sanjit Shaw
 
PPTX
Introduction to numpy Session 1
Jatin Miglani
 
PPT
Functions in C++
Sachin Sharma
 
Collections Framework
Sunil OS
 
Exception handling
PhD Research Scholar
 
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Python lambda functions with filter, map & reduce function
ARVIND PANDE
 
JAVA OOP
Sunil OS
 
Object-oriented concepts
BG Java EE Course
 
This pointer
Kamal Acharya
 
Validation Controls in asp.net
Deep Patel
 
Datatype in JavaScript
Rajat Saxena
 
Queue implementation
Rajendran
 
android layouts
Deepa Rani
 
04. constructor & destructor
Haresh Jaiswal
 
JDBC
Sunil OS
 
Call by value or call by reference in C++
Sachin Yadav
 
Android App Development - 05 Action bar
Diego Grancini
 
Data types in python
RaginiJain21
 
Managing I/O in c++
Pranali Chaudhari
 
Polymorphism in c++(ppt)
Sanjit Shaw
 
Introduction to numpy Session 1
Jatin Miglani
 
Functions in C++
Sachin Sharma
 
Ad

Viewers also liked (20)

PDF
Java Arrays
OXUS 20
 
PDF
Java Applet and Graphics
OXUS 20
 
PDF
PHP Basic and Fundamental Questions and Answers with Detail Explanation
OXUS 20
 
PDF
Java Regular Expression PART I
OXUS 20
 
PDF
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
OXUS 20
 
PDF
Java Regular Expression PART II
OXUS 20
 
PPTX
TKP Java Notes for Teaching Kids Programming
Lynn Langit
 
PDF
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
OXUS 20
 
PPTX
Conditional Statement
OXUS 20
 
PDF
Java Unicode with Cool GUI Examples
OXUS 20
 
PDF
Object Oriented Concept Static vs. Non Static
OXUS 20
 
PDF
Java Guessing Game Number Tutorial
OXUS 20
 
PPTX
Structure programming – Java Programming – Theory
OXUS 20
 
PDF
Create Splash Screen with Java Step by Step
OXUS 20
 
PDF
Web Design and Development Life Cycle and Technologies
OXUS 20
 
PDF
Note - Java Remote Debug
boyw165
 
PDF
Everything about Database JOINS and Relationships
OXUS 20
 
DOCX
Core java notes with examples
bindur87
 
PDF
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Abdul Rahman Sherzad
 
PDF
Java Unicode with Live GUI Examples
Abdul Rahman Sherzad
 
Java Arrays
OXUS 20
 
Java Applet and Graphics
OXUS 20
 
PHP Basic and Fundamental Questions and Answers with Detail Explanation
OXUS 20
 
Java Regular Expression PART I
OXUS 20
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
OXUS 20
 
Java Regular Expression PART II
OXUS 20
 
TKP Java Notes for Teaching Kids Programming
Lynn Langit
 
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
OXUS 20
 
Conditional Statement
OXUS 20
 
Java Unicode with Cool GUI Examples
OXUS 20
 
Object Oriented Concept Static vs. Non Static
OXUS 20
 
Java Guessing Game Number Tutorial
OXUS 20
 
Structure programming – Java Programming – Theory
OXUS 20
 
Create Splash Screen with Java Step by Step
OXUS 20
 
Web Design and Development Life Cycle and Technologies
OXUS 20
 
Note - Java Remote Debug
boyw165
 
Everything about Database JOINS and Relationships
OXUS 20
 
Core java notes with examples
bindur87
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Abdul Rahman Sherzad
 
Java Unicode with Live GUI Examples
Abdul Rahman Sherzad
 
Ad

Similar to Everything about Object Oriented Programming (20)

PPT
06 inheritance
ROHINI KOLI
 
PPT
CHAPTER 1 - OVERVIEW OOP.ppt
NgoHuuNhan1
 
PPT
ObjectOrientedSystems.ppt
ChishaleFriday
 
PPTX
3_ObjectOrientedSystems.pptx
RokaKaram
 
PDF
Improving application design with a rich domain model (springone 2007)
Chris Richardson
 
PPTX
Object Oriented Programming
RAJU MAKWANA
 
PDF
"An introduction to object-oriented programming for those who have never done...
Fwdays
 
PDF
Inheritance
FALLEE31188
 
PPTX
Real Object-Oriented Programming: Empirically Validated Benefits of the DCI P...
James Coplien
 
PPT
003 OOP Concepts.ppt
MonishaAb1
 
PPTX
Is2215 lecture4 student (1)
dannygriff1
 
PPTX
Inheritance
Harry Potter
 
PPTX
Inheritance
Tony Nguyen
 
PPTX
Inheritance
Luis Goldster
 
PPTX
Inheritance
James Wong
 
PPTX
Inheritance
Young Alista
 
PPTX
Inheritance
Hoang Nguyen
 
PPTX
Inheritance
Fraboni Ec
 
PPT
004 Java Classes.ppt
HarshitShukla539244
 
06 inheritance
ROHINI KOLI
 
CHAPTER 1 - OVERVIEW OOP.ppt
NgoHuuNhan1
 
ObjectOrientedSystems.ppt
ChishaleFriday
 
3_ObjectOrientedSystems.pptx
RokaKaram
 
Improving application design with a rich domain model (springone 2007)
Chris Richardson
 
Object Oriented Programming
RAJU MAKWANA
 
"An introduction to object-oriented programming for those who have never done...
Fwdays
 
Inheritance
FALLEE31188
 
Real Object-Oriented Programming: Empirically Validated Benefits of the DCI P...
James Coplien
 
003 OOP Concepts.ppt
MonishaAb1
 
Is2215 lecture4 student (1)
dannygriff1
 
Inheritance
Harry Potter
 
Inheritance
Tony Nguyen
 
Inheritance
Luis Goldster
 
Inheritance
James Wong
 
Inheritance
Young Alista
 
Inheritance
Hoang Nguyen
 
Inheritance
Fraboni Ec
 
004 Java Classes.ppt
HarshitShukla539244
 

More from Abdul Rahman Sherzad (20)

PDF
Data is the Fuel of Organizations: Opportunities and Challenges in Afghanistan
Abdul Rahman Sherzad
 
PDF
PHP Unicode Input Validation Snippets
Abdul Rahman Sherzad
 
PDF
Iterations and Recursions
Abdul Rahman Sherzad
 
PDF
Sorting Alpha Numeric Data in MySQL
Abdul Rahman Sherzad
 
PDF
PHP Variable variables Examples
Abdul Rahman Sherzad
 
PDF
Cross Join Example and Applications
Abdul Rahman Sherzad
 
PDF
Applicability of Educational Data Mining in Afghanistan: Opportunities and Ch...
Abdul Rahman Sherzad
 
PDF
Web Application Security and Awareness
Abdul Rahman Sherzad
 
PDF
Database Automation with MySQL Triggers and Event Schedulers
Abdul Rahman Sherzad
 
PDF
Mobile Score Notification System
Abdul Rahman Sherzad
 
PDF
Herat Innovation Lab 2015
Abdul Rahman Sherzad
 
PDF
Evaluation of Existing Web Structure of Afghan Universities
Abdul Rahman Sherzad
 
PDF
PHP Basic and Fundamental Questions and Answers with Detail Explanation
Abdul Rahman Sherzad
 
PDF
Java Applet and Graphics
Abdul Rahman Sherzad
 
PDF
Fundamentals of Database Systems Questions and Answers
Abdul Rahman Sherzad
 
PDF
Everything about Database JOINS and Relationships
Abdul Rahman Sherzad
 
PDF
Create Splash Screen with Java Step by Step
Abdul Rahman Sherzad
 
PDF
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Abdul Rahman Sherzad
 
PDF
Web Design and Development Life Cycle and Technologies
Abdul Rahman Sherzad
 
PDF
Java Regular Expression PART II
Abdul Rahman Sherzad
 
Data is the Fuel of Organizations: Opportunities and Challenges in Afghanistan
Abdul Rahman Sherzad
 
PHP Unicode Input Validation Snippets
Abdul Rahman Sherzad
 
Iterations and Recursions
Abdul Rahman Sherzad
 
Sorting Alpha Numeric Data in MySQL
Abdul Rahman Sherzad
 
PHP Variable variables Examples
Abdul Rahman Sherzad
 
Cross Join Example and Applications
Abdul Rahman Sherzad
 
Applicability of Educational Data Mining in Afghanistan: Opportunities and Ch...
Abdul Rahman Sherzad
 
Web Application Security and Awareness
Abdul Rahman Sherzad
 
Database Automation with MySQL Triggers and Event Schedulers
Abdul Rahman Sherzad
 
Mobile Score Notification System
Abdul Rahman Sherzad
 
Herat Innovation Lab 2015
Abdul Rahman Sherzad
 
Evaluation of Existing Web Structure of Afghan Universities
Abdul Rahman Sherzad
 
PHP Basic and Fundamental Questions and Answers with Detail Explanation
Abdul Rahman Sherzad
 
Java Applet and Graphics
Abdul Rahman Sherzad
 
Fundamentals of Database Systems Questions and Answers
Abdul Rahman Sherzad
 
Everything about Database JOINS and Relationships
Abdul Rahman Sherzad
 
Create Splash Screen with Java Step by Step
Abdul Rahman Sherzad
 
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Abdul Rahman Sherzad
 
Web Design and Development Life Cycle and Technologies
Abdul Rahman Sherzad
 
Java Regular Expression PART II
Abdul Rahman Sherzad
 

Recently uploaded (20)

DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PDF
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Landforms and landscapes data surprise preview
jpinnuck
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Congenital Hypothyroidism pptx
AneetaSharma15
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 

Everything about Object Oriented Programming

  • 2. Agenda » Object Oriented Programming » Definition and Demo ˃ Class ˃ Object ˃ Encapsulation (Information Hiding) ˃ Inheritance ˃ Polymorphism ˃ The instanceof Operator 2 https://www.facebook.com/Oxus20
  • 3. Object Oriented Programming (OOP) » OOP makes it easier for programmers to structure and form software programs. » For the reason that individual objects can be modified without touching other aspects of the program. ˃ It is also easier to update and modify programs written in object-oriented languages. » As software programs have grown larger over the years, OOP has made developing these large programs more manageable. 3 https://www.facebook.com/Oxus20
  • 4. Class Definition » A class is kind of a blueprint or template that refer to the methods and attributes that will be in each object. » BankAccount is a blueprint as follow ˃ State / Attributes + Name + Account number + Type of account + Balance ˃ Behaviors / Methods + To assign initial value + To deposit an account + To withdraw an account + To display name, account number & balance. Now you, me and others can have bank accounts (objects / instances) under the above BankAccount blueprint where each can have different account name, number and balance as well as I can deposit 500USD, you can deposit 300USD, as well as withdraw, etc. https://www.facebook.com/Oxus20 4
  • 5. BankAccount Class Example public class BankAccount { // BankAccount attributes private String accountNumber; private String accountName; private double balance; // BankAccount methods // the constructor public BankAccount(String accNumber, String accName) { accountNumber = accNumber; accountName = accName; balance = 0; } 5 https://www.facebook.com/Oxus20
  • 6. BankAccount Class Example (contd.) // methods to read the attributes public String getAccountName() { return accountName; } public String getAccountNumber() { return accountNumber; } public double getBalance() { return balance; } 6 https://www.facebook.com/Oxus20
  • 7. BankAccount Class Example (contd.) // methods to deposit and withdraw money public boolean deposit(double amount) { if (amount > 0) { balance = balance + amount; return true; } else { return false; } } public boolean withdraw(double amount) { if (amount > balance) { return false; } else { balance = balance - amount; return true; } } 7 } https://www.facebook.com/Oxus20
  • 8. Object Definition » An object holds both variables and methods; one object might represent you, a second object might represent me. » Consider the class of Man and Woman ˃ Me and You are objects » An Object is a particular instance of a given class. When you open a bank account; an object or instance of the class BankAccount will be created for you. https://www.facebook.com/Oxus20 8
  • 9. BankAccount Object Example BankAccount absherzad = new BankAccount("20120", "Abdul Rahman Sherzad"); » absherzad is an object of BankAccount with Account Number of "20120" and Account Name of "Abdul Rahman Sherzad". » absherzad object can deposit, withdraw as well as check the balance. 9 https://www.facebook.com/Oxus20
  • 10. BankAccount Demo public class BankAccountDemo { public static void main(String[] args) { BankAccount absherzad = new BankAccount("20120", "Abdul Rahman Sherzad"); absherzad.deposit(500); absherzad.deposit(1500); System.out.println("Balance is: " + absherzad.getBalance()); //2000 absherzad.withdraw(400); System.out.println("Balance is: " + absherzad.getBalance()); //1600 } } 10 https://www.facebook.com/Oxus20
  • 11. Encapsulation Definition » Encapsulation is the technique of making the class attributes private and providing access to the attributes via public methods. » If attributes are declared private, it cannot be accessed by anyone outside the class. ˃ For this reason, encapsulation is also referred to as Data Hiding (Information Hiding). » Encapsulation can be described as a protective barrier that prevents the code and data corruption. » The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. https://www.facebook.com/Oxus20 11
  • 12. Encapsulation Benefit Demo public class EncapsulationDemo { public static void main(String[] args) { BankAccount absherzad = new BankAccount("20120", absherzad.deposit(500); "Abdul Rahman Sherzad"); // Not possible because withdraw() check the withdraw amount with balance // Because current balance is 500 and less than withdraw amount is 1000 absherzad.withdraw(1000); // Encapsulation Benefit // Not possible because class balance is private // and not accessible directly outside the BankAccount class absherzad.balance = 120000; // Encapsulation Benefit } } 12 https://www.facebook.com/Oxus20
  • 13. Inheritance Definition » inheritance is a mechanism for enhancing existing classes ˃ Inheritance is one of the other most powerful techniques of object-oriented programming ˃ Inheritance allows for large-scale code reuse » with inheritance, you can derive a new class from an existing one ˃ automatically inherit all of the attributes and methods of the existing class ˃ only need to add attributes and / or methods for new functionality 13 https://www.facebook.com/Oxus20
  • 14. BankAccount Inheritance Example » Savings Account is a bank account with interest » Checking Account is a bank account with transaction fees 14 https://www.facebook.com/Oxus20
  • 15. Specialty Bank Accounts » now we want to implement SavingsAccount and CheckingAccount ˃ A SavingsAccount is a bank account with an associated interest rate, interest is calculated and added to the balance periodically ˃ could copy-and-paste the code for BankAccount, then add an attribute for interest rate and a method for adding interest ˃ A CheckingAccount is a bank account with some number of free transactions, with a fee charged for subsequent transactions ˃ could copy-and-paste the code for BankAccount, then add an attribute to keep track of the number of transactions and a method for deducting fees 15 https://www.facebook.com/Oxus20
  • 16. Disadvantages of the copy-and-paste Approach » tedious and boring work » lots of duplicate and redundant code ˃ if you change the code in one place, you have to change it everywhere or else lose consistency (e.g., add customer name to the bank account info) » limits polymorphism (will explain later) 16 https://www.facebook.com/Oxus20
  • 17. SavingsAccount class (inheritance provides a better solution) » SavingsAccount can be defined to be a special kind of BankAccount » Automatically inherit common features (balance, account #, account name, deposit, withdraw) » Simply add the new features specific to a SavingsAccount » Need to store interest rate, provide method for adding interest to the balance » General form for inheritance: public class DERIVED_CLASS extends EXISTING_CLASS { ADDITIONAL_ATTRIBUTES ADDITIONAL_METHODS } 17 https://www.facebook.com/Oxus20
  • 18. SavingsAccount Class public class SavingsAccount extends BankAccount { private double interestRate; public SavingsAccount(String accNumber, String accName, double rate) { super(accNumber, accName); interestRate = rate; } public void addInterest() { double interest = getBalance() * interestRate / 100; this.deposit(interest); } } 18 https://www.facebook.com/Oxus20
  • 19. SavingsAccount Demo public class SavingsAccountDemo { public static void main(String[] args) { SavingsAccount saving = new SavingsAccount("20120", "Abdul Rahman Sherzad", 10); // deposit() is inherited from BankAccount (PARENT CLASS) saving.deposit(500); // getBalance() is also inherited from BankAccount (PARENT CLASS) System.out.println("Before Interest: " + saving.getBalance()); saving.addInterest(); System.out.println("After Interest: " + saving.getBalance()); } } 19 https://www.facebook.com/Oxus20
  • 20. CheckingAccount Class public class CheckingAccount extends BankAccount { private int transactionCount; private static final int NUM_FREE = 3; private static final double TRANS_FEE = 2.0; public CheckingAccount(String accNumber, String accName) { super(accNumber, accName); transactionCount = 0; } public boolean deposit(double amount) { if (super.deposit(amount)) { transactionCount++; return true; } return false; } 20 https://www.facebook.com/Oxus20
  • 21. CheckingAccount Class (contd.) public boolean withdraw(double amount) { if (super.withdraw(amount)) { transactionCount++; return true; } return false; } public void deductFees() { if (transactionCount > NUM_FREE) { double fees = TRANS_FEE * (transactionCount - NUM_FREE); if (super.withdraw(fees)) { transactionCount = 0; } } } } https://www.facebook.com/Oxus20 21
  • 22. CheckingAccount Demo public class CheckingAccountDemo { public static void main(String[] args) { CheckingAccount checking = new CheckingAccount("20120", "Abdul Rahman Sherzad"); checking.deposit(500); checking.withdraw(200); checking.deposit(700); // No deduction fee because we had only 3 transactions checking.deductFees(); System.out.println("transactions <= 3: " + checking.getBalance()); // One more transaction checking.deposit(200); // Deduction fee occurs because we have had 4 transactions checking.deductFees(); System.out.println("transactions > 3: " + checking.getBalance()); } 22 } https://www.facebook.com/Oxus20
  • 23. Polymorphism Definition » Polymorphism is the ability of an object to take on many forms. » The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. 23 https://www.facebook.com/Oxus20
  • 24. Polymorphism In BankAccount » A SavingsAccount IS_A BankAccount (with some extra functionality) » A CheckingAccount IS_A BankAccount (with some extra functionality) » Whatever you can do to a BankAccount (i.e. deposit, withdraw); you can do with a SavingsAccount or CheckingAccount » Derived classes can certainly do more (i.e. addInterest) for SavingsAccount) » Derived classes may do things differently (i.e. deposit for CheckingAccount) 24 https://www.facebook.com/Oxus20
  • 25. Polymorphism In BankAccount Example public class BankAccountPolymorphismDemo { public static void main(String[] args) { BankAccount firstAccount = new SavingsAccount("20120", "Abdul Rahman Sherzad", 10); BankAccount secondAccount = new CheckingAccount("20120", "Abdul Rahman Sherzad"); // calls the method defined in BankAccount firstAccount.deposit(100.0); // calls the method defined in CheckingAccount // because deposit() is overridden in CheckingAccount secondAccount.deposit(100.0); } } 25 https://www.facebook.com/Oxus20
  • 26. Instanceof Operator » if you need to determine the specific type of an object ˃ use the instanceof operator ˃ can then downcast from the general to the more specific type ˃ For example the object from BankAccount Parent Class will be casted to either SavingsAccount and/or CheckingAccount 26 https://www.facebook.com/Oxus20