Algorithms and OOD (CSC 207 2014F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [Learning Outcomes] [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Student-Curated Resources] [Java 8 API] [Java 8 Tutorials] [Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2014S (Rebelsky)] [CSC 207 2014F (Walker)] [CSC 207 2011S (Weinman)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Overview
Some questions and lessons from the lab on inheritance.
Consider these declarations
public class Super
{
...;
} // class Super
public class Sub
extends Super
{
...;
} // class Sub
Super fluous = new Super(...);
Sub marine = new Sub(...);
marine in places where the code wants an object of type
Super? Why or why not?
fluous in places where the code wants an object of type
Sub? Why or why not?
Super"?
Super califragilistic = ...
Suppose we write
Super fluous = new Super(...);
Sub marine = new Sub(...);
Super sonic = marine;
pen.println(sonic.toString());
Super and Sub provide toString methods, when I call
sonic.toString(), which method do I get? Why?
sonic is a SuperSuper sonic = (Super) marine;Sub provides a method, system(), can I call sonic.system()?
Sub, so we should be
able to use it.Counter that refers to a DecrementableCounter, you
can't call the decrement method. This is similar.)Sub.system includes a call to toString,
how does the programmer implementing Sub.system indicate whether
he/she/ze wants to use Sub.toString or Super.toString?
toString to use?
Examples in /examples/SuperSub
Constructors
Additional Information
Remember
public class Counter
{
...
public String toString()
{
...
} // toString()
public void increment()
{
...
} // increment()
public void reset()
{
...
} // reset()
} // class Counter
Suppose we want another counter that counts twice as fast. What approaches might we take?