Download presentation
Presentation is loading. Please wait.
Published byGwen Copeland Modified over 9 years ago
1
Inheritance continued
2
Why is inheritance so important Recap It allows code to be reused/altered without affecting the original code Problem solutions can be divided into easy to handle sections which are straightforward to assemble (a Lego analogy is often used) Added layers of complexity and functionality can be assembled while compartmentalising the sections.
3
Visibility Modifiers What does public mean? What does private mean?
4
Protected
5
Lets Review
6
Basic Employee class Employee { private String name; private int hourlyWage; public void setupObject(String nameParameter, int hourlyWageParameter) { name = nameParameter; hourlyWage = hourlyWageParameter; } // End setupObject public String getName() { return name; } // End getName public int calculateWages(int hoursWorked) { return (hoursWorked * hourlyWage); } // End calculateWages // Other methods such as changeWages, etc would go here } // End Employee
7
Teller class Teller extends Employee { private int totalLodged=0; private int totalWithdrawn=0; public void lodgeMoney(int amountToLodge) { totalLodged += amountToLodge; } // End lodgeMoney public void withDrawMoney(int amountToWithdraw) { totalWithdrawn += mountToWithdraw; } // End withDrawMoney // Other methods such as getTotalLodged, etc would go here } // End Teller
8
Manager class Manager extends Teller { private int loansApproved=0; public void approveLoan(int loanAmount) { loansApproved += loanAmount; } // End approveLoan // Other methods such as getLoans etc would go here } // End Manager
9
Super You want to be able to assign values when an object is created. Constructors are not supported by inheritance But how are we going to do this? We call the constructor of the superclass by using the keyword super
10
class person { protected string name; public person (string p) { this.name = p; } class student extends person { private int id; public student (string n, int id) { super (n); this.id = id; }
11
Notes about Inheritance In inheritance the child (subclass) chooses its parent (superclass) Remember - only public or “protected” methods and variables are inherited Should still use getter and setter methods for values of variables.
12
Signature The signature of a method is determined by the method name and the number and data types of arguments. The following 3 methods have different signatures –public void myMethod (int one, int two) –public void myMethod (int one, float two) –public void myMethod (int one, float two, int three)
13
Signature The signature of a method does not include its modifier (public, private, protected etc) and return type. The following declarations will result in errors because the methods with the same name do not have different signatures. –public void methodOne (int x) –private void methodOne (int x) –public int methodTwo (float y) –public float methodTwo (float y)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.