Abstract Classes Flashcards
1) Abstract classes
You can declare a class as abstract.
Once a class has been declared in this way it means that you are not allowed to c______ o_______ of that class.
create objects
2) Why would you want to declare a class as ‘abstract’?
Some classes work well as a template or a base class, whose attributes and methods can be inherited by subclasses.
It is the subclasses that we would use to create objects that are better suited to real-world objects.
See Java in two Semesters, chp 9, p. 251
3) How to declare a class as abstract?
Place the keyword abstract in the h__________:
e.g.
public abstract class Employee
header
4) The ‘final’ Modifier
The keyword final can be used to modify a variable and turn it into a constant.
It can also be used to modify a c_____ and a m______.
In the case of a class it is placed before the class declaration, like this:
public final class SomeClass
{
// code goes here
}
class
method
5) What does the ‘final’ keyword do if we use it to modify a class?
e.g.
public final class SomeClass
{
// code goes here
}
A: It means that the class cannot be s_____________.
subclassed
6) What does the ‘final’ keyword do if it is used to modify a method?
e.g.
public final void someMethod()
{
// code goes here
}
A: It means that the method cannot be o___________.
overridden
7) The toString Method
The toString method allows us to display all the i__________ about an object without having to invoke a lot of individual m___________ each time. (Great for testing!)
toString belongs to the Object class and is therefore inherited by all classes, and can be overridden for each individual class.
e.g.
@Override
public String toString() \here the String method is being overloaded
{
return “Name: “ + accountName + ‘\n’ + “Account number: “ + accountNumber + ‘\n’
+ “Balance: “ + balance;
}
information
methods
8)