Inheritance Flashcards
1) What is inheritance? Part 1
Object-oriented programming languages provide the ability to e_______ existing classes by a_______ attributes and
methods to them.
This is called inheritance.
extend
adding
2) What is inheritance? Part 2
Inheritance is the sharing of a______ and m__________ among classes.
We take a class, and then define other classes based on the first one.
The new classes i______ all the attributes and methods of the first one, but also have attributes and methods
of their own.
attributes
methods
inherit
3) The UML notation for inheritance is:
A t___________
triangle
4)
An inheritance relationship is a h___________ relationship.
The class at the top of the hierarchy is referred to as the s___________ (or base class) and the class below is known as the s__________ (or derived class).
hierarchal
superclass
subclass
5) extends
Look at the following header of a subclass: public class PartTimeEmployee extends Employee
‘Extends’ is the k___________ that the PartTimeEmployee class (the subclass) inherits all the attributes
and methods of the Employee class (the s__________).
keyword
superclass
6) A problem
Whilst the keyword e________ means that any object of the subclass will have the attributes of the
b____ class, like always, the attributes in the base class have been d_______ as p_______.
This means that none of the subclass methods can directly access the attributes of the base class.
base
declared as private
7) Solving the problem
There are three possible solutions:
a) We could declare the original attributes as p_________—but this would take away
the whole point of e_____________.
b) We could use the keyword p___________.
c) We could use ‘g____’ and ‘s___’ methods.
public
encapsulation
protected
get and set methods
8) The keyword ‘protected’ (UML notation is a #)
To solve the problem on the previous card we could use the ‘protected’ keyword.
Anything d________ as ‘protected’ is a_______ to the methods of any subclasses.
There are, however, two issues to think about here.
a) Plan in advance what you want your class to be able to i______.
b) It w_______ your efforts to encapsulate information w_____ the class, since, in Java, protected attributes
are also accessible to any other class in the same package
declared
accessible
inherit
weakens
within
9) ‘super’
This line of code adds a new attribute to our subclass. Remember, we also have inherited attributes.
private double hourlyPay;
Next comes the c___________.
We want to be able to assign values to our attributes.
Our constructor will need to receive p_________ that will be assigned to the inherited attributes.
But these attributes have been declared as private in the superclass—so they aren’t accessible to objects of the subclass.
We can call the constructor of the superclass by using the keyword ‘super’.
constructor
parameters
10) Look at the following code
public PartTimeEmployee(String numberIn, String nameIn, double hourlyPayIn)
{
super(numberIn, nameIn); // call the constructor of the superclass
hourlyPay = hourlyPayIn;
}
Notice, that the line that calls super has to be the first one—if we had written our constructor with our attribute first (hourly pay = hourlyPayIn) it would not c___________.
complie
11) type-casting
Type casting means f________ an item to change from one t______ to another.
As you can see from the following code, type casting is achieved by placing the new type name in b_________
before the item you wish to change.
e.g.
l = (int) getLength();
h = (int) getHeight(); \length and height would have originally been doubles.
forcing
type
brackets
12) method overriding
Method overriding is another way of achieving p___________ (more than one use of something, like method overloading).
polymorphism
13) Java annotation
Look at how ‘override’ is shown in the following code:
@Override
public boolean withdraw(double amountIn) \withdraw is the method that is being overridden
This is an example of a Java a______________.
Annotations begin with the @ symbol, and always start with an upper case letter.
annotation
14) Why would we note that we are overriding a method with the @Override annotation?
To ensure that we give the overridden method exactly the same n____ and p________ list as the
method it is supposed to be overriding.
Without the annotation, this would escape the notice of the compiler, and you would have simply written a n___ method.
But with the annotation included you would get a compile error if the method headings did not match
name
parameter
new
15) Method Overloading vs Method Overriding
With method overloading the methods with the same name within a class are d__________ by their p_______
l______.
In the case of method overriding, the methods have the same parameter list but belong to different classes—the superclass and the subclass.
In this case, they are distinguished by the o_______ with which they are associated.
distinguished by their parameter lists
object