Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

Inheritance, Overloading and Overriding: Recall

The document discusses inheritance, overriding, and overloading in object-oriented programming. It uses a bank account example to demonstrate these concepts. Abstract classes are used to model generalized account types, with subclasses Checking and Savings that implement required abstract methods differently based on their specific behaviors and properties. Constructors can be overloaded to distinguish between different configurations of arguments.

Uploaded by

Mr izki M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Inheritance, Overloading and Overriding: Recall

The document discusses inheritance, overriding, and overloading in object-oriented programming. It uses a bank account example to demonstrate these concepts. Abstract classes are used to model generalized account types, with subclasses Checking and Savings that implement required abstract methods differently based on their specific behaviors and properties. Constructors can be overloaded to distinguish between different configurations of arguments.

Uploaded by

Mr izki M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Inheritance, overloading and overriding

● Recall
– with inheritance the behavior and data associated with
the child classes are always an extension of the behavior
and data associated with the parent class
● In a child class you can
– redefine a method's implementation (override)
● a method that is inherited by the parent, and the child
class wants to change its behavior
– define new methods with the same method name but different
arguments (overload)
● different tasks are performed by each method but they
share the same method name

1
The Bank Account example
● Accounts must have
– current balance
– name of account holder
– a withdraw method
– a deposit method
● Current accounts
– have a maximum withdraw amount
● you cannot withdraw more than $200 in one transaction
● Savings accounts
– have a minimum balance that they need to maintain
at all times.
2
The Bank Account example
● Shared behavior and data between Savings and
Checking Both types of accounts
– Data have these methods
and they behave the same
● current balance
● name of account holder
– Behavior (method names and implementation)
● accessors for common data
● deposit
– Behavior (method names without behavior)
● withdraw Both types of accounts
● display
have these methods
but each method behaves
differently in each
account type 3
The Bank Account example
● Account is a generalized idea
● What actually exists in the banking model are
savings and checking accounts.
– both are accounts with specialized operations on
them. you can refer to them as accounts but you are
using them according what a savings and/or a
checking account can do.
● Generalized ideas (i.e. Account) can be directly
mapped in java as
– abstract classes
– interfaces.

4
The Bank Account with abstract classes
Account abstract class
double balance
String name
+getBalance():double
+getName():String
+setName(String):void
+setBalance(double):void
+deposit(double):boolean
+withdraw(double):boolean abstract methods
+display()
● Abstract classes cannot be instantiated
– there is no constructor !
● Abstract methods must be implemented by
subclasses of Account
– there is no body for withdraw and display inside
Account
5
The Bank Account with abstract classes

Account
double balance
String name
+getBalance():double
+getName():String
+setName(String):void
+setBalance(double):void
+deposit(double):boolean
+withdraw(double):boolean
+display()

Checking Savings
double minimumBalance
+getMinBal():double
+setMinBal(double):void

