Data Types. Reference Types. Example Instantiation of a Class. Use of instances. Single Constructor

Страницы работы

31 страница (Word-файл)

Фрагмент текста работы

Keywords

Class – a template of a data object z Interface – a specification

Instance – an instantiation of a Class or Interface physically represented in memory z Method – a set sequence of instructions

Instance Field – variable associated with a particular instance.

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

z  Most of your time is spent using Reference types.

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

Example Instantiation of a Class

BankAccount account = new BankAccount();

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 () {

}

}                  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

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

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 ();

}

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

Example Interface Use

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;

}

}

Abstract Classes

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

Похожие материалы

Информация о работе