Object-oriented basics Object Class vs object Inheritance Overloading Interface 1
The object concept Object Encapsulation abstraction Entity with state and behaviour state -> variables behaviour -> methods ( functions in C++) Provides Modularity Information hiding Both very important for large-scale programming 2
Example :BankAccount public class BankAccount { private int amount = 0; BankAccount(){ } BankAccount - amount: int = 0 - chan: Channel ~ BankAccount() ~ BankAccount(init :int) ~ BankAccount(chan :Channel) + transfer(payment :Payment) : void + deposit(value :int) : void + withdraw(value :int) : void + getbalance() : int } BankAccount(int init){ amount = init; } public synchronized void deposit(int value){ amount += value; } public synchronized void withdraw(int value){ amount -= value; } public int getbalance(){ return amount; } 3
Class The class A type or specification used to design objects An object is a specific instance of a class Example BankAccount specifies the account type BankAccount a, b, c, d; Defines particular instances when created 4
Class declaration 5
Object life cycle Create AClass myobj = new AClass(); new operator and constructor Constructor initialises the object Use Manipulate state and call methods myobj.method(); By default, all variables are private Removal Automatic garbage collector Ref:myObj o (AClass) 6
Visibility Classes can control what they show to the outside world Public - open to all Protected - open to derived (child) classes Private - only for current class Package - open for other classes in package 7
References Java uses references BankAccount a, b; a = new BankAccount(1000); b = a; a.withdraw(100); Make sure you understand this 8
Class relationships Programs consists of (many) objects They must have some connection in order to work Three main relations has aggregation is inheritance knows association 9
Has relation Whole-part relationship One way relationship - account: BankAccount AccountHolder Thread -account BankAccount - amount: int = 0 - chan: Channel + AccountHolder(name :String) + AccountHolder(account :BankAccount, name :String) + run() : void ~ BankAccount() ~ BankAccount(init :int) ~ BankAccount(chan :Channel) + transfer(payment :Payment) : void + deposit(value :int) : void + withdraw(value :int) : void + getbalance() : int 10
Is relation Inheritance General-specific relation Common state and behaviour can be stored in super (parent) class More specific types created with subclasses (child or derived class Overloading Subclass method can have new implementation BankAccount - amount: int = 0 - chan: Channel ~ BankAccount() ~ BankAccount(init :int) ~ BankAccount(chan :Channel) + transfer(payment :Payment) : void + deposit(value :int) : void + withdraw(value :int) : void + getbalance() : int Sav ingsaccount - interest: double + getinterest() : double 11
Use of inheritance Structuring Incremental development BankAccount b = new BankAccount(10); SavingsAccount s = new SavingsAccount(100); BankAccount bb; SavingsAccount ss; bb = s; // valid, s IS a BankAccount // but no getinterest() ss = b; // invalid, b is NOT a SavingsAccount 12
Knows relation Equal relation Either one- or two-way Implemented as a reference Passed in constructor or method call BankAccount - amount: int = 0 - chan: Channel ~ BankAccount() ~ BankAccount(init :int) ~ BankAccount(chan :Channel) + transfer(payment :Payment) : void + deposit(value :int) : void + withdraw(value :int) : void + getbalance() : int -chan Channel - buffer: Object - empty: boolean ~ Channel() + send(data :Object) : void + receive() : Object 13
Package Concept to group related classes Helps in Finding classes Avoiding name conflicts Controlling access (visibility) Implementation Directory hierarchy E.g.: a/b/c/bankaccount.java a.b.c.bankaccount.java Use Requires fully qualified name Declare you package (package a.b.c;) Import classes from other packages (import q.b.*;) 14
Interface Simple but perhaps most difficult concept to understand An interface is a contract Named collection of methods an object must implement Specification of behaviour NO implementation Large-scale programming Separates what from how Applications can change implementation without affecting other parts BUT! Interfaces cannot change! 15
How to use Declare as interface Account { } class BankAccount implements Account {..} Program for interfaces, not classes AccountHolder has Account! Account acc = new BankAccount(); BankAccount can be replaced by other classes As long as they implement Account What about SavingsAccount? 16 «interface» Account + transfer() : void + withdraw() : void + getbalance() : void + deposit() : void «realize» BankAccount - amount: int = 0 - chan: Channel ~ BankAccount() ~ BankAccount(init :int) ~ BankAccount(chan :Channel) + transfer(payment :Payment) : void + deposit(value :int) : void + withdraw(value :int) : void + getbalance() : int