Chapter 5 Class Design Flashcards

1
Q

Inheriting a class gives you access to … ?

A
Inheriting a class gives you access to all of the public and protected methods of the
class, but special rules for constructors and overriding methods must be followed or the code will not compile.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Should we provide in the child’s constructors parent class constructor ?

A
If the parent class doesn’t include a no-argument constructor,
an explicit call to a parent constructor must be provided in the child’s constructors.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is variable hiding (inheritance aspect) ?

A

When you hide a variable, you define a variable with the same name as a variable in a parent class. This creates two copies of the variable within an instance of the child class: one instance defined for the parent reference and another defi ned for the child reference.

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

What is the difference between overriding and hiding methods ?

A

Unlike overriding a method, in which a child method
replaces the parent method in calls defi ned in both the parent and child, hidden methods only replace parent methods in the calls defi ned in the child class.

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

What abstract class definition rules you know (instantiation, numbers of methods, modifiers, inheritance, implementation)?

A
  1. Abstract classes cannot be instantiated directly.
  2. Abstract classes may be defined with any number, including zero, of abstract and nonabstract methods.
  3. Abstract classes may not be marked as private or final.
  4. An abstract class that extends another abstract class inherits all of its abstract methods as its own abstract methods.
  5. The first concrete class that extends an abstract class must provide an implementation for all of the inherited abstract methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What abstract method definition rules you know (scope of definition, modifiers, implementation,)?

A
  1. Abstract methods may only be defined in abstract classes.
  2. Abstract methods may not be declared private or final.
  3. Abstract methods must not provide a method body/implementation in the abstract class for which is it declared.
  4. Implementing an abstract method in
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is interface ?

A

It may be helpful to think of an interface as a specialized kind of abstract class, since it shares many of the same properties and rules as an abstract class. The following is a list of rules for creating an interface, many of which you should recognize as adaptions of the rules for defining abstract classes:

  1. Interfaces cannot be instantiated directly.
  2. An interface is not required to have any methods.
  3. An interface may not be marked as final.
  4. All top-level interfaces are assumed to have public or default access, and they must include the abstract modifier in their definition. Therefore, marking an interface as private, protected, or final will trigger a compiler error, since this is incompatible with these assumptions.
  5. All nondefault methods in an interface are assumed to have the modifiers abstract and public in their definition. Therefore, marking a method as private, protected, or final will trigger compiler errors as these are incompatible with the abstract and public keywords.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is default interface methods (purpuse and rules) ?

A

A default method is a method defi ned within an interface with the default keyword in which a method body is provided. Contrast default methods with “regular” methods in an interface, which are assumed to be abstract and may not have a method body.

A default method within an interface defi nes an abstract method with a default implementation. In this manner, classes have the option to override the default method if they need to, but they are not required to do so. If the class doesn’t override the method, the default implementation will be used. In this manner, the method defi nition is concrete, not abstract.

The following are the default interface method rules you need to be familiar with:

  1. A default method may only be declared within an interface and not within a class or abstract class.
  2. A default method must be marked with the default keyword. If a method is marked as default, it must provide a method body.
  3. A default method is not assumed to be static, final, or abstract, as it may be used or overridden by a class that implements the interface.
  4. Like all methods in an interface, a default method is assumed to be public and will not compile if marked as private or protected.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is static interface methods ?

A
Java 8 also now includes support for static methods within interfaces. These methods are defined explicitly with the static keyword and function nearly identically to static methods defined in classes. In fact, there is really only one distinction
between a static method in a class and an interface. A static method defined in an interface is not inherited in any classes that implement the interface.

Here are the static interface method rules you need to be familiar with:

  1. Like all methods in an interface, a static method is assumed to be public and will not compile if marked as private or protected.
  2. To reference the static method, a reference to the name of the interface must be used.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is called virtual method ?

A

A virtual method is a method in which the specific implementation is not determined until runtime. In fact, all non-final, nonstatic, and non-private Java methods are considered virtual methods, since any of them can be overridden at runtime. What makes a virtual method special in Java is that if you call a method on an object that overrides a method, you get the overridden method, even if the call to the method is on a parent reference or within the parent class.

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

What modifiers are implicitly applied to all interface methods ?

A

All interface methods are only implicitly public. Since Java 8 now includes default and static methods and they are never abstract, you cannot assume the abstract modifier will be implicitly applied to all methods by the compiler.

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

Can we override nonprivate static method from parent class to non-static method in the child class ?

A

No. For nonprivate methods in the parent class, both methods must use static
(hide) or neither should use static (override).

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

When null can be passed as an object value ?

A

Null value can always be passed as an object value, regardless of type.

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