Chapter 10 Flashcards

1
Q

Inheritance

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Superclass

A

Class that’s extended by another class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Subclass

A

-Class that extends (inherits from) another class.
-It inherits all fields and methods from its superclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Inheritance is a what kind of relationship?

A

-is-a relationship
-subclass is a specialization of a superclass
-ie. “a message post is a post” and “a photo post is a post”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Inheritance hierarchy

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Inheritance is an ___ technique that lets us categorize classes of objects under certain criteria and helps us specify the characteristics of these classes

A

abstraction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How is inheritance expressed in Java in the superclass?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How is inheritance expressed in Java in the subclasses?

A

-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.
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Inheritance and access rights

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the keyword “super” do?

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Superclass constructor

A

-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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

super():
inserting this call automatically works only if…

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

T/F: if you do not write a call to a superclass constructor, the superclass constructor will not be executed

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

inheritance allows us to __ previously written classes in a new context

A

-reuse
-one of the great benefits we get from the inheritance facility

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

abstract classes

A

Classes that are not intended to be used to create instances , but whose purpose is exclusively to serve as superclasses for other classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Advantages of inheritance

A
  1. Avoiding code dupes
  2. Code reuse
  3. Easier Maintenance
  4. Extendibility
17
Q

Subtype

A

-As an analog to the class hierarchy, types form a type hierarchy. The type defined by a subclass definition is a subtype of the type of its superclass.
-Classes define types - the type of object created from class MessagePost is MessagePost
-classes may have subclasses
-thus the types defined by the classes can have subtypes
-so MessagePost is a subtype of type Post

18
Q

Variables and subtypes

A

Variables may hold objects of their declared type or of any subtype of their declared type
ie:
Car myCar = new Car();

19
Q

Are these allowed?
Vehicle v1 = new Vehicle();
Vehicle v2 = new Car();
Vehicle v3 = new Bicycle();

A

Yes

20
Q

Substitution

A

Subtype objects may be used wherever objects of a supertype are expected.

21
Q

Is this allowed?
Car c1 = new Vehicle();

A

no
// this is an error!
-The variable is declared to be able to store cars.
-A vehicle may or may not be a car

22
Q

Is this allowed?
Car c2 = new Bicycle();

A

no
// this is an error!
A bicycle is not a car- the type Bicycle is not a subtype of Car

23
Q

We can pass an object of type MessagePost to a method that has a parameter of type Post, how?

A

definition of addPost method:
public void addPost(Post post)

use method to add message posts and photo posts to the feed:
NewsFeed feed = new NewsFeed();
MessagePost message = new MessagePost(. . .);
PhotoPost photo = new PhotoPost(. . .);
feed.addPost(message);
feed.addPost(photo);

24
Q

Polymorphic variables

A

-literally many shapes
-a variable can hold objects of different types (namely, the declared types or any subtype of the declared type)

25
Q

type loss

A

ie. the type of the object in v is actually Car, but the compiler does not know this. - it translates code line by line, so looks at last line with the error in isolation without knowing what’s stored in variable v
-this is actually allowed, if it was executed in sequence:
Vehicle v;
Car c = new Car();
v = c; // correct
c = v; // error

26
Q

Avoid type loss with__

A

-a cast operator
-ie
c = (Car) v;
-consists of the type name (car) written in parenthesis in front of a variable of an expression
-so compiler believes object is a Car and will not report error

27
Q

Which would result in errors?
Vehicle v;
1. Car c;
2. Bicycle b;
3. c = new Car();
4. v = c; // okay
5. b = (Bicycle) c; // compile time error!
6. b = (Bicycle) v; // runtime error!

A

5 and 6 because compiler notices that Car and Bicycle do not form a subtype/supertype relationship, so c can never hold a Bicycle object

28
Q

Casting should be avoided wherever possible, because___

A

-can lead to runtime errors
-in almost all cases when you use a cast, you could restructure your code to avoid this cast and end up with a better-design program
-this usually involves replacing the cast with a polymorphic method call