Chapter 5 Flashcards

1
Q

Method Hiding (5 rules)

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
2
Q

Constructor definition (5 rules)

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() 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
3
Q

super() vs super

A

super(), is a statement that explicitly calls a parent constructor and may only be used in
the first line of a constructor of a child class.

The second, super, is a keyword used to reference a member defined in a parent class and may be used throughout the child class

OCA will try to trick you. Look at how super() and super are used:

public Rabbit(int age) {
super();
super.setAge(10);
}

public Rabbit(int age) {
super; // DOES NOT COMPILE
super().setAge(10); // DOES NOT COMPILE
}

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

Method Overriding (4 rules)

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

final methods

A

final methods cannot be overridden.

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

Defining an interface (5 rules)

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

Inheriting an Interface (2 rules)

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Interface Variables (2 rules)

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Default Interface Methods (4 rules)

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Static Interface Methods (2 rules)

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Polymorphism

A

The property of an object to take on many different forms.

A Java object may be accessed using a reference with the same type
as the object, a reference that is a superclass of the object, or a reference that defi nes an
interface the object implements, either directly or through a superclass. Furthermore, a cast
is not required if the object is being reassigned to a super type or interface of the object.

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

Casting Variables (4 rules)

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 when 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
13
Q

Objects Vs. Reference (2 rules)

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.

In Java, all objects are accessed by reference, so as a developer you never have direct access
to the object itself. Conceptually, though, you should consider the object as the entity that
exists in memory, allocated by the Java runtime environment. Regardless of the type of the
reference you have for the object in memory, the object itself doesn’t change.

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

Concrete class

A

A concrete class is the first nonabstract subclass that extends an abstract class and is required to implement all inherited abstract methods.

When you see a concrete class extending an abstract class on the exam, check that it implements all of the required abstract methods.

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