Inheritance: Abstract Classes Flashcards

1
Q

Final Methods and Classes

A

The final modifier can be used with methods and classes to control how they are used. A final method cannot be Overridden. A final class cannot have subclasses.

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

Abstract Class

A

An abstract class cannot be instantiated and exists solely for the purpose of inheritance and polymorphism. Like a combination of an interface and a superclass.

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

Abstract Method

A

A method signature with the abstract modifier. Can only exist in an abstract class, and must be overridden in any implementation class that extends the abstract class.

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

Reasons to use an Abstract Class

A
To prevent a superclass from being instantiated.  
For example:  Having a Generic “Feline” as an object may not make sense, so by making Feline abstract it forces the user of the class to instantiate the more concrete HouseCat or Lion objects that are subclasses of Feline. When you need to have the ability to inherit functionality from a superclass and force a subclass to implement subclass specific methods. 
Abstract classes should only represent an IS-A relationship (a Car IS-A Vehicle) and never a HAS-A relationship (a Car HAS-A Drivable)
This same need can also be accomplished by using a combination of a superclass and interfaces.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Private Access Modifiers

A
Accessible on in the class
can be applied to methods and member variables
Protected - accessible in the class and in any subclasses in the inheritance tree.  
can be applied to methods and member variables
In Java, Protected it also available to any class in the same package, but this use is discouraged.
Default (no access modifier) - accessible to any class or subclass in the same package.  
can be applied to methods and member variables
Public - accessible everywhere 
can be applied to methods, member variables, classes, and interfaces)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
When to use each accessor
Public
Protected
Private
Default
A
Public is for “set in stone” methods that you want other programmers to rely on to use your object.  These create the behaviors of the object, but changing their method signatures may break other code that is using your object.
Protected is for building connections between inherited classes.  It lets you have methods in a superclass that are accessible to the subclasses, but does not allow access outside the hierarchy.
Private is for unstable, worker methods that may change and are only for use inside the class itself.  
Default should generally be avoided.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Access Modifiers

A

Class design should include how others will use your object, the methods that allow that use should be public. All other methods and variables should be private, until needed in the hierarchy or publically. So if you are unsure, start as private and increase the access as needed.

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

Abstract class characteristics

A
  1. Can extend it like a superclass.
  2. Can inherit implementation from it like a superclass.
  3. Can provide method signatures that must be implemented like an interface.
  4. A class can only extend either 1 abstract class or 1 superclass but may implement multiple interfaces.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly