CSCI 1302 Exam 2 study guide
What is inheritance in Java? - ANS-defining a new class by extending an existing class/creating
a new class based on definition of other classes
What is inheritance good for? - ANS-reuse and avoiding redundancy
What type of relationship is inheritance? - ANS-is-a relationship
example: If we have an animal class and a dog class. The dog class can extend the animal
class, since a dog "is a" animal.
What does the newly defined class that extends the superclass gain? - ANS-all accessible data
properties and methods, but NOT constructors
Between an Animal class and a Dog class, which is the subclass? - ANS-the dog class,
because it is the extended class
T or F? Subclass "is a" superclass - ANS-true.
for example, a cat is an animal
Other terms for superclass? - ANS-parent class, base class, supertype
Other terms for subclass? - ANS-child class, extended class, derived class, subtype
What can a subclass do? - ANS-it can add data, add methods, and override methods (from
parent class)
If changes are made to the superclass, does the subclass automatically receive these changes?
- ANS-yes
What must a subclass use? - ANS--get/set methods for superclass data members (data
members in superclass should be private)
How do you indicate inheritance in a UML diagram? - ANS-an arrow that points from the
subclass to the superclass
example: The Dog class would point to the Animal, its parent class
Define a Cat class that extends Animal class - ANS-public class Cat extends Animal {
// implement cat class
}
, How must the private data fields in parent class be accessed? - ANS-through getter and setters,
due to visibility rules
T or F? Inheritance models "is a" relationship but should not be used for every "is a"
relationship. - ANS-true, just use your best judgement based on classes and their
characteristics, not based on convenience
Can you inherit from multiple classes in java? - ANS-no, java only has single inheritance
Can a superclass have multiple subclasses? - ANS-yes. a superclass can have multiple
subclasses but a subclass can not have more than one parent class.
for example, a Dog class, Elephant class, and Cat class can all extend an Animal class
what does the super keyword do? - ANS-it is similar to the "this" keyword but it refers to the
parent class instead of self. You can use it to access a constructor or method that is in the
superclass.
example: super(); calls the default constructor from the superclass.
if calling a superclass constructor, where does the keyword need to be used? - ANS-at the
beginning or the first line, just like with the this keyword.
how come you get everything for "free" when it comes to superclass and subclasses - ANS-java
automatically calls super in child class constructors
what does java execute up to the "extends" chain? - ANS-all default superclass constructors.
this is referred to as constructor chaining
what is constructor chaining and how does it work? - ANS-The process of calling one
constructor from within another constructor. Constructor invocations chain backwards through
ALL parent classes automatically
example of calling the toString() of the superclass? - ANS-super.toString()
what is overriding a method? - ANS-language feature that allows a subclass or child class to
provide a specific implementation of a method that is already provided by the parent class.
example:
@Override
public String toString() {
//whatever string you want to return
}
What is inheritance in Java? - ANS-defining a new class by extending an existing class/creating
a new class based on definition of other classes
What is inheritance good for? - ANS-reuse and avoiding redundancy
What type of relationship is inheritance? - ANS-is-a relationship
example: If we have an animal class and a dog class. The dog class can extend the animal
class, since a dog "is a" animal.
What does the newly defined class that extends the superclass gain? - ANS-all accessible data
properties and methods, but NOT constructors
Between an Animal class and a Dog class, which is the subclass? - ANS-the dog class,
because it is the extended class
T or F? Subclass "is a" superclass - ANS-true.
for example, a cat is an animal
Other terms for superclass? - ANS-parent class, base class, supertype
Other terms for subclass? - ANS-child class, extended class, derived class, subtype
What can a subclass do? - ANS-it can add data, add methods, and override methods (from
parent class)
If changes are made to the superclass, does the subclass automatically receive these changes?
- ANS-yes
What must a subclass use? - ANS--get/set methods for superclass data members (data
members in superclass should be private)
How do you indicate inheritance in a UML diagram? - ANS-an arrow that points from the
subclass to the superclass
example: The Dog class would point to the Animal, its parent class
Define a Cat class that extends Animal class - ANS-public class Cat extends Animal {
// implement cat class
}
, How must the private data fields in parent class be accessed? - ANS-through getter and setters,
due to visibility rules
T or F? Inheritance models "is a" relationship but should not be used for every "is a"
relationship. - ANS-true, just use your best judgement based on classes and their
characteristics, not based on convenience
Can you inherit from multiple classes in java? - ANS-no, java only has single inheritance
Can a superclass have multiple subclasses? - ANS-yes. a superclass can have multiple
subclasses but a subclass can not have more than one parent class.
for example, a Dog class, Elephant class, and Cat class can all extend an Animal class
what does the super keyword do? - ANS-it is similar to the "this" keyword but it refers to the
parent class instead of self. You can use it to access a constructor or method that is in the
superclass.
example: super(); calls the default constructor from the superclass.
if calling a superclass constructor, where does the keyword need to be used? - ANS-at the
beginning or the first line, just like with the this keyword.
how come you get everything for "free" when it comes to superclass and subclasses - ANS-java
automatically calls super in child class constructors
what does java execute up to the "extends" chain? - ANS-all default superclass constructors.
this is referred to as constructor chaining
what is constructor chaining and how does it work? - ANS-The process of calling one
constructor from within another constructor. Constructor invocations chain backwards through
ALL parent classes automatically
example of calling the toString() of the superclass? - ANS-super.toString()
what is overriding a method? - ANS-language feature that allows a subclass or child class to
provide a specific implementation of a method that is already provided by the parent class.
example:
@Override
public String toString() {
//whatever string you want to return
}