1 / 38

Inheritance

Inheritance. CSC 171 FALL 2004 LECTURE 18. READING. Read Horstmann, Chapter 11. Design Methodology. Problem Definition Requirements Analysis Architecture Construction Testing Future Improvements. Classes and Objects. Object oriented programs Define classes of objects

serena
Download Presentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance CSC 171 FALL 2004 LECTURE 18

  2. READING • Read Horstmann, Chapter 11

  3. Design Methodology • Problem Definition • Requirements Analysis • Architecture • Construction • Testing • Future Improvements

  4. Classes and Objects • Object oriented programs • Define classes of objects • Make specific object out of class definitions • Run by having the objects interact • A class is a type of thing • Instructor • An object is a specific thing • Ted • An object is an instance of a class

  5. Hierarchies • Humans have found that organizing concepts into hierarchies a useful method of organizing information

  6. HIERARCHIES Hierarchies are nested groupings. Examples of hierarchies • Levels of organization in our bodies: • Organism: Organ Systems: Organs: Tissues: Cells: Organelles • Ecology: • Biome: Community: Population: Organism • Political boundaries: • USA: New York State: Monroe County: City of Rochester • Notice how each group is completely subordinate to any group on its left.

  7. Inheritance Hierarchies • Object oriented languages, such as JAVA allows us to group classes into inheritance hierarchies. • The most general classes are near the root • superclasses • The more specific classes are near the leaves • Subclasses • Subclasses inherit attributes from superclasses

  8. Example: Banking Systems • Consider a system that supports Savings and Checking accounts • What are the similarities? • What are the specifics

  9. Accounts • Both savings and Checking accounts support the idea of • Balance • Deposit • Withdraw • Savings accounts pay interest checking accounts do not • Checking accounts have transaction fees, savings accounts do not

  10. Super & Sub classes • More general concepts are in super classes • More specific concepts are in sub classes • Sub classes extend (inherit from) superclasses

  11. Why inheritance? • The power of inheritance is that sub-classes inherit the capabilities of the super-classes they extend • This reduces code redundancy

  12. Example: Banking systems • Bank accounts are a type of object • Savings accounts are a type of bank account • Checking Accounts are bank accounts public class BankAccount { . . . } public class SavingsAccount extends BankAccount { . . .} public class CheckingAccount extends BankAccount { . . .}

  13. BankAccount • Instance variable “balance” • Methods “deposit” and “withdraw”

  14. public class BankAccount { public void deposit (double amount) { balance += amount; } public void withdraw(double amount) { if (amount <= balance) balance -= amount; } private double balance; }

  15. SavingsAccount • A bank account with an interest rate

  16. public class SavingsAccount extends BankAccount { public SavingsAccount(double rate) { interestRate = rate; } public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); } private double interestRate; }

  17. Inheritance and Methods • Override method: Supply a different implementation of a method that exists in the superclass • Inherit method: Don't supply a new implementation of a method that exists in the superclass • Add method: Supply a new method that doesn't exist in the superclass

  18. Inheritance and Fields • Inherit field: All fields from the superclass are automatically inherited • Add field: Supply a new field that doesn't exist in the superclass • Can't override fields

  19. Checking Account • A Bank Account with transaction fees • Need to keep tack of transactions

  20. Checking Account public class CheckingAccount extends BankAccount { private int transactionCount; public CheckingAccount() { transactionCount = 0; } }

  21. public void deposit(double amount) {   transactionCount++;   super.deposit(amount);} public void withdraw(double amount) {   transactionCount++;   super.deposit(amount);}

  22. Excercise • Define a base class “Employee” • Employees have names • Define constructor

  23. public class Employee { private String name; public Employee(String name) { this.name = name; } public String name() { return name;} }

  24. Excercise • Define a sub class “HourlyEmployee” • Names, hourly rates, and hours worked • Define constructor, clockhours and “getPay()”

  25. public class HourlyEmployee extends Employee { private double wage, hoursworked; public HourlyEmployee(String name, double wage) { super(name); this.wage = wage; hoursworked = 0; }

  26. public void clockHours(double hours) { hoursworked += hours; } public double getPay() { double pay = hoursworked * wage; hoursworked = 0 ; return pay; } }

  27. Excercise • Define a sub-class “SalaryEmployee” • Names and annual Salary • Define constructor, and “getPay()” (montly)

  28. public class SalaryEmployee extends Employee { private double annualSalary; public SalaryEmployee(String name, double salary) { super(name); annualSalary = salary; }

  29. public double getPay() { return annualSalary / 12; } }

  30. TERMINOLOGY • A base class is often called a parent class. A sub-class is then called a child class (or derived class) • Parents of parents are called ancestor classes • Children of children are called descendent classes

  31. Overriding vs. Overloading • Overloading refers to having a different configuration of parameters for the same method name • Overriding refers to the redefinition of a method in a subclass

  32. ACCESS ISSUES • private instance variables or methods in a super (base,parent) class are not accessible in sub (derived, child) classes. • So, private methods and variable are effectively not inherited. • They are “there” but you have to use the super-class accessor and mutator methods.

  33. Protected • protected methods and variables can be accessed inside derived classes, or in any class in the same package.

  34. Package Access • If you don not place any of public, private, or protected before an instance variable or method definition, then it will have package access (also know as default access or friendly access). It will be visible to any class in the same package, but not outside.

More Related