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

What's hot (20)

javascript objects
javascript objectsjavascript objects
javascript objects
Vijay Kalyan
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
rehaniltifat
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 
Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism in Python
Home
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
 
OO Metrics
OO MetricsOO Metrics
OO Metrics
skmetz
 
Java constructors
Java constructorsJava constructors
Java constructors
QUONTRASOLUTIONS
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
Damian T. Gordon
 
Constructor
ConstructorConstructor
Constructor
poonamchopra7975
 
MySQL Views
MySQL ViewsMySQL Views
MySQL Views
Reggie Niccolo Santos
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
Sunil OS
 
02 Writing Executable Statments
02 Writing Executable Statments02 Writing Executable Statments
02 Writing Executable Statments
rehaniltifat
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAChapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Maulik Borsaniya
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
Binay Kumar Ray
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
Kurapati Vishwak
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
Ashita Agrawal
 
Chapter 4 Structured Query Language
Chapter 4 Structured Query LanguageChapter 4 Structured Query Language
Chapter 4 Structured Query Language
Eddyzulham Mahluzydde
 
Function & procedure
Function & procedureFunction & procedure
Function & procedure
atishupadhyay
 
javascript objects
javascript objectsjavascript objects
javascript objects
Vijay Kalyan
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
rehaniltifat
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 
Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism in Python
Home
 
OO Metrics
OO MetricsOO Metrics
OO Metrics
skmetz
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
Damian T. Gordon
 
02 Writing Executable Statments
02 Writing Executable Statments02 Writing Executable Statments
02 Writing Executable Statments
rehaniltifat
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAChapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Maulik Borsaniya
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
Kurapati Vishwak
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
Ashita Agrawal
 
Function & procedure
Function & procedureFunction & procedure
Function & procedure
atishupadhyay
 

Similar to Object Oriented Programming with Real World Examples (20)

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

More from OXUS 20 (20)

Java Arrays
Java ArraysJava Arrays
Java Arrays
OXUS 20
 
Java Methods
Java MethodsJava Methods
Java Methods
OXUS 20
 
Conditional Statement
Conditional Statement Conditional Statement
Conditional Statement
OXUS 20
 
Structure programming – Java Programming – Theory
Structure programming – Java Programming – TheoryStructure programming – Java Programming – Theory
Structure programming – Java Programming – Theory
OXUS 20
 
PHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail ExplanationPHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail Explanation
OXUS 20
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and Graphics
OXUS 20
 
Fundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and AnswersFundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and Answers
OXUS 20
 
Everything about Database JOINS and Relationships
Everything about Database JOINS and RelationshipsEverything about Database JOINS and Relationships
Everything about Database JOINS and Relationships
OXUS 20
 
Create Splash Screen with Java Step by Step
Create Splash Screen with Java Step by StepCreate Splash Screen with Java Step by Step
Create Splash Screen with Java Step by Step
OXUS 20
 
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using JavaFal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
OXUS 20
 
Web Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and TechnologiesWeb Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and Technologies
OXUS 20
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesJava Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
OXUS 20
 
Java Unicode with Cool GUI Examples
Java Unicode with Cool GUI ExamplesJava Unicode with Cool GUI Examples
Java Unicode with Cool GUI Examples
OXUS 20
 
JAVA GUI PART III
JAVA GUI PART IIIJAVA GUI PART III
JAVA GUI PART III
OXUS 20
 
Java Regular Expression PART II
Java Regular Expression PART IIJava Regular Expression PART II
Java Regular Expression PART II
OXUS 20
 
Java Regular Expression PART I
Java Regular Expression PART IJava Regular Expression PART I
Java Regular Expression PART I
OXUS 20
 
Java GUI PART II
Java GUI PART IIJava GUI PART II
Java GUI PART II
OXUS 20
 
JAVA GUI PART I
JAVA GUI PART IJAVA GUI PART I
JAVA GUI PART I
OXUS 20
 
Java Guessing Game Number Tutorial
Java Guessing Game Number TutorialJava Guessing Game Number Tutorial
Java Guessing Game Number Tutorial
OXUS 20
 
JAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART IIIJAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART III
OXUS 20
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
OXUS 20
 
Java Methods
Java MethodsJava Methods
Java Methods
OXUS 20
 
Conditional Statement
Conditional Statement Conditional Statement
Conditional Statement
OXUS 20
 
Structure programming – Java Programming – Theory
Structure programming – Java Programming – TheoryStructure programming – Java Programming – Theory
Structure programming – Java Programming – Theory
OXUS 20
 
PHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail ExplanationPHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail Explanation
OXUS 20
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and Graphics
OXUS 20
 
Fundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and AnswersFundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and Answers
OXUS 20
 
Everything about Database JOINS and Relationships
Everything about Database JOINS and RelationshipsEverything about Database JOINS and Relationships
Everything about Database JOINS and Relationships
OXUS 20
 
Create Splash Screen with Java Step by Step
Create Splash Screen with Java Step by StepCreate Splash Screen with Java Step by Step
Create Splash Screen with Java Step by Step
OXUS 20
 
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using JavaFal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
OXUS 20
 
Web Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and TechnologiesWeb Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and Technologies
OXUS 20
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesJava Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
OXUS 20
 
Java Unicode with Cool GUI Examples
Java Unicode with Cool GUI ExamplesJava Unicode with Cool GUI Examples
Java Unicode with Cool GUI Examples
OXUS 20
 
JAVA GUI PART III
JAVA GUI PART IIIJAVA GUI PART III
JAVA GUI PART III
OXUS 20
 
Java Regular Expression PART II
Java Regular Expression PART IIJava Regular Expression PART II
Java Regular Expression PART II
OXUS 20
 
Java Regular Expression PART I
Java Regular Expression PART IJava Regular Expression PART I
Java Regular Expression PART I
OXUS 20
 
Java GUI PART II
Java GUI PART IIJava GUI PART II
Java GUI PART II
OXUS 20
 
JAVA GUI PART I
JAVA GUI PART IJAVA GUI PART I
JAVA GUI PART I
OXUS 20
 
Java Guessing Game Number Tutorial
Java Guessing Game Number TutorialJava Guessing Game Number Tutorial
Java Guessing Game Number Tutorial
OXUS 20
 
JAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART IIIJAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART III
OXUS 20
 
Ad

Recently uploaded (20)

How to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRMHow to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRM
Celine George
 
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
National Information Standards Organization (NISO)
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
Quiz Club of PSG College of Arts & Science
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
State institute of educational technology
State institute of educational technologyState institute of educational technology
State institute of educational technology
vp5806484
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
LDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDMMIA Reiki Yoga S8 Free Workshop Grad LevelLDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDM & Mia eStudios
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in IndiaSmart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
fincrifcontent
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
How to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRMHow to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRM
Celine George
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
State institute of educational technology
State institute of educational technologyState institute of educational technology
State institute of educational technology
vp5806484
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
LDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDMMIA Reiki Yoga S8 Free Workshop Grad LevelLDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDM & Mia eStudios
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in IndiaSmart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
fincrifcontent
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 

Object Oriented Programming with Real World Examples

  • 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