Everything about Object Oriented Programming

27
Object Oriented Programming Classes and Objects Encapsulation Inheritance Polymorphism Abdul Rahman Sherzad https://www.facebook.com/Oxus20 [email protected]

description

Object Oriented Programming (OOP) makes it easier for programmers to structure and form software programs; since individual objects can be modified without touching other aspects of the program. With this presentation we are covering most OOP features (Class, Object, Inheritance, Polymorphism, Encapsulation, etc.) with real life examples.

Transcript of Everything about Object Oriented Programming

Page 1: Everything about Object Oriented Programming

Object Oriented

Programming

• Classes and Objects

• Encapsulation

• Inheritance

• Polymorphism

Abdul Rahman Sherzad

https://www.facebook.com/Oxus20

[email protected]

Page 2: Everything about Object Oriented Programming

Agenda

» Object Oriented Programming

» Definition and Demo

˃ Class

˃ Object

˃ Encapsulation (Information Hiding)

˃ Inheritance

˃ Polymorphism

˃ The instanceof Operator 2

https://www.facebook.com/Oxus20

Page 3: Everything about Object Oriented Programming

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

Page 4: Everything about Object Oriented Programming

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

Page 5: Everything about Object Oriented Programming

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;

}

https://www.facebook.com/Oxus20

5

Page 6: Everything about Object Oriented Programming

BankAccount Class Example (contd.) // methods to read the attributes

public String getAccountName() {

return accountName;

}

public String getAccountNumber() {

return accountNumber;

}

public double getBalance() {

return balance;

}

https://www.facebook.com/Oxus20

6

Page 7: Everything about Object Oriented Programming

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;

}

}

}

https://www.facebook.com/Oxus20

7

Page 8: Everything about Object Oriented Programming

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

Page 9: Everything about Object Oriented Programming

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.

https://www.facebook.com/Oxus20

9

Page 10: Everything about Object Oriented Programming

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

}

}

https://www.facebook.com/Oxus20

10

Page 11: Everything about Object Oriented Programming

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

Page 12: Everything about Object Oriented Programming

Encapsulation Benefit Demo

public class EncapsulationDemo {

public static void main(String[] args) {

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

absherzad.deposit(500);

// 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

}

}

https://www.facebook.com/Oxus20

12

Page 13: Everything about Object Oriented Programming

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

https://www.facebook.com/Oxus20

13

Page 14: Everything about Object Oriented Programming

BankAccount Inheritance Example

» Savings Account is a

bank account with

interest

» Checking Account is a

bank account with

transaction fees

https://www.facebook.com/Oxus20

14

Page 15: Everything about Object Oriented Programming

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

Page 16: Everything about Object Oriented Programming

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

Page 17: Everything about Object Oriented Programming

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

Page 18: Everything about Object Oriented Programming

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

Page 19: Everything about Object Oriented Programming

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

Page 20: Everything about Object Oriented Programming

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

Page 21: Everything about Object Oriented Programming

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;

}

}

}

} 21

https://www.facebook.com/Oxus20

Page 22: Everything about Object Oriented Programming

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

Page 23: Everything about Object Oriented Programming

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

Page 24: Everything about Object Oriented Programming

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

Page 25: Everything about Object Oriented Programming

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

Page 26: Everything about Object Oriented Programming

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

Page 27: Everything about Object Oriented Programming

END

https://www.facebook.com/Oxus20

27