![Page 1: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/1.jpg)
Classes &Interfaces
Java’s Object Oriented System
Justin Mazzola Paluska
![Page 2: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/2.jpg)
KeywordsClass – a template of a data objectInterface – a specificationInstance – an instantiation of a Class or Interface physically represented in memory
Method – a set sequence of instructions
Instance Field – variable associated with a particular instance.Static Field – variable shared among all instances of a Class
class mem
bers
![Page 3: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/3.jpg)
Data Types
There are two types in JavaPrimitive typesReference types
Most of your time is spent using Reference types.
![Page 4: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/4.jpg)
Reference Types
Also known as ObjectsTo create an instance of a reference type, use the new keyword in JavaThe new keyword:1. Makes space for the new object in memory2. Calls the constructor you specify3. Returns a reference to the new object
![Page 5: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/5.jpg)
Example Instantiation of a Class
BankAccount account = new BankAccount();
Class Name Instance
Variable Name
Class Constructor
![Page 6: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/6.jpg)
Use of instancesCall methods off of instances:
account.withdraw(amount);account.deposit(amount);
Access its instance variables:account.idaccount.balance
When we're done with an object, we just stop using it.
Java will garbage collect the object when there are no more references to it.
![Page 7: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/7.jpg)
Defining a Class
The template for a class definition follows:[access] [abstract/final] class className
[extends superClassName][implements interfaceNames…] {//constructors//member functions//member variables
}
![Page 8: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/8.jpg)
Simple Example
public class BankAccount {…
}
![Page 9: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/9.jpg)
Class Members
In class definitions we can define the following members:
ConstructorsInstance and static methodsInstance and static fieldsNested classes
![Page 10: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/10.jpg)
Constructors
Must have the same name of the Class that they are inCan have multiple constructors per ClassHandles initialization of your classTemplate:[access] className ([arguments…]) {
//constructor body}
![Page 11: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/11.jpg)
Example:Single Constructorpublic class BankAccount {
public BankAccount () {…
}} Notice that the name of
the constructor is the same as the class
![Page 12: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/12.jpg)
Example:Multiple Constructors
public class BankAccount {public BankAccount () {
…}public BankAccount (int initialAmount) {
…}
}These are different
constructors because they take in different arguments
![Page 13: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/13.jpg)
MethodsMethods perform functionsMethods work on the state of the classLike Scheme, methods can take in multiple arguments, and return up to one valueIf no value is to be returned, use the keyword voidA class can have as many methods as neededTemplate:[access] returnType methodName ([arguments…]) {
//method body}
![Page 14: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/14.jpg)
Example Methods
public class BankAccount {public void withdraw (int amount) {
…}public int getAmount () {
…}
}
![Page 15: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/15.jpg)
Method OverloadingA class can have two functions with the same name in a class as long as their arguments differ.Example:
void foo () {…}void foo (int bar) {…}
Java knows which method to call based on the method signatureExample: myClass.foo(7) //calls 2nd method
![Page 16: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/16.jpg)
Fields
A field is like a variable, it stores stateA field has a associated data type which determines the type of data that this field will holdTemplate:[access] dataType fieldName [= value];
![Page 17: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/17.jpg)
Example Fields
public class BankAccount {public int balance;public Date lastWithdrawal;public List transactions;
}
![Page 18: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/18.jpg)
Bringing It Togetherpublic class BankAccount {
private int balance;public BankAccount () {
balance = 0;}public void withdraw (int amount) {
balance = balance – amount;}public void deposit (int amount) {
balance = balance + amount;}
}
Field
Constructor
Methods
![Page 19: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/19.jpg)
Accessors
Before we saw the placeholder [access].There are 4 types of access keywords to describe which classes have access:
public – any other class in any packageprotected – any subclass has access(default) – only classes within the same packageprivate – only accessible from within a class
Good for keeping data abstraction intact
![Page 20: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/20.jpg)
InheritanceAllows classes to inherit functionality from other classesAllows data and procedural abstractionDecreases complexity of large software systems
![Page 21: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/21.jpg)
Checking and SavingsTwo separate ideas with different behaviors, but there exists overlap of functionality
BankAccount
CheckingAccount SavingsAccount
![Page 22: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/22.jpg)
InterfacesAn interface is a specification of a ClassDeclares methods but does not define themInterfaces do not have constructorsTemplate:[access] interface interfaceName
[extends interfaceNameList…] {//method declarations
}
![Page 23: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/23.jpg)
Example Interfacepublic interface BankAccount {
public void withdraw (int amount);public void deposit (int amount);public int getBalance ();
}
Notice that for method declarations, the method
body is not defined.
![Page 24: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/24.jpg)
How do we use the Interface?We make classes or other interface implement or extend the interface.If a class implements an interface, that class must provide an implementation (a method body) for every method specified by the interface
If a class implements multiple interfaces, it must implement all methods of every interface it chooses to implement
![Page 25: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/25.jpg)
Example Interface Usepublic class CheckingAccount implements BankAccount {
private int balance;public CheckingAccount (int initial) {
balance = initial;}
//implemented methods from BankAccountpublic void withdraw (int amount) {
balance = balance – amount;}public void deposit (int amount) {
balance = balance + amount;}public int getBalance () {
return balance;}
}
Since CheckingAccount implements BankAccount, it must provide implementations for these methods
![Page 26: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/26.jpg)
Abstract ClassesAbstract classes are a mix between interfaces and classes
can have defined method bodiescan have fields
Helps to capture the idea of state as well as functionalityTemplate:See Class template (use keyword abstract)
![Page 27: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/27.jpg)
Advantage of Abstract ClassesFor our BankAccount example we can choose to provide implementations for methods we know is common, and declarations for methods that might differLet’s build an abstract class for BankAccount
![Page 28: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/28.jpg)
Example:Abstract Classpublic abstract class BankAccount {
protected int balance;public int getBalance () {
return balance;}public void deposit (int amount) {
balance = balance + amount;}public void withdraw (int amount);
}
![Page 29: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/29.jpg)
Example:Class Extensionpublic class CheckingAccount extends BankAccount {
public CheckingAccount () {balance = 0;
}public void withdraw (int amount) {
balance = balance – amount;}
}
![Page 30: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/30.jpg)
Example:Class Extensionpublic class SavingsAccount extends BankAccount {
private int numberOfWithdrawals;public SavingsAccount () {
balance = 0;numberOfWithdrawals = 0;
}public void withdraw (int amount) {
if (numberOfWithdrawals > 5) {throw new RuntimeException (“Cannot make >5 withdrawals a month”);
} else {balance = balance – amount;numberOfWithdrawals++;
}}public void resetNumOfWithdrawals () {…}
}
![Page 31: Classes & Interfaces - MIT OpenCourseWare · Keywords zClass – a template of a data object zInterface – a specification zInstance – an instantiation of a Class or Interface](https://reader034.vdocument.in/reader034/viewer/2022050303/5f6c49eda874e06b85167e34/html5/thumbnails/31.jpg)
Break