Class Basics Definitions

advertisement
Slide 1
Class Basics
Definitions
Greg Butler
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________
Slide 2
Purpose
The purpose of this presentation is to
introduce the student to the concept of
classes and related object oriented concepts.
Slide 3
Objectives
The student will, when presented with a
programming problem, short answer question, or
multiple choice question be able to define and
describe the value of the following concepts:
–
–
–
–
–
–
Class
Method
Attribute (class and object variables)
Inheritance
Abstraction
Message
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
Slide 4
What’s a class? What’s an
object?
•
A class is a “template” for a specific type
of objects.
Class Name: Bank
– Generally classes generally represent real
Account
world objects e.g. (bank account), but also
may represent transactions (deposit)
Attributes:
– An object is an instance of a class– a
Owner
member of a class that has a unique
Account Number
identity (e.g. my bank account, the deposit
Balance
I made yesterday)
Date Established
– Classes have attributes (variable values)
that distinguish them from other classes
Methods:
and their instances from each other.
MakeDeposit ( double amt)
– Classes have methods, which allow them
MakeWithdrawal (double amt)
to perform tasks for themselves
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________
Slide 5
Can you really inherit class?
No, but you can inherit attributes that may or may not be classy!
Inheritance: The creation of a new
class from an existing superclass
•The new class is called a subclass or
child
•It “inherits” the attributes and
methods from the superclass and can
change or add to them
•CheckingAccount and
SavingsAccount are subclasses of
BankAccount
•They inherit all of the attributes
and methods from BankAccount
(previous slide)
•They can add their own (next
slide)
Bank Account
Checking Account
?
?
Savings Account
?
?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________
Slide 6
Inheritance and Subclasses
In our hypothetical bank, all accounts have an Owner, Account
Number, Balance, and a Date Established.
• Savings Accounts: The interest rate paid varies based on the current
balance. Statements will show all common attributes and the interest
rate.
• Checking Accounts: A penalty is assessed once the balance drops
below a minimum value. Statements will show all common
attributes and the penalty assessed. Bank Account
Checking Account
minimumBal
SetMinBalance(double minBal)
AssessPenalty(double minBal, double balance)
printStatement()
Savings Account
intRate
SetIntRate(double balance)
printStatement()
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________
Slide 7
Object Communications—
Messages from Beyond…
• Objects and classes communicate using messages
– Messages are of the form
object_being_addressed. method(args)
– The message signature must be correct for the method being
addressed:
MyDismalCheckingAccount.SetMinBalance(5000.00)
Here we see the message
to set the balance for the
MyDismalCheckingAccount
object to $5000.00
MyDismalCheckingAccount
minimumBal
SetMinBalance(double minBal)
AssessPenalty(double minBal, double balance)
printStatement()
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________
Slide 8
Polymorphism isn’t a science fiction
creature
Polymorphism is the ability for different objects to respond
differently to the same message.
Namely, each object responds appropriately
printStatement()
SavingsAccount
When a SavingsAccount object is
sent a message invoking the
PrintStatement() method, it prints
out the Owner, Account Number,
Balance AND the applicable
interest rate
CheckingAccount
When a CheckingAccount object is
sent a message invoking the
PrintStatement() method, it prints
out the Owner, Account Number,
Balance AND the applicable
penalty
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________
Slide 9
Abstraction, Encapsulation,and Coupling
NASA Strikes Again????
The general idea here is that to use an object you
don’t need to know how it does stuff or all of its
internal variables.
– Easier to use. Send it a message that fits the required
signature. Why do I care how the SavingsAccount
object prints a statement or an Integer object converts a
string?
– Easier to maintain. If Sun rewrites the conversion
routine, it doesn’t affect how I use the conversion
routine. Its still Integer.parseInt(arg) to me.
Much more to come…
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________
Download