Object Relationship (Part 2)
Author: Mohammad J Iqbal
#Generalization (Inheritance): Represents “is a” relationship between classes
public abstract class Account{
String accountID;
double balance;
public Account(String accountID)
{
this.accountID = accountID;
}
protected abstract double getBalance();
}
public class SavingsAccount extends Account{
double interestRate;
public SavingsAccount(String accountId){
super(accountId);
}
public double getBalance(){
return balance;
}
}
#Realization (Interface Implementation): Concrete classes implements interfaces
public interface Exercise{
public enum Intensity{
LOW, MEDIUM, HIGH
}
public int calorieBurn(int mins);
}
public class Swimming implements Exercise{
Intensity intensity = Intensity.MEDIUM;
int calorieBurn;
public int calorieBurn(int mins){
//calculate based on intensity and minutes
return calorieBurn;
}
}
#Dependency Relationship: Represents loosely coupled correction between classes.
#Usage (Dependency) Relationship: One class depends on another class to perform certain task or access certain functionality.