Upload
gwen-copeland
View
215
Download
0
Embed Size (px)
Citation preview
Inheritance continuedInheritance continued
Why is inheritance so importantWhy is inheritance so importantRecapRecap
It allows code to be reused/altered without It allows code to be reused/altered without affecting the original codeaffecting the original code
Problem solutions can be divided into easy Problem solutions can be divided into easy to handle sections which are straightforward to handle sections which are straightforward to assemble (a Lego analogy is often used)to assemble (a Lego analogy is often used)
Added layers of complexity and functionality Added layers of complexity and functionality can be assembled while compartmentalising can be assembled while compartmentalising the sections.the sections.
Visibility ModifiersVisibility Modifiers
What does public mean?What does public mean?
What does private mean?What does private mean?
ProtectedProtected
Lets ReviewLets Review
Basic EmployeeBasic Employeeclass 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
TellerTellerclass 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
ManagerManagerclass 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
SuperSuper
You want to be able to assign values when You want to be able to assign values when an object is created.an object is created.
Constructors are not supported by Constructors are not supported by inheritanceinheritance
But how are we going to do this?But how are we going to do this? We call the constructor of the superclass by We call the constructor of the superclass by
using the keyword using the keyword supersuper
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;}
}
Notes about InheritanceNotes about Inheritance
In inheritance the child (subclass) chooses In inheritance the child (subclass) chooses its parent (superclass)its parent (superclass)
Remember - only public or “protected” Remember - only public or “protected” methods and variables are inheritedmethods and variables are inherited
Should still use getter and setter methods Should still use getter and setter methods for values of variables.for values of variables.
SignatureSignature
The The signaturesignature of a method is determined by of a method is determined by the method name and the number and data the method name and the number and data types of arguments.types of arguments.
The following 3 methods have different The following 3 methods have different signaturessignatures– public void myMethod (int one, int two)public void myMethod (int one, int two)– public void myMethod (int one, float two)public void myMethod (int one, float two)– public void myMethod (int one, float two, int public void myMethod (int one, float two, int
three)three)
SignatureSignature
The signature of a method does not include its The signature of a method does not include its modifier (public, private, protected etc) and return modifier (public, private, protected etc) and return type.type.
The following declarations will result in errors The following declarations will result in errors because the methods with the same name do not because the methods with the same name do not have different signatures.have different signatures.– public void methodOne (int x)public void methodOne (int x)– private void methodOne (int x)private void methodOne (int x)
– public int methodTwo (float y)public int methodTwo (float y)– public float methodTwo (float y)public float methodTwo (float y)