Classes & Interfaces: Java's Object Oriented System
Classes & Interfaces: Java's Object Oriented System
Interfaces
Java’s Object Oriented System
class members
z Instance Field – variable associated with a
particular instance.
z Static Field – variable shared among all instances
of a Class
Data Types
z There are two types in Java
z Primitive types
z Reference types
Class
Name Class
Instance Constructor
Variable
Name
Use of instances
z Call methods off of instances:
z account.withdraw(amount);
z account.deposit(amount);
z Access its instance variables:
z account.id
z account.balance
z When we're done with an object, we just stop
using it.
z Java will garbage collect the object when there
are no more references to it.
Defining a Class
z The template for a class definition follows:
[access] [abstract/final] class className
[extends superClassName]
[implements interfaceNames…] {
//constructors
//member functions
//member variables
}
Simple Example
public class BankAccount {
…
}
Class Members
z In class definitions we can define the
following members:
z Constructors
z Instance and static methods
z Instance and static fields
z Nested classes
Constructors
z Must have the same name of the Class that
they are in
z Can have multiple constructors per Class
z Handles initialization of your class
z Template:
[access] className ([arguments…]) {
//constructor body
}
Example:
Single Constructor
public class BankAccount {
public BankAccount () {
…
}
} Notice that the name of
the constructor is the
same as the class
Example:
Multiple Constructors
public class BankAccount {
public BankAccount () {
…
}
public BankAccount (int initialAmount) {
…
} These are different
} constructors because they
take in different arguments
Methods
z Methods perform functions
z Methods work on the state of the class
z Like Scheme, methods can take in multiple
arguments, and return up to one value
z If no value is to be returned, use the keyword void
z A class can have as many methods as needed
z Template:
[access] returnType methodName ([arguments…]) {
//method body
}
Example Methods
public class BankAccount {
public void withdraw (int amount) {
…
}
public int getAmount () {
…
}
}
Method Overloading
z A class can have two functions with the same
name in a class as long as their arguments
differ.
z Example:
z void foo () {…}
z void foo (int bar) {…}
z Java knows which method to call based on
the method signature
z Example: myClass.foo(7) //calls 2nd method
Fields
z A field is like a variable, it stores state
z A field has a associated data type which
determines the type of data that this field will
hold
z Template:
[access] dataType fieldName [= value];
Example Fields
public class BankAccount {
public int balance;
public Date lastWithdrawal;
public List transactions;
}
Bringing It Together
public class BankAccount {
private int balance; Field
public BankAccount () {
balance = 0;
Constructor
}
public void withdraw (int amount) {
balance = balance – amount;
}
Methods
public void deposit (int amount) {
balance = balance + amount;
}
}
Accessors
z Before we saw the placeholder [access].
z There are 4 types of access keywords to
describe which classes have access:
z public – any other class in any package
z protected – any subclass has access
z (default) – only classes within the same package
z private – only accessible from within a class
z Good for keeping data abstraction intact
Inheritance
z Allows classes to inherit functionality from
other classes
z Allows data and procedural abstraction
z Decreases complexity of large software
systems
Checking and Savings
z Two separate ideas with different behaviors,
but there exists overlap of functionality
BankAccount
CheckingAccount SavingsAccount
Interfaces
z An interface is a specification of a Class
z Declares methods but does not define them
z Interfaces do not have constructors
z Template:
[access] interface interfaceName
[extends interfaceNameList…] {
//method declarations
}
Example Interface
public interface BankAccount {
public void withdraw (int amount);
public void deposit (int amount);
public int getBalance ();
}
} else {
balance = balance – amount;
numberOfWithdrawals++;
}
}
public void resetNumOfWithdrawals () {…}
}
Break