5 Inheritance Flashcards

1
Q

What do subclasses include from the parent class?

A
new child subclass automatically includes any
public or protected primitives, objects, or methods   defined in the parent class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

If a class is marked final, what does that mean?

A

The class cannot be subclassed

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

Explain how super and this work for constructors in a subclass and where they are located.

A
In Java, the first statement of every constructor is either a call to another constructor within the class, using this(), or a call to a constructor in the direct parent class, using super(). If a parent constructor takes arguments, the super constructor would also take arguments. For simplicity in this section, we refer to the super() command as any parent
constructor, even those that take an argument. Notice the user of both super() and super(age) in the following example:
public class Animal {
private int age;
public Animal(int age) {
super();
this.age = age;
}
}
public class Zebra extends Animal {
public Zebra(int age) {
super(age);
}
public Zebra() {
this(4);
}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the constructor rules?

A
Constructor Defi nition Rules:
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.
Make sure you understand these rules; the exam will often provide code that breaks one
or many of these rules and therefore doesn’t compile.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a good trick question about inheirtance?

A
You should be wary of any exam question in which the parent class defines a constructor
that takes arguments and doesn’t define a                 
no-argument constructor. Be sure to check that
the code compiles before answering a question about it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe the rules for this and super when used with parent and subclass members.

A

x

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

What does the compiler check when overriding a method?

A

Compiler checks:

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

Explain covariance

A

x

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

Can the child method have an exception if the parent method does not?

A

No, it can’t. Code will not compile.

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

Can the child method have an exception if the parent method does not?

A

No, it can’t. (Except runtime exceptions) Code will not compile. Breaks rules for overriding methods.

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

Can private methods be overridden in the subclass?

A

No. Private means no other classes have access. Just redeclare the method in the subclass and design it however you want.

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

What are the five rules for hiding a method?

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).
Note that the fi rst four are the same as the rules for overriding a method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the five rules for hiding a method?

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).
Note that the first four are the same as the rules for overriding a method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can a final method be overridden?

A

No.

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

Can a static final method be overridden/hidden?

A

No, final cannot be subclassed

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

Can a class be abstract?

A

yes.

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

T or F In Java, the fi rst statement of every constructor is either a call to another constructor
within the class, using this(), or a call to a constructor in the direct parent class, using super().

A

true

18
Q

x

A

At runtime the child version of an overridden method is always executed for an instance
regardless of whether the method call is defined in a parent or child class method. In this
manner, the parent method is never used unless an explicit call to the parent method is
referenced, using the syntax ParentClassName.method(). Alternatively, at runtime the parent
version of a hidden method is always executed if the call to the method is defined in the
parent class.

19
Q

What does hiding a variable mean and how do you do it?

A

When you hide a variable, you defi ne 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 defi ned for the parent reference and another defi ned for the child reference.

20
Q

Can a variable be overridden?

A

you can’t override a variable; you can only hide it.

21
Q

Does an abstract class have to have abstract methods?

A

No.

22
Q

Can an abstract METHOD be in a class that is not abstract?

A

No.

23
Q

What is the correct way to write an abstract method so there is NO BODY?

A

modifier abstract return type methodName();
public abstract void swim();
DO NOT put braces
public abstract void swim(){ } // doesn’t compile. can’t have braces

24
Q

Does a concrete class have to implement all the abstract methods of the abstract class it extends?

A

Yes.

public abstract class Animal {
public abstract String getName();
}
public class Walrus extends Animal { // DOES NOT COMPILE because it didn't implement the abstract class
}
25
Q

What are the rules for defining abstract classes?

A

Abstract Class Definition Rules:
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.

26
Q

What are the rules for defining abstract methods?

A

Abstract Method Defi nition Rules:
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.

27
Q

What are the rules for defining an interface?

A

Defining an Interface
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.
The fourth rule doesn’t apply to inner interfaces, although inner classes and interfaces
are not in scope for the OCA exam.

28
Q

What are the rules for inheriting an interface?

A

Inheriting an Interface
There are two inheritance rules you should keep in mind when extending an interface:
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.

29
Q

What are the rules for interface variables?

A

public static final All of them, all the time. And they must be initialized on the line of declaration since they are final.

30
Q

Describe a default method in an interface

A

A default method is a method defined 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.

31
Q

Is a default method in an interface public?

A

yes. Because all methods within an interface are assumed to be public, the access modifier for a default method is therefore public.

32
Q

What are the default interface method rules?

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

What are the rules for static interface methods?

A

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

What is a virtual method?

A

A virtual method is a method in which the specific implementation is not determined until runtime.

35
Q
T or F  A subclass cannot declare an overridden method with a new or broader
exception than in the superclass, since the method may be accessed using a reference to
the superclass.
A

true

36
Q

Rules for method overriding

A

1) a method must have the same signature
2) be at least as accessible as the parent method (can’t be more restrictive)
3) must not declare any new or broader exceptions
4) must use covariant return types.

37
Q

Rules for hiding methods and variables

A

Understand the rules for hiding methods and variables.

  • When a static method is recreated in a subclass, it is referred to as method hiding.
  • When a variable name is reused in a subclass it is variable hiding.
  • In both situations, the original method or variable still exists and is used in methods that reference the object in the parent class.
  • For method hiding, the use of static in the method declaration must be the same between the parent and child class.
  • Finally, variable and method hiding should generally be avoided since it leads to confusing and diffi cult-to-follow code.
38
Q

Explain valid reference casting.

A

Recognize valid reference casting. An instance can be automatically cast to a superclass or interface reference without an explicit cast. Alternatively, an explicit cast is required if the reference is being narrowed to a subclass of the object. The Java compiler doesn’t permit casting to unrelated types. You should be able to discern between compiler-time casting errors and those that will not occur until runtime and that throw a CastClassException.

39
Q

Explain the relationship of super for subclass constructors and instantiating subclasses.

A

If you don’t write some form of super(); in the child class constructor, the compiler will add “super();” for you. If you don’t have a no argument constructor defined in the parent class it will have a compiler error.

40
Q

How many other classes can a single class extend?

A

Each class can only extend ONE class

41
Q

Can a class be abstract and final? public abstract final myClass{ }

A

No, it can be one or the other, not both.