Object-oriented Programming
Object-Oriented Programming
Why OOP?
Why OOP?
Object-Oriented Programming A(hopefully)more intuitive way of representing data A common method for organizing programs Formally split"global state"and "local state"for every object
Object-Oriented Programming ● A (hopefully) more intuitive way of representing data ● A common method for organizing programs ● Formally split "global state" and "local state" for every object
Classes Every object is an instance of a class A class is a type or category of objects A class serves as a blueprint for its instances Instance Tiffany:is a Human Class Tiffany has a :name and an:age Attributes Tiffany can:talk:to other Humans Behaviors
Classes • Every object is an instance of a class • A class is a type or category of objects • A class serves as a blueprint for its instances Tiffany is a Human Tiffany has a name and an age Instance Class Attributes Tiffany can talk to other Humans Behaviors
Example:Bank Accounts A class serves as a blueprint for its instances Tiff's Idea:All bank accounts have a balance Account and an account holder; the Account class should define those values for each newly created instance. Withdraw S5 Idea:All bank accounts should have Transfer $5 "withdraw"and "deposit"behaviors that all work in the same way. Alex's Better Idea:All bank accounts share a Account "withdraw"and "deposit"method. Deposit $5 All accounts might also share other characteristics like maximum withdrawal, or loan limits
Example: Bank Accounts Tiff’s Account Alex's Account Deposit $5 Withdraw $5 Transfer $5 A class serves as a blueprint for its instances Idea: All bank accounts have a balance and an account holder; the Account class should define those values for each newly created instance. Idea: All bank accounts should have “withdraw” and “deposit” behaviors that all work in the same way. Better Idea: All bank accounts share a “withdraw” and “deposit” method. All accounts might also share other characteristics like maximum withdrawal, or loan limits