Chapter 5 - Class Design Flashcards

1
Q

Inheritance

(4)

A
  • Inheritance is the process by which the new child subclass automatically includes any public or protected primitives, objects, or methods de ned in the parent class.
  • Java supports single inheritance, by which a class may inherit from only one direct parent class.
  • Java does allow one exception to the single inheritance rule: classes may implement multiple interfaces.
  • It is possible in Java to prevent a class from being extended by marking the class with the final modi er.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Extending

(2)

A
public abstract **class** ElephantSeal **extends** Seal {
 // Methods and Variables defined here
}
  • must be public or default accessible
  • can be abstract or final itself (but doesnt have to)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Class Modifiers

(4)

A
  • public:
    indicates that it can be referenced and used in any class
  • default package private access:
    indicates the class can be accessed only by a subclass or class within the same package
  • protected:
    can only be applied to inner classes
  • private:
    can only be applied to inner classes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Java Object Inheritance

(2)

A

The key is that when Java sees you define a class that doesn’t extend another class, it immediately adds the syntax extends java.lang.Object to the class defnition.

If you define a new class that extends an existing class, Java doesn’t add this syntax, although the new class still inherits from java.lang.Object. Since all classes inherit from java.lang.Object, extending an existing class means the child automatically inherits from java.lang.Object by construction.

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

Constructor Rule

(5)

A
  1. The first statement of every constructor is a call to another constructor within the class using this(), or a call to a constructor in the direct parent class using super().
  2. The super() or this() call may not be used after the first statement of the constructor.
  3. If no super() call is declared in a constructor, Java will insert a no-argument super() as the first statement of the constructor.
  4. If the parent doesn’t have a no-argument constructor and the child doesn’t define any constructors, the compiler will throw an error and try to insert a default no-argument constructor into the child class.
  5. If the parent doesn’t have a no-argument constructor, the compiler requires an explicit call to a parent constructor in each child constructor.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Calling Inherited Class Members

(4)

A
  • Java classes may use any public or protected member of the parent class, including meth- ods, primitives, or object references.
  • If the parent class and child class are part of the same package, the child class may also use any default members de ned in the parent class.
  • Finally, a child class may never access a private member of the parent class, at least not through any direct reference.
  • In Java, you can explicitly reference a member of the parent class by using the super keyword
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Overriding a Method

(5)

A
  1. The method in the child class must have the same signature as the method in the parent class.
  2. The method in the child class must be at least as accessible or more accessible than the method in the parent class. (private methods can be redeclared though)
  3. The method in the child class may not throw a checked exception that is new or broader than the class of any exception thrown in the parent class method.
  4. If the method returns a value, it must be the same or a subclass of the method in the parent class, known as covariant return types.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Hiding Methods

(5)

A
  1. The method in the child class must have the same signature as the method in the parent class.
  2. The method in the child class must be at least as accessible or more accessible than the method in the parent class.
  3. The method in the child class may not throw a checked exception that is new or broader than the class of any exception thrown in the parent class method.
  4. If the method returns a value, it must be the same or a subclass of the method in the parent class, known as covariant return types.
  5. The method defined in the child class must be marked as static if it is marked as static in the parent class (method hiding). Likewise, the method must not be marked as static in the child class if it is not marked as static in the parent class (method overriding).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Creating final methods

(1)

A
  • final methods cannot be overridden or hidden
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Hiding Variables

(4)

A
  • you can’t override a variable; you can only hide it
  • If you’re referencing the variable from within the parent class, the variable de ned in the parent class is used
  • Alternatively, if you’re referencing the variable from within a child class, the variable de ned in the child class is used
  • Likewise, you can refer- ence the parent value of the variable with an explicit use of the super keyword.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Abstract Class Definition Rules (5)

A
  1. Abstract classes cannot be instantiated directly.
  2. Abstract classes may be defined with any number, including zero, of abstract and non-abstract 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
12
Q

Concrete Classes

(2)

A
  • A concrete class is the first nonabstract subclass that extends an abstract class and is required to implement all inherited abstract methods.
  • a concrete subclass is not required to provide an implementation for an abstract method if an interme- diate abstract class provides the implementation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Abstract Method Definition Rules

(4)

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 a subclass follows the same rules for overriding a method. For example, the name and signature must be the same, and the visibility of the method in the subclass must be at least as accessible as the method in the parent class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Object vs. Reference

(2)

A
  1. The type of the object determines which properties exist within the object in memory.
  2. The type of the reference to the object determines which methods and variables are accessible to the Java program.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Casting Objects

(4)

A
  1. Casting an object from a subclass to a superclass doesn’t require an explicit cast.
  2. Casting an object from a superclass to a subclass requires an explicit cast.
  3. The compiler will not allow casts to unrelated types.
  4. Even if the code compiles without issue, an exception may be thrown at runtime if the object being cast is not actually an instance of that class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Virtual Methods

(1)

A

A virtual method is a method in which the specific implementation is not determined until runtime. In fact, all non-final, non-static, 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.

17
Q

Defining an Interface

(6)

A
  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. private or protected.
  4. All top-level interfaces are assumed to have public or default access, and they are asusmed to be abstract.
  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.
  6. Variables in Interfaces are assumed to be public, static an final.
18
Q

Inheriting an Interface

(2)

A
  1. An interface that extends another interface, as well as an abstract class that implements an interface, inherits all of the abstract methods as its own abstract methods.
  2. The first concrete class that implements an interface, or extends an abstract class that implements an interface, must provide an implementation for all of the inherited abstract methods.
19
Q

Classes, Interfaces, and Keywords

(2)

A
  1. A Class can implement an Interface but not extend it
  2. A Interface can extend another Interface but on implement it
20
Q

Abstract Methods and Multiple Inheritance

A
  • if the method name and input parameters are the same but the return types are different between two methods, the class or interface attempting to inherit both interfaces will not compile
21
Q

Interface Variables

A
  1. Interface variables are assumed to be public, static, and final. Therefore, marking a variable as private or protected will trigger a compiler error, as will marking any variable as abstract.
  2. The value of an interface variable must be set when it is declared since it is marked as final.
22
Q

Default Interface Methods

(5)

A
  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.
  5. If a class implements two interfaces that have default methods with the same name and signature, the compiler will throw an error
23
Q

Static Interface Methods

(2)

A
  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.