CSC 143/Chapter 9: Difference between revisions
From charlesreid1
(Created page with "=Chapter 9: Inheritance and Interfaces= Sections: 9.1 Inheritance basics 9.2 Interacting with the superclass 9.3 Polymorphism 9.4 Inheritance and Design 9.5 Interfaces...") |
|||
| Line 14: | Line 14: | ||
9.6 Case study: Financial class hierarchy | 9.6 Case study: Financial class hierarchy | ||
==Section 9.1: Inheritance Basics== | |||
===Definitions=== | |||
Definitions: | |||
* Code reuse | |||
* Is-a relationship | |||
* Inheritance hierarchy | |||
* Inheritance (inherit) | |||
* Superclass | |||
* Subclass | |||
* Override | |||
===Material=== | |||
Large programs on large scale require abstractions - or, another way to look at it, abstractions open up whole new problem spaces | |||
Hierarchy of employees example: | |||
* Legal firm: lawyers, general secretaries, legal secretaries, marketers | |||
* Each person is an employee | |||
* Each X is a Y, &c. | |||
* Types of employees have salaries, skills, etc., some are new methods, some override parent methods | |||
* Is-a relationship between classes | |||
Extending a class: | |||
* Turn non-code example into code | |||
* Employee class, Secretary class | |||
Overriding methods: | |||
* Lawyers, vacation days and vacation forms | |||
* Overloading - re-defining a method from a parent class | |||
* Overriding - one class with multiple versions of the same method for different parameter signatures | |||
==Section 9.2: Interacting with the Superclass== | |||
===Material=== | |||
Calling overridden methods: | |||
* giving employees a raise: explicitly calling super method | |||
* can only call super once - super.super.method() not allowed | |||
Accessing inherited fields | |||
* more complex example than employee class: stocks class | |||
* encapsulation: even child classes can't examine private data | |||
* implement an accessor method getParam() | |||
* Now the child class can define a method that uses a parameter defined in the parent class | |||
Super class constructor | |||
* Methods are inherited | |||
* Constructors are NOT inherited | |||
* can cause trouble when trying to access private data | |||
Example code from book: constructor that sets various fields to initial values | |||
* Does not compile - fields are all private, cannot be accessed/modified by child class | |||
* Let's examine what the compiler is REALLY saying: | |||
* "Wait a minute, what do you think you're doing setting shares and costs? You're a dividend stock - so you take care of dividends. Let the stock class take care of stock stuff, like shares and costs. Mmmkay, thanks." | |||
* The real solution: let the parent class take care of what it takes care of, let the child class take care of what it takes care of | |||
* Call the super constructor | |||
* Finished code for example constructor | |||
DividendStock Behavior | |||
* implement dividend payments method | |||
* new method simply computes profit same as old method, plus dividends | |||
* Extend getProfit(), which calculates the profit using super.getProfit() | |||
Object class | |||
* superclass for all other Java classes | |||
* implements few key behaviors: clone(), equals(obj), finalize(), getClass(), hashCode(), toString(), notify()/notifyAll()/wait() | |||
* can pass as type for a method or constructor parameter, then deal with any input type (although limited in methods can call) | |||
Objects and equality | |||
* Lamp analogy: if I show you two objects, say a pair of lamps, and I ask you, are these two lamps equal? | |||
* You might ask me: in what sense? | |||
** Brightness? Power consumption? | |||
** Manufacturer, components, parts interchangeable? | |||
** Exact replica, atom-by-atom, of the original lamp? | |||
** Cosmic Zen and oneness with the universe: these two lamps are equal only if they are composed of those exact atoms | |||
** What if one of them is a picture? What if both of them are pictures? | |||
The equals method | |||
* shoes analogy - equivalent, but distinct and separate items | |||
* When comparing two objects, we may want to know if they refer to the same location in memory, where data for an object is located | |||
* Or, we may have two objects referring to different locations in memory, but containing the same values for each variable | |||
* We have to be clear/specific! | |||
* The == operator is ambiguous for objects | |||
* Always, always, always use .equals() | |||
* Can now use boolean zen: return (a==b) && (c==d) | |||
==Section 9.3== | |||
===Definitions=== | |||
Definitions: | |||
* | |||
=Chapter 9 Summary= | |||
=Chapter 9 Goodies= | |||
==Profiles== | |||
Ludwig Wittgenstein | |||
* Quotes/excerpts, philosophy of computer programming | |||
* Think about these concepts as broader language/grammar problems | |||
* This is why we call them COMPUTER LANGUAGES! | |||
=Flags= | =Flags= | ||
{{CSC143Flag}} | {{CSC143Flag}} | ||
Revision as of 20:26, 3 September 2016
Chapter 9: Inheritance and Interfaces
Sections:
9.1 Inheritance basics
9.2 Interacting with the superclass
9.3 Polymorphism
9.4 Inheritance and Design
9.5 Interfaces
9.6 Case study: Financial class hierarchy
Section 9.1: Inheritance Basics
Definitions
Definitions:
- Code reuse
- Is-a relationship
- Inheritance hierarchy
- Inheritance (inherit)
- Superclass
- Subclass
- Override
Material
Large programs on large scale require abstractions - or, another way to look at it, abstractions open up whole new problem spaces
Hierarchy of employees example:
- Legal firm: lawyers, general secretaries, legal secretaries, marketers
- Each person is an employee
- Each X is a Y, &c.
- Types of employees have salaries, skills, etc., some are new methods, some override parent methods
- Is-a relationship between classes
Extending a class:
- Turn non-code example into code
- Employee class, Secretary class
Overriding methods:
- Lawyers, vacation days and vacation forms
- Overloading - re-defining a method from a parent class
- Overriding - one class with multiple versions of the same method for different parameter signatures
Section 9.2: Interacting with the Superclass
Material
Calling overridden methods:
- giving employees a raise: explicitly calling super method
- can only call super once - super.super.method() not allowed
Accessing inherited fields
- more complex example than employee class: stocks class
- encapsulation: even child classes can't examine private data
- implement an accessor method getParam()
- Now the child class can define a method that uses a parameter defined in the parent class
Super class constructor
- Methods are inherited
- Constructors are NOT inherited
- can cause trouble when trying to access private data
Example code from book: constructor that sets various fields to initial values
- Does not compile - fields are all private, cannot be accessed/modified by child class
- Let's examine what the compiler is REALLY saying:
- "Wait a minute, what do you think you're doing setting shares and costs? You're a dividend stock - so you take care of dividends. Let the stock class take care of stock stuff, like shares and costs. Mmmkay, thanks."
- The real solution: let the parent class take care of what it takes care of, let the child class take care of what it takes care of
- Call the super constructor
- Finished code for example constructor
DividendStock Behavior
- implement dividend payments method
- new method simply computes profit same as old method, plus dividends
- Extend getProfit(), which calculates the profit using super.getProfit()
Object class
- superclass for all other Java classes
- implements few key behaviors: clone(), equals(obj), finalize(), getClass(), hashCode(), toString(), notify()/notifyAll()/wait()
- can pass as type for a method or constructor parameter, then deal with any input type (although limited in methods can call)
Objects and equality
- Lamp analogy: if I show you two objects, say a pair of lamps, and I ask you, are these two lamps equal?
- You might ask me: in what sense?
- Brightness? Power consumption?
- Manufacturer, components, parts interchangeable?
- Exact replica, atom-by-atom, of the original lamp?
- Cosmic Zen and oneness with the universe: these two lamps are equal only if they are composed of those exact atoms
- What if one of them is a picture? What if both of them are pictures?
The equals method
- shoes analogy - equivalent, but distinct and separate items
- When comparing two objects, we may want to know if they refer to the same location in memory, where data for an object is located
- Or, we may have two objects referring to different locations in memory, but containing the same values for each variable
- We have to be clear/specific!
- The == operator is ambiguous for objects
- Always, always, always use .equals()
- Can now use boolean zen: return (a==b) && (c==d)
Section 9.3
Definitions
Definitions:
Chapter 9 Summary
Chapter 9 Goodies
Profiles
Ludwig Wittgenstein
- Quotes/excerpts, philosophy of computer programming
- Think about these concepts as broader language/grammar problems
- This is why we call them COMPUTER LANGUAGES!
Flags
| CSC 143 - Intro to Programming II Computer Science 143 - Intro to Programming II, South Seattle College.
Chapter 8: Object Oriented Reivew CSC 143/Chapter 8 Chapter 9: Inheritance and Interfaces CSC 143/Chapter 9 Chapter 10: ArrayList CSC 143/Chapter 10 Chapter 11: Java Collections Framework CSC 143/Chapter 11 Chapter 12: Recursion CSC 143/Chapter 12 Chapter 13: Searching and Sorting CSC 143/Chapter 13 Chapter 14: Stacks and Queues CSC 143/Chapter 14 Chapter 16: Linked Lists CSC 143/Chapter 16
Category:Teaching · Category:CSC 143 · Category:CSC Related: CSC 142 Flags · Template:CSC143Flag · e |