Chapter 10 Flashcards
Inheritance
- allows us to define one class as an extension of another
- solution to duplication
- instead of defining certain classes independently, we first define a class that contains everything they have in common
Superclass
Class that’s extended by another class
Subclass
-Class that extends (inherits from) another class.
-It inherits all fields and methods from its superclass
Inheritance is a what kind of relationship?
-is-a relationship
-subclass is a specialization of a superclass
-ie. “a message post is a post” and “a photo post is a post”
Inheritance hierarchy
-classes that are linked through inheritance relationships form an inheritance hierarchy
-ie. a poodle is a dog, which is a mammal, which is an animal
Inheritance is an ___ technique that lets us categorize classes of objects under certain criteria and helps us specify the characteristics of these classes
abstraction
How is inheritance expressed in Java in the superclass?
- nothing different
public class Post
{
private String username; // username of the post’s author
private long timestamp;
private int likes;
private ArrayList<String> comments;
// Constructors and methods omitted.
}</String>
How is inheritance expressed in Java in the subclasses?
-keyword extends defines the inheritance relationship
-only defines the fields unique to the class objects - the fields from the superclass (Post) are inherited and do not need to be listed here
public class MessagePost extends Post
{
private String message;
// Constructors and methods omitted.
}
Inheritance and access rights
-members defined as public in either the superclass or subclass portions will be accessible to objects of other classes, but members defined as private will be inaccessible.
-a subclass cannot access private members of its superclass
-if a subclass method needed to access or change private fields in its superclass, then the superclass would need to provide appropriate accessor and/or mutator methods
What does the keyword “super” do?
-it’s a call from the subclass constructor to the constructor of the superclass
-its effect is that the Post constructor is executed as part of the MessagePost constructor’s execution
-message post created- MessagePost constructor is called- which as its first statement calls the Post constructor - which initializes post’s fields- then returns to MessagePost constructor- which initializes remaining field defined in the MessagePost class
-for the above to work, those parameters need for the initialization of the post fields are passed on to the superclass constructor as parameters to the super call
Superclass constructor
-The constructor of a subclass must always invoke the constructor of its superclass as its first statement
-if source code does not include such a call, Java will attempt to insert a call automatically
- super();
-ie.
public MessagePost(String author, String text)
{
super(author);
message = text;
}
super():
inserting this call automatically works only if…
-the superclass has a constructor w/o parameters (because the compiler cannot guess what parameter values should be passed) - otherwise an error will be reported
T/F: if you do not write a call to a superclass constructor, the superclass constructor will not be executed
F - if you do not write a call to a superclass constructor, the Java compiler will insert a superclass call automatically, to ensure that the superclass fields are properly initialized
-but, it’s a good idea to always include explicit superclass calls in the constructors.
inheritance allows us to __ previously written classes in a new context
-reuse
-one of the great benefits we get from the inheritance facility
abstract classes
Classes that are not intended to be used to create instances , but whose purpose is exclusively to serve as superclasses for other classes