6
The Bank Account with abstract classes
abstract public class Account {
double balance;
String name;

public double getBalance(){


Account return balance;
}
double balance
String name public void setBalance(double val){
+getBalance():double balance = val;
+getName():String }
+setName(String):void public String getName(){
+setBalance(double):void return name;
+deposit(double):boolean }
+withdraw(double):boolean
+display() public void setName(String aName){
name = aName;
}

public boolean deposit(double amount){


balance = balance + amount;
return true;
}

abstract public boolean withdraw(double amount);


abstract public void display();
}
7
The Bank Account with abstract classes
public class Checking extends Account{
Account Checking(String name, double amount){
this.name = name;
if (amount > 0){
double balance this.balance = amount;
String name } else {
+getBalance():double // error reporting code omitted
this.balance = 0;
+getName():String }
+setName(String):void }
+setBalance(double):void public boolean withdraw(double amount){
+deposit(double):boolean if (amount > 0 && amount <= balance) {
balance = balance - amount;
+withdraw(double):boolean return true;
+display() } else if ( amount > balance){
// error reporting code omitted
return false;
} else {
// error reporting code omitted
return false;
Checking }
}
public void display(){
System.out.println(" ****** Current Account Details
****** ");
System.out.println(" Name: "+ this.getName());
System.out.println(" Current Balance: "+
this.getBalance());
System.out.println("\t\t\t\t ****** Current Account
Details ****** ");
}
} 8
The Bank Account with abstract classes
public class Savings extends Account{
Account double minimumBalance;
Savings(String name, double amount, double minBalance){
this.name = name;
double balance if (amount > 0){
String name this.balance = amount;
+getBalance():double } else {
// error reporting code omitted
+getName():String this.balance = 0;
+setName(String):void }
+setBalance(double):void if (minBalance > 0){
+deposit(double):boolean this.minimumBalance = minBalance;
} else {
+withdraw(double):boolean // error reporting code omitted
+display() this.minimumBalance = 0;
}
}
public void setMinBal(double newBal){
minimumBalance = newBal;
}
Savings
public double getMinBal(){
double minimumBalance return minimumBalance;
}
+getMinBal():double
+setMinBal(double):void public boolean withdraw(double amount){
//code omitted
}
public void display(){
//code omitted
}
} 9
Overloading the constructor
public class Savings extends Account{
double minimumBalance;

Savings(String name, double amount, double minBalance){


this.name = name;
if (amount > 0){
this.balance = amount;
} else {
Distinguish the // error reporting code omitted
this.balance = 0;
constructor by number }
of arguments and types if (minBalance > 0){
this.minimumBalance = minBalance;
for each argument. } else {
// error reporting code omitted
this.minimumBalance = 0;
}
}

Savings(String name, double amount){


this.name = name;
if (amount > 0){
this.balance = amount;
} else {
this.balance = 0;
}
this.minimumBalance = 0;
}

10
Overloading the constructor
public class Savings extends Account{
double minimumBalance;

Savings(String name, double amount,


double minBalance){
this.name = name;
if (amount > 0){
this.balance = amount;
} else {
// error reporting code omitted
public class Main{ this.balance = 0;
}
public static void main(String[] args){ if (minBalance > 0){
Savings mySavings = this.minimumBalance = minBalance;
new Savings(“John”,100.00,50.00); } else {
Savings anotherSavings = // error reporting code omitted
new Savings(“Mary”,200.00); this.minimumBalance = 0;
} }
} }

Savings(String name, double amount){


this.name = name;
if (amount > 0){
this.balance = amount;
} else {
this.balance = 0;
}
this.minimumBalance = 0;
}

}
11
Overloading a method
public class Savings extends Account{
double minimumBalance;

public void display(){


System.out.println("\n ******
Savings Account Details ****** ");
System.out.println(" Name: "+
this.getName());
System.out.println(" Current Balance: "+
this.getBalance());
public class Main{ System.out.println(" Minimum Balance: "+
this.getMinBal());
public static void main(String[] args){ System.out.println("\t\t\t\t ******
Savings mySavings = Savings Account Details ******\n ");
new Savings(“John”,100.00,50.00); }
Savings anotherSavings =
new Savings(“Mary”,200.00); public void display(String date){
System.out.println(date+”your balance
mySavings.display(); is “+this.getBalance());
mySavings.display(“Today”); }
}
} }

12
Bank account with interfaces only!
● Interfaces in java define sets of operations that the
type must implement. <<IAccount>>
+getBalance():double
Checking +getName():String
double balance +setName(String):void
String name +setBalance(double):void
+deposit(double):boolean
+withdraw(double):boolean
+display()

Savings
double minimumBalance
double balance
String name
+getMinBal():double
+setMinBal(double):void

13
Bank account with interfaces only!

<<IAccount>> interface IAccount {

+getBalance():double public double getBalance();


+getName():String public String getName();
public void setName(String aName);
+setName(String):void
public void setBalance(double amount);
+setBalance(double):void public boolean deposit(double amount);
+deposit(double):boolean public boolean withdraw(double amount);
+withdraw(double):boolean public void display();
+display() }

● There is no implementation inside interfaces !

14
Bank account with interfaces only!
public class Savings implements IAccount{
double minimumBalance;
double balance;
String name;

Savings
double minimumBalance // Same as Slide 7
double balance public double getBalance(){ }
String name public void setBalance(double val){ }
public String getName(){ }
+getMinBal():double public void setName(String aName){ }
+setMinBal(double):void public boolean deposit(double amount){ }

// Same as Slide 9
Savings(String name, double amount){ }
Savings(String name, double amount,
double minBalance){ }
public void setMinBal(double newBal){ }
public double getMinBal(){ }
public boolean withdraw(double amount){ }
public void display(){ }

15
Bank account with interfaces only!
public class Checking implements IAccount{
double minimumBalance;
double balance;
String name;

Checking
double balance // Same as Slide 7
String name public double getBalance(){ }
public void setBalance(double val){ }
public String getName(){ }
public void setName(String aName){ }
public boolean deposit(double amount){ }

// Same as Slide 8
public boolean withdraw(double amount){ }
public void display(){ }

16
Interfaces or Abstract classes
● Java allows you to implement as many interfaces as
you like
– you can only extend one abstract class not more !
● Abstract classes can also contain state (instance
variables) and implemented methods
– interfaces cannot have instance variables (they can have
static variables) and cannot have implementations for
methods

17
Interfaces or Abstract classes (cont)
● Both define a type
– with abstract classes you can also share implementation
and enforce method signatures to be implemented later
– with interfaces you can only enforce method signatures
that need to be implemented later
– A class can implement multiple interfaces but extend
only one class (concrete or abstract)

18
Types Revisited
● In Java each interface defines a type. Interface
extension and implementation as subtype
relationships
● A subtype relation in Java is:
– if class C1 extends class C2 then C1 is a subtype of C2

– if interface I1 extends I then I1 is a subtype of I


– if class C implements interface I then C is a subtype of I
– for every interface I, I is a subtype of Object
– for every type T , T[ ] is a subtype of Object
– if T1 is a subtype of T2 then T1[ ] is a subtype of T2[ ]

19
Upcasting
● Operation that changes the runtime type of an
instance to one of its supertypes (i.e. move up the
hierarchy)
force an instance that is of type Savings Account to be

viewed as of type Account.
public class Main{ Force mySavings
public static void main(String[] args){
to be used as of type
Savings mySavings = new Savings(“John”,100.00,50.00); Account
Savings anotherSavings = new Savings(“Mary”,200.00);
Checking cAccount = new Checking(“Michael”, 89.00);
mySavings.display();
mySavings.display(“Today”);

Account oneAC = (Account) mySavings;


Account secondAC= (Account) cAccount;
Account[] allAccounts = new Account[10];
allAccounts[0] = oneAC;
allAccounts[1] = secondAC;
}
}

20
Downcasting
● Operation that changes the runtime type of an
instance to one of its supertypes (i.e. move down
the hierarchy)
–force an instance that is of type Account to be viewed as
of type Savings Account. Force the first element
public class Main{ of the array that is of
public static void main(String[] args){
type Account to be
Savings mySavings = new Savings(“John”,100.00,50.00); used as of type Savings
Savings anotherSavings = new Savings(“Mary”,200.00);
Checking cAccount = new Checking(“Michael”, 89.00);
mySavings.display();
mySavings.display(“Today”); The second element in the list
Account oneAC = (Account) mySavings; is an instance of Checking
Account, stored as of type
Account secondAC= (Account) cAccount;
Account[] allAccounts = new Account[10];
allAccounts[0] = oneAC;
allAccounts[1] = secondAC;
Account. Casting it to
Savings is wrong!
Savings saveAcc = (Savings) allAccounts[0];
Savings saveAcc2 = (Savings) allAccounts[1];
}
} 21

You might also like