Programming - 5 - Inheritance Flashcards
1
Q
Definition of public, private or protected class members
A
- Public members can be accessed from outside the class, i.e. by objects of other classes that have a reference to an object of this class
- Private members can only be accessed from methods of this class
- Protected members can only be accessed from methods of this class or of any derived classes through inheritance.
2
Q
Get and Set Methods
A
- Private or protected instance variables that need to be accessed externally typically have associated and methods
- Difference with maing the methods public is that:
- using get and set methods controls how these variables are accessed
3
Q
What is at the top of the Inheritance hierarchy?
A
- Top of the hierarchy always the Java class Object
- Every class that we define without inheritance inherits from Object, with the compiler setting implicitly the superclass to Object
- So effectively there is a class inheritance tree with Object the root
- A key method supported by Object is toString which subclasses can redefine (override)
4
Q
What is Data Encapsulation
A
Implementation details of a class are kept hidden fro the user (getters and setters are used)
5
Q
What is an Abstract Class?
A
- Superclass that will never be instantiated only subclassed (e.g. class Shape)
- Incomplete, only providing methods to be redfined in subclasses.
- Key: If at least one method is “abstract”, class should be “abstract” !
6
Q
What is an interface?
A
- Abstract type used to specify behaviours that classes must implement
- variables are final, all methods have to be defined (for abstract class, they don’t)
- classes can extend multiple interfaces
- essentially an abstract class but with all the methods having to be defined by subclasses.
7
Q
What is polymorphism?
A
- Object passes multiple is-a tests
- Example
- public interface vegetarian {};
- public class animal {};
- public class Deer extends Animal implements vegetarian{};
8
Q
What sort of relationship does inheritance represent?
A
is-a
9
Q
What sort of relationship does composition represent?
A
has-a