[SOLVED] CS代写 SENG201

30 $

File Name: CS代写_SENG201.zip
File Size: 150.72 KB

SKU: 7079375867 Category: Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Or Upload Your Assignment Here:


Software Engineering I SENG201

Lecture 11 – Inheritance (part 1) March 15, 2022

Copyright By PowCoder代写加微信 assignmentchef

Previous lecture
1. Introduction to interface classes in Java
2. Interfaces in UML
3. Interesting features of interfaces

1. Introduction to inheritance
2. Overriding methods
3. Subclass constructors

1. Introduction to inheritance
2. Overriding methods
3. Subclass constructors

• Categorize “concepts” (i.e., classes) by hierarchical relationships – Concepts in hierarchy share properties and behavior
Superclass (parent) Vehicle
Specialization (generalization)
Medium: land, sea, air Subclass (child)
LandVehicle
SeaVehicle
AirVehicle
Power: engine, human

BankAccount
accountNumber
withdraw()
getBalance()

BankAccount
accountNumber
withdraw()
getBalance()
Common behaviour of related classes
CheckingAccount
[balance] [accountNumber] transactionCount
[getBalance()]
deductFees()
withdraw()
SavingsAccount
[balance] [accountNumber] interestRate
[deposit()] [withdraw()] [getBalance()] addInterest()
From superclass
• Available in subclass1
• Not (re-)declared in subclass
From superclass
• Redefined (overridden) in subclass2
1 Not included in UML class diagram of subclass 2 Included in UML class diagram of subclass

Complete class diagram in UML
BankAccount
-balance:double
-accountNumber:int
+deposit(amount:double):void +withdraw(amount:double):void +getBalance():double
CheckingAccount
-transactionCount:int
+deductFees():void
+deposit(amount:double):void
+withdraw(amount:double):void
SavingsAccount
-interestRate:double
+addInterest():void

BankAccount
-balance:double
-accountNumber:int
+deposit(amount:double):void +withdraw(amount:double):void +getBalance():double
Example in Java
public class BankAccount {
private double balance; private int accountNumber;
public BankAccount() { balance = 0; }
public void deposit(double amount) { balance = balance + amount; } public void withdraw(double amount) { balance = balance – amount; } public double getBalance() { return balance; }
SavingsAccount
-interestRate:double
public class SavingsAccount extends BankAccount {
// added instance variables
private double interestRate;
// new constructor
public SavingsAccount(double rate) {
interestRate = rate;
// new methods
public void addInterest() {
// can use getBalance(), deposit(), etc. as if they were implemented in SavingsAccount
double interest = getBalance() * interestRate / 100; deposit(interest);
+addInterest():void

Private properties and methods of parent are not accessible
Use non-private parent method to set / get private data (e.g., getBalance(), deposit())
BankAccount
-accountNumber
+deposit(…)
+withdraw(…)
+getBalance()
SavingsAccount
-interestRate
+addInterest()

1. Introduction to inheritance
2. Overriding methods
3. Subclass constructors

Overriding
BankAccount
-balance: double
-accountNumber: String
+deposit(amount: double): void +withdraw(amount: double): void +getBalance(): double
CheckingAccount
-transactionCount: int
+deductFees(): void
+deposit(amount: double): void
+withdraw(amount: double): void
Sub-class can customize behavior by overriding methods from parent (ancestor)
Write method with same signature (name, parameters) and return type
Cannot override to change return type or visibility

BankAccount
-balance: double
-accountNumber: String
public class BankAccount {
public void deposit(double amount) {
+deposit(amount: double): void +withdraw(amount: double): void +getBalance(): double
balance = balance + amount;
CheckingAccount
-transactionCount: int
public class CheckingAccount extends BankAccount {
public void deposit(double amount) {
transactionCount++; //deposit(amount); super.deposit(amount);
// WRONG! Same as this.deposit(amount) // CORRECT
+deductFees(): void
+deposit(amount: double): void
+withdraw(amount: double): void
Keyword super: explicitly access property / method of superclass of this class
Keyword this: explicitly access property / method of this subclass

BankAccount
-balance: double
-accountNumber: String
public class BankAccount {
public void deposit(double amount) {
+deposit(amount: double): void +withdraw(amount: double): void +getBalance(): double
balance = balance + amount;
CheckingAccount
-transactionCount: int
public class CheckingAccount extends BankAccount {
public void deposit(double amount) {
transactionCount++; //deposit(amount); super.deposit(amount);
// WRONG! Same as this.deposit(amount) // CORRECT
+deductFees(): void
+deposit(amount: double): void
+withdraw(amount: double): void
Keyword super: explicitly access property / method of superclass of this class
Keyword this: explicitly access property / method of this subclass

-name: String
+getBalance(): double
BankAccount
CheckingAccount
+getBalance(): double
Beware of accidental overriding: writing a method provided by grandparent class

Overriding
• Javadoc shows inheritance hierarchy – Helps us identify existing methods
• Use annotation @Override
– Signal that we think we are overriding a parent / ancestor method
• Can prevent overriding by making class or method final

public class BankAccount {
public void deposit(){…}
public final void withdraw(){…} }
public class CheckingAccount extends BankAccount {
public void deposit (double amount) {
// do something
// compiler complains
public void depositSmallAmount (double amount) {
// do something }
public void withdraw(){…} // compiler complains }
• Compiler errors
– The method depositSmallAmount(double) of type CheckingAccount must override or implement a supertype method
– Cannot override the final method from BankAccount

1. Introduction to inheritance
2. Overriding methods
3. Subclass constructors

public class BankAccount {
… BankAccount() {
balance = 0;
public class SavingsAccount extends BankAccount {
// added instance variables
private double interestRate;
// new constructor
public SavingsAccount(double rate) {
super(); // calls constructor of BankAccount explicitly // not needed for default constructor
interestRate = rate;
• super followed by arguments
– Must be first statement in subclass constructor
• Superclass default constructor always called with subclass

Another example
public class SavingsAccount extends BankAccount {
// added instance variables
private double interestRate;
// new constructor
public SavingsAccount(double initialAmount, double rate) {
super(initialAmount); // calls constructor of BankAccount to set balance interestRate = rate;
public class BankAccount { …
BankAccount() { balance = 0; }
BankAccount(double amount) // to make super (initialAmount) in SavingsAccount work {
balance = amount;
public BankAccount(double amount, int number) {
balance = amount;
accountNumber = number;

1. Introduction to inheritance
2. Overriding methods
3. Subclass constructors

Cartoon of the day
Key lesson: Inheritance allows us to reuse and refine behaviour from other classes. This increases maintainability and reusability.

程序代写 CS代考加微信: assignmentchef QQ: 1823890830 Email: [email protected]

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] CS代写 SENG201
30 $