Ch 3 Java Inheritance
subclass
-The class which inherits the properties of other -(derived class, child class)
superclass
-the class whose properties are inherited -(base class, parent class)
Class
A blueprint for an object.
Class Hierarchy
A child of one class can be the parent of another, creating a...
one, many
A class can have exactly ___ parent class, but it might have ____ child classes.
subclass
A class derived from a superclass. Also called a child class.
superclass
A class from which another is derived. Also called a parent class or base class.
What does the protected access class modifier do?
Allows subclasses to have access to superclass data member and methods marked as such.
Why should you never use == to compare the attributes of objects?
Because it compares whether or not two references point to the same object in memory, not if they're attributes are the same. equals() can be overridden. == cannot.
@Override is used to ________?
Declare that a superclass method is being overriden. It is optional, and placed directly above the method that is doing the overriding. It is also a safety mechanism and will return an error if no superclass method exists in the same format as the overridden method.
Inheritance uses a ______ relationship.
IS-A
A(n) ______ cannot be instantiated
Interface
What does the "super" reference do?
It can be used to refer to superclass methods, and is used to refer to the superclass constructor.
toString() can be overridden at multiple _______ to meet class implementations and outputs.
Levels
Why do we use inheritance?
Minimizes redundant code for common traits
All classes are subclasses of the _________ class.
Object
If no direct superclass is specified, than a class will inherit from the __________ class by default.
Object
In Java, all classes inherit from the ______ class by default
Object
Software Reuse
One purpose of inheritance.
superclass and base class
Other names for the parent of a subclass are ______ and ______ .
child, derived
Other names for the sub-class of a parent are ___ class and ___ class.
Subclasses can _________ methods defined in a superclass.
Override
______ is when a subclass redefines a method of it's parent class
Overriding
________ members are inherited by the child class, but cannot be referenced directly by name. may be used indirectly by calling methods.
Private.
Class Hierarchy
The "family tree" of classes.
What is inheritance?
The action of taking methods and fields from a parent class and allowing a child or subclass to inherit and use those methods and fields
subclass
The class which inherits the properties of other It is also called a derived class or child class
superclass
The class whose properties are inherited It is also called a base class or parent class
A private member of a class is visible only to ______
The defining class
is-a
The relationship between a child and parent class.
What is the difference between a superclass and a subclass?
The superclass is a generalized class containing all common methods and fields The subclass is the child class that adds methods and fields that are unique to it.
How do you use inheritance in a class?
Use the extends keyword after the class name: public class Car extends Vehicle
Overrides
When a child class defines a method with the same name/signature as a method of the parent class, the child class... the parent.
The Principle of Encapsulation
When variables of the parent class are declared public so that the child class can access them, the ... is violated.
concrete class
a class for which you can create an object (basically anything that isn't abstract)
abstract class
a class for which you cannot create an object
Subclasses
a class that is a special case of an existing class
An ______ cannot be instantiated
abstract class
class for which you cannot create obects
abstract class
public & private are examples of...
access modifiers
cannot
an abstract class ___ be instantiated
instantiated
an interface cannot be
The subclass inherits these from the superclass
behavior and state
An abstract method does not have a ______
body
A class ______ extend multiple classes
cannot
extends syntax
class Super { ..... ..... } class Sub extends Super { ..... ..... }
a class which you can create objects for
concrete class
Object
every class in Java is a descendent of the ___ class
A public member of a class is visible to ______
everything
In Java, a class can ______ up to one other class to inherit from it
extend
Classes use ______in the header to create child classes.
extends
The keyword used to inherit a class is...
extends
Inheritance is a mechanism in which one class can inherit the ______ and methods of another "parent" class
fields
a class can implement one or more interfaces
helped Java get rid of the impossibility of multiple inheritance.
If no superclass constructor reference exists in the subclass constructor, then the superclass _________________ a default superclass constructor _________ before any code is run in the constructor.
implicitly calls; super()
IS-A Relationship
is a way of saying: This object is a type of that object
extends
is the keyword used to inherit the properties of a class
An Interface consists solely of ______
method signatures
Inheritance is a mechanism in which one class can inherit the fields and ______ of another "parent" class
methods
A class derived from an abstract parent must _______ all of it's parent's abstract methods.
override , or the derived class will also be considered abstract.
all methods of classes in the same package can access the feature
package access
the default access when no access modifier is given
package access
Inheritance is a mechanism in which one class can inherit the fields and methods of another "______" class
parent
four levels of controlling access to fields
public access, private access, protected access, package access
single inheritance B -> A
public class A { ..... } public class B extends A { ..... }
overriding
redefining a method in a subclass when we want a method in the child class to behave differently than in its parent class
more specialized class that inherits from the superclass
subclass
Any new instance fields that you define in the subclass are present only in
subclass objects
A protected member of a class is visible to ______
subclasses
To call the constructor of a parent class, you must use the ______ keyword
super
use this keyword to call a method of the superclass
super ex. super.deposit(amount);
How do you call upon the constructor of a superclass?
super();
Invoking the parameterized Superclass Constructor
super(values);
first
the call using super must be the ____ statement in the constructor
If you define a method that does not exist in the superclass then,
the new method can only be applied to subclass objects
Inheritance
the process where one class acquires the properties (methods and fields) of another. -manageable in a hierarchical order.
interface
you can have reference variables with ___ types
salespeople
you have an employee class and you want to have a special class for employees who are ______.