z Class – a template of a data object z Interface – a specification
z Instance – an instantiation of a Class or Interface physically represented in memory z Method – a set sequence of instructions
z Instance Field – variable associated with a particular instance.
z Static Field – variable shared among all instances of a Class
z There are two types in Java z Primitive types z Reference types
z Most of your time is spent using Reference types.
z Also known as Objects
z To create an instance of a reference type, use the new keyword in Java
z The new keyword:
1. Makes space for the new object in memory
2. Calls the constructor you specify
3. Returns a reference to the new object
BankAccount account = new BankAccount();
Class
Instance Constructor
Variable
Name
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.
z The template for a class definition follows: [access] [abstract/final] class className [extends superClassName] [implements interfaceNames…] { //constructors //member functions //member variables } |
public class BankAccount {
…
}
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
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:
public class BankAccount { public BankAccount () {
…
}
} Notice that the name of
the constructor is the same as the class
Example:
public class BankAccount {
public BankAccount () {
…
}
} These are different
} constructors because they
take in different arguments
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
}
public class BankAccount { public void withdraw (int amount) {
…
}
public int getAmount () {
…
}
}
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
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];
public class BankAccount {
public int balance; public Date lastWithdrawal;
public List transactions;
}
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
z Allows classes to inherit functionality from
other classes
z Allows data and procedural abstraction z Decreases complexity of large software
systems
z Two separate ideas with different behaviors, but there exists overlap of functionality
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
}
public interface BankAccount { public void withdraw (int amount); public void deposit (int amount);
public int getBalance ();
}
body is not defined.
How do we use the Interface?
z We make classes or other interface
implement or extend the interface.
z If a class implements an interface, that class
must provide an implementation (a method body) for every method specified by the interface
z If a class implements multiple interfaces, it must implement all methods of every interface it chooses to implement
public class CheckingAccount implements BankAccount { private int balance;
public CheckingAccount (int initial) { balance = initial;
}
//implemented methods from BankAccount public void withdraw (int amount) { balance = balance – amount;
}
public void deposit (int amount) { balance = balance + amount;
}
public int getBalance () { return balance;
}
}
z Abstract classes are a mix between
interfaces and classes z can have defined method bodies z can have fields
z Helps to capture the idea of state as well as functionality
Уважаемый посетитель!
Чтобы распечатать файл, скачайте его (в формате Word).
Ссылка на скачивание - внизу страницы.