You are probably being recorded, perhaps even transcribed.
Approximate overview
Academic/Scholarly
Cultural
Peer
Wellness
Misc
Note: You can find code in examples/inheritance.
We’ll address some of the questions from the readings by revisiting our mental model.
Do superclasses include info about their subclasses?
No
Can you cast from a subclass to a superclass?
Yes, but there’s rarely a reason to so.
Can you explain protection for fields?
Sure. Remember that we use protection to achieve one of the goals of encapsulation: We are free to make changes to our code without significantly affecting our clients. Different situations require different choices.
There are four levels:
private, “package” (write nothing),protectedandpublic.
privatemeans that the field or method is only available to the class or other objects in the class.
“package” expands that availability to other classes in the same package as well as objects in those classes.
protectedfurther expands it to subclasses in other packages.
publicexpands it to everyone.
Can you explain how we use super in methods?
You will most frequently use
superin constructors to make a call to the appropriate constructor in the superclass.
If you don’t do so, Java will look for a zero-ary constructor in the superclass and call that.
At times, when we want to use a method from the superclass that has been overridden in the subclass, we can write
super.methodName.
How do we know when to use interfaces and when to use inheritance?
Practice.
In general, we use inheritance when we have fields and methods we want to reuse from another class.
We use interfaces when we don’t have that kind of reuse.
We use either when we want to write functions that work with multiple similar objects.
Do you need to specify public or private for all fields?
Nope. There’s also “package” (writing nothing) and
protected.
I prefer package protection because it often achieves the right level of protection (available to other classes in the same package, but not classes outside the package).
What’s wrong with these as solutions to the self checks?
public class StaffMember extends Person {
String department;
}
public class Administrator extends person {
}