DOC PREVIEW
MIT 6 170 - Classes & Interfaces

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Classes &InterfacesKeywordsData TypesReference TypesExample Instantiation of a ClassUse of instancesDefining a ClassSimple ExampleClass MembersConstructorsExample:Single ConstructorExample:Multiple ConstructorsMethodsExample MethodsMethod OverloadingFieldsExample FieldsBringing It TogetherAccessorsInheritanceChecking and SavingsInterfacesExample InterfaceHow do we use the Interface?Example Interface UseAbstract ClassesAdvantage of Abstract ClassesExample:Abstract ClassExample:Class ExtensionExample:Class ExtensionBreakClasses &InterfacesJava’s Object Oriented SystemJustin Mazzola PaluskaKeywordsz Class – a template of a data objectz Interface – a specificationz Instance – an instantiation of a Class or Interface physically represented in memoryz Method – a set sequence of instructionsz Instance Field – variable associated with a particular instance.z Static Field – variable shared among all instances of a Classclass membersData Typesz There are two types in Javaz Primitive typesz Reference typesz Most of your time is spent using Reference types.Reference Typesz Also known as Objectsz To create an instance of a reference type, use the new keyword in Javaz The new keyword:1. Makes space for the new object in memory2. Calls the constructor you specify3. Returns a reference to the new objectExample Instantiation of a ClassBankAccount account = new BankAccount();Class NameInstance Variable NameClass ConstructorUse of instancesz Call methods off of instances:z account.withdraw(amount);z account.deposit(amount);z Access its instance variables:z account.idz account.balancez 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 Classz The template for a class definition follows:[access][abstract/final] class className [extends superClassName][implements interfaceNames…] {//constructors//member functions//member variables}Simple Examplepublic class BankAccount {…}Class Membersz In class definitions we can define the following members:z Constructorsz Instance and static methodsz Instance and static fieldsz Nested classesConstructorsz Must have the same name of the Class that they are inz Can have multiple constructors per Classz Handles initialization of your classz Template:[access] className ([arguments…]) {//constructor body}Example:Single Constructorpublic class BankAccount {public BankAccount () {…}}Notice that the name of the constructor is the same as the classExample:Multiple Constructorspublic class BankAccount {public BankAccount () {…}public BankAccount (int initialAmount) {…}}These are different constructors because they take in different argumentsMethodsz Methods perform functionsz Methods work on the state of the classz Like Scheme, methods can take in multiple arguments, and return up to one valuez If no value is to be returned, use the keyword voidz A class can have as many methods as neededz Template:[access] returnType methodName ([arguments…]) {//method body}Example Methodspublic class BankAccount {public void withdraw (int amount) {…}public int getAmount () {…}}Method Overloadingz 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 signaturez Example: myClass.foo(7) //calls 2ndmethodFieldsz A field is like a variable, it stores statez A field has a associated data type which determines the type of data that this field will holdz Template:[access] dataType fieldName [= value];Example Fieldspublic class BankAccount {public int balance;public Date lastWithdrawal;public List transactions;}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;}}FieldConstructorMethodsAccessorsz 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 packagez protected – any subclass has accessz (default) – only classes within the same packagez private – only accessible from within a classz Good for keeping data abstraction intactInheritancez Allows classes to inherit functionality from other classesz Allows data and procedural abstractionz Decreases complexity of large software systemsChecking and Savingsz Two separate ideas with different behaviors, but there exists overlap of functionalityBankAccountCheckingAccount SavingsAccountInterfacesz An interface is a specification of a Classz Declares methods but does not define themz Interfaces do not have constructorsz Template:[access] interface interfaceName[extends interfaceNameList…] {//method declarations}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.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 interfacez If a class implements multiple interfaces, it must implement all methods of every interface it chooses to implementExample 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 methodsAbstract Classesz Abstract classes are a mix between interfaces and classesz can have defined method bodiesz can have fieldsz Helps to capture the idea of state as well as functionalityz Template:See Class template (use keyword abstract)Advantage of Abstract Classesz For our BankAccount example we can choose to provide implementations for methods we know is common, and declarations for methods that might differz Let’s build an abstract class for BankAccountExample:Abstract Classpublic abstract class BankAccount {protected int balance;public int getBalance () {return balance;}public void deposit (int


View Full Document

MIT 6 170 - Classes & Interfaces

Download Classes & Interfaces
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Classes & Interfaces and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Classes & Interfaces 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?