13
Inheritance continued Inheritance continued

Inheritance continued. Why is inheritance so important Recap It allows code to be reused/altered without affecting the original code Problem solutions

Embed Size (px)

Citation preview

Page 1: Inheritance continued. Why is inheritance so important Recap  It allows code to be reused/altered without affecting the original code  Problem solutions

Inheritance continuedInheritance continued

Page 2: Inheritance continued. Why is inheritance so important Recap  It allows code to be reused/altered without affecting the original code  Problem solutions

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.

Page 3: Inheritance continued. Why is inheritance so important Recap  It allows code to be reused/altered without affecting the original code  Problem solutions

Visibility ModifiersVisibility Modifiers

What does public mean?What does public mean?

What does private mean?What does private mean?

Page 4: Inheritance continued. Why is inheritance so important Recap  It allows code to be reused/altered without affecting the original code  Problem solutions

ProtectedProtected

Page 5: Inheritance continued. Why is inheritance so important Recap  It allows code to be reused/altered without affecting the original code  Problem solutions

Lets ReviewLets Review

Page 6: Inheritance continued. Why is inheritance so important Recap  It allows code to be reused/altered without affecting the original code  Problem solutions

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

Page 7: Inheritance continued. Why is inheritance so important Recap  It allows code to be reused/altered without affecting the original code  Problem solutions

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

Page 8: Inheritance continued. Why is inheritance so important Recap  It allows code to be reused/altered without affecting the original code  Problem solutions

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

Page 9: Inheritance continued. Why is inheritance so important Recap  It allows code to be reused/altered without affecting the original code  Problem solutions

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

Page 10: Inheritance continued. Why is inheritance so important Recap  It allows code to be reused/altered without affecting the original code  Problem solutions

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;}

}

Page 11: Inheritance continued. Why is inheritance so important Recap  It allows code to be reused/altered without affecting the original code  Problem solutions

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.

Page 12: Inheritance continued. Why is inheritance so important Recap  It allows code to be reused/altered without affecting the original code  Problem solutions

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)

Page 13: Inheritance continued. Why is inheritance so important Recap  It allows code to be reused/altered without affecting the original code  Problem solutions

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)