Chapter 5: Class Design Flashcards

1
Q

What is inheritance?

A

The process by which the subclass automatically 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 child X inherits from class Y, which in turn inherits from class Z, then what is X in relation to Z?

A

An indirect child or descendent.

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

What’s the difference between single inheritance and multiple inheritances? Does Java support both?

A

Single inheritance is when the the child and parent relationship is only one level. For example, you would not be able to inherit from a grandparent in this case.

Multiple inheritance is when a class can extend multiple parents.

Java does not support multiple inheritance, but it does support single inheritance.

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

Does Java support multiple levels of single inheritance? Can a child inherit from a grandparent?

A

yes

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

What’s the single exception to single inheritance?

A

Interfaces, a class can implement as many interfaces as you want.

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

What’s one way in java to prevent a class from being extended?

A

By marking it with the final modifier

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

What’s the syntax for inheritance?

A

public class ElephantSeal extends Seal{

}

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

Do children have access to private members? (fields and methods)

A

no

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

Which takes up more memory? the parent or the child?

A

The child. Because the child is both the parent AND more.

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

How does the default access modifier differ from the public keyword?

A

Public is available for any class to be used. Default access is only available to be used within the same package. Which differs from protected in that default access is not extended by inheritance. So a child does not have access to default access unless they parent and the child are in the same package.

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

Does this compile (in the same file)?

class Rodent {}

public class Groundhog extends Rodent{}

A

Yes. Fine as only one of the classes is marked public.

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

Does this compile (in the same file)?

public class Rodent {}

public class Groundhog extends Rodent{}

A

no. only one public class allowed per file.

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

True or false: There can be at most one public class or interface in a Java file?

A

True

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

What’s the only class in Java that doesn’t have any parent classes?

A

java.lang.Object

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

Does every class have at least one constructor?

A

Yes. There is a default no-argument constructor no matter what.

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

What is a super() constructor? Can it have arguments?

A

it is the constructor of any parent class. It must have arguments if the constructor has arguments.

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

Will this compile? Why or why not?

public class Zoo{

public Zoo(){

System.out.println(“Zoo created”);

super();

}

}

A

No. LIke the this() command, super() must be the first statement used in a constructor.

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

Will this compile? Why or why not?

public class Zoo{

public Zoo(){

super();

System.out.println(“Zoo created”);

super();

}

}

A

No. super() can only be the first statement in a constructor. It cannot be first and second, or first and third.

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

If a parent has defined one constructor, is the child forced to use it?

A

yes

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

if a parent has declared two constructors, does the child need to use both of the constructors?

A

No. It may use either of the declared constructors, it may use either valid constructor from the parent.

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

How does Java handle the collision between parent and child classes which methods with matching signatures?

A

You can override the method, or refer explicitly to the parent version with super.() So, let’s say the child has the same method signature if you want it to do the exact same thing as the parent, just do super.(method) to get the same behavior.

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

What’s the order of operations here? (Assume App2 extends App, and App3 extends App2)

public static void main(String…strings ) {

App app = new App();

App2 app2 = new App2();

App3 app3 = new App3();

}

}

A

the first line will call App’s empty constructor.

The second line will again call App’s empty constructor. then it will call App2’s version of the empty constructor.

The third line will call App’s constructor, then App2’s constructor, and then App3’s constructor.

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

What are three limitations to the child being able to override a parent class? (other than, of course, the method signatures not matching)

A
  1. The method int he child class must be at least as accessible or more accessible than the method in the parent class.
  2. The method in the child class may not throw new exceptions or exceptions broader than the exception thrown in the parent class.
  3. If the method returns a value, it must be the same or a sublclass of the method in the parent clas.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Remember, the child method must be at least as accessible as the parent.

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

What does it mean that the return type of the parent method and the child method must be covariant?

A

It means that the overridden version of the parent’s method must return something similar to the return type of the parent. So if the parent’s method returns an int, the child’s method cannot return a string.

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

Anytime you see an overridden method on the exam, first check to make sure it is truly being overridden and not overloaded. After that, check the access modifiers, return types, and any exceptions defined in the method are compatible with one another.

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

If the parent method has a declared exception, and the child doesn’t, will the method compile? (Assuming everything else is the same?) (overridden method)

A

Yes. As long as no new exception is defined, this isn’t a problem.

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

True or fals: A child’s version of a parent’s method can hide or eliminate a parent’s method exception without issue. (overridden method)

A

True

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

Is the child (overridden method) allowed to throw a more specific exception than the parent method?

A

Yes. As long as the more specific exception is a subclass of the parent’s method.

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

Is a child method’s (overridden method)exception allowed to be more generic than the parent’s exception declaration?

A

No. It can only go from less specific to more specific, from the top down.

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

Can a child’s overridden version of a parent’s method declare a method exception when the parent’s does not?

A

No.

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

Is it possible for a child class to override a private method from it’s parent?

A

No. Private methods and fields or only accessible within the class.

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

What’s the difference between an overridden method and a hidden method?

A

Essentially the only difference is that because it’s static you don’t need to use the @Override annotation. The method is bound to the class, not an object. So as far as the parent/child knows, it isn’t really there.

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

What happens if you attempt to hide a method on a child, but the method isn’t marked static on both the parent and the child?

A

Will not compile. At that point, you need to make both methods static to hide it, or non static and override it.

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

Can you override a method if it is marked final?

A

No. Final methods cannot be overridden.

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

Can you override a hidden method if it has been marked final?

A

No.

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

Can you mark an overridden version of a parent’s method final on the child?

A

Yes. But if the parent is marked final you cannot override the method at all.

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

Why is it important to not that app2 build’s it’s own copy of app, and that app3 build it’s own copy of app2 and app, that are not the same as the copy made by app2?

public static void main(String…strings ) {

App app = new App(10);

App2 app2 = new App2(11);

App3 app3 = new App3(12);

System.out.println(“App’s number “ + app.number);

//prints it’s own number as well as it’s super’s number

app2. printNumbers();
app3. printNumbers();

}

}

A

It’s important to note because if you are making calls to get information from the super, you need to know that not one ur-super is made per program, that each child talks to. If you want fields or methods that are available, like an urtext, you need to make them static.

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

What 2 goals might make you want to provide an abstract class?

A
  1. You want to provide reusable methods and classes to developers in the parent class, yet force them to provide their own implementations.
  2. You don’t ever want someone to make an instance of that class, unless its a child.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What is an abstract class?

A

It’s a class marked with the abstract keyword that can’t be instantiated.

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

Can you have abstract methods on a non abstract class?

A

no

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

Will this compile?

public abstract class TestAbstract {

abstract public void printStuffMethod() {};

}

A

No. Abstract methods cannot have a method body as defined by the curly brackets.

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

Can you have non abstract methods on an abstract class?

A

Yes.

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

Can you have non abstract fields on an abstract class?

A

Yes

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

Can you have abstract methods inside a non abstract class?

A

no

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

Can you have abstract variables, methods, and classes?

A

No, you cannot have abstract variables. But you can have abstract methods and classes

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

Which of these wont compile:

public abstract class TestAbstract {

(1) public abstract int stuff;
(2) public int moreStuff;
(3) public void printStuffMethod() {};
(4) public abstract void printMoreStuffMethod() {};
(5) public void printEvenMoreStuff();
(6) public abstract void printEvenEvenMoreStuff();

}

A

1 won’t work because you can’t have an abstract field.

  1. this is just fine.

3 this is just fine

4 won’t work because abstract methods cant have a body

5 won’t work because it doesn’t have a body

6 this is fine

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

Does this compile?

public abstract class TestAbstract {

public int moreStuff;

public void printStuffMethod() {System.out.println(“in the abstract class”);};

public abstract void printMoreStuffMethod();

public abstract void printEvenEvenMoreStuff();

}

A

Yes. The int is fine because it’s not abstract. The method with a body is fine because its not abstract. The other abstract methods work because they don’t have a body.

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

Will this compile?

public abstract class whale{

protected abstract void sing();}

public class HumpbackWhale extends Whale{

private void sing(){System.out.println(“Humpback whale sings”);}

}

A

No. You cannot reduce the accessibility of a super’s method. In the super the method is protected, but in the child it is private.

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

When does an abstract class become useful?

A

When it is extended by a concrete subclass.

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

Does this compile?

public abstract class Animal{

public abstract String getName();}

public class Bird extends Animal{}

A

No. The first concrete class of an abstract subclass must implement the supers methods.

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

Why doesn’t this compile?

public abstract class Animal{

public abstract String getName();}

public class Walrus extends Animal{}

public abstract class Eagle extends Aniaml{}

A

This fails to compile because as Walrus is not abstract, and it is a first order descendent, it must implement the abstract methods of the parent. Because Eagle is abstract it compiles just fine.

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

Are abstract classes allowed to extend other abstract classes without implementing the super’s methods?

A

yes.

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

If an intermediate class (abstract OR concrete) provides an implementation for an abstract method, is that method inherited as a concrete method, or an abstract one? Does it matter whether or not the class providing the implementation is concrete or abstract?

A

It is inherited as a concrete method. It doesn’t matter whether the implementation comes from an abstract or concrete intermediary.

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

Assuming a concrete class extends this class, and that it is the first concrete class to extend it, which of the following methods must be implemented on the concrete child?

public abstract class TestAbstract {

public int moreStuff;

public void printStuffMethod() {System.out.println(“in the abstract class”);};

public abstract void printMoreStuffMethod();

public abstract void printEvenEvenMoreStuff();

}

A

All but the printStuffMethod(). As it already has an implementation, the child doesn’t need to provide a new one.

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

If this class were extended by an abstract class, which of the following methods would need to be overridden?

public abstract class TestAbstract {

public int moreStuff;

public void printStuffMethod() {System.out.println(“in the abstract class”);};

public abstract void printMoreStuffMethod();

public abstract void printEvenEvenMoreStuff();

}

A

None. Abstract classes don’t really have to do anything with the methods on their abstract super.

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

How many methods does the class Lion need to implement?

public abstract class Animal{public abstract String getName();}

public abstract class BigCat extends Animal{public abstract void roar();}

public class Lion extends BigCat{}

A

Two. Because no implementation has previously been provided by the parent and the grandparent, Lion must do both.

58
Q

Why doesn’t this compile?

abstract class Test2{

public abstract void printMoreStuff() {};

}

A

Can’t have a body {} on an abstract method.

59
Q

Does this compile?

public class Main extends Test3 {

public static void main(String[] args) {

}

}

abstract class Test1{

public void printStuff() {System.out.println(“shiz”);}

}

abstract class Test2{

public abstract void printMoreStuff();

}

class Test3 extends Test2{

@Override

public void printMoreStuff() {

System.out.println(“Print More Stuff”);

}

}

A

yes

60
Q

Why doesn’t this comopile?

public class Main extends Test3 {

public static void main(String[] args) {

}

}

abstract class Test1{

public void printStuff() {System.out.println(“shiz”);}

}

abstract class Test2{

public abstract void printMoreStuff();

}

class Test3 extends Test2{

}

A

Test 3 needs to implement printMoreStuff();

61
Q

Why doesn’t this compile?

String name = “Jedd”;

switch (name){

case “Jedd”: System.out.println(“Name is equal to Jedd”);

case “Kade”: System.out.println(“Name is equal to Kade”);

case “Ben”: System.out.println(“Name is equal to Ben”);

case new String(“Tracy”) : System.out.println(“name is equal to Tracy”);

}

A

Case expressions must be constant expressions. So the ‘new’ keyword cant be used.

62
Q

What is the output?

int x = 1;

switch (x){

case 1: System.out.println(“x is equal to one”);

case 2: System.out.println(“x is equal to 2”);

case 3: System.out.println(“x is equal to three”);

default: System.out.println(“none of these”);

}

A

as there is no break statement, everything after case 1, the case that returned true, will run.

63
Q

What is the output?

int x = 5;

switch (x){

case 1: System.out.println(“x is equal to one”);

break;

case 2: System.out.println(“x is equal to 2”);

break;

case 3: System.out.println(“x is equal to three”);

break;

default: System.out.println(“none of these”);

case 4: System.out.println(“x is equal to 4”);

}

A

The default and case 4 will print out. Case 4 would not print out, however, if the default statement were followed by a break; statement.

64
Q

Can you use a continue statement inside of a switch statment?

A

Yes. But it must be within a for loop of sorts.

65
Q

can you make a List<t> of primitives? </t>

A

no, List<> can only work with objects.

66
Q

what is the output:

char y = ‘y’;

char z = ‘z’;

List<integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7);</integer>

switch (y) {

case ‘y’:

for (int i = 0; i < numbers.size(); i++) {

System.out.println(“y is equal to y”);

break;

}

break;

case ‘z’:

System.out.println(“y is equal to z”);

default:

System.out.println(“y isn’t equal to anything”);

}

}

A

y is equal to y

67
Q

what is the output?

char y = ‘y’;

char z = ‘z’;

List<integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7);</integer>

switch (y) {

case ‘y’:

for (int i = 0; i < numbers.size(); i++) {

System.out.println(“y is equal to y”);

continue;

}

break;

case ‘z’:

System.out.println(“y is equal to z”);

default:

System.out.println(“y isn’t equal to anything”);

}

A

y is equal to y

y is equal to y

y is equal to y

y is equal to y

y is equal to y

y is equal to y

y is equal to y

68
Q

Can you instantiate an abstract class directly?

A

No.

69
Q

How many abstract and/or non abstract methods can be defined on an abstract class?

A

As many as you want! Even zero

70
Q

Can abstract classes be marked private, protected, or final?

A

NO

71
Q

What happens to the abstract methods of an abstract class when it is extended by another abstract class?

A

It inherits all of the abstract methods as their own. So when the abstract child is extended by a concrete class, those methods are passed on even though they need not be defined on the abstract child.

72
Q

What must the first concrete class which inherits abstract methods from an abstract parent?

A

Implement the abstract methods. Remember, however, that if the methods are not abstract and, therefore, have definitions, the concrete child doesn’t have to do anything with those already implemented methods.

73
Q

Can you define an abstract method on a non-abstract class?

A

no

74
Q

Can you declare abstract methods as private or final?

A

no

75
Q

Can abstract methods have a body {}?

A

no

76
Q

Is implementing an abstract method similar to overriding, in that the method signatures must match?

A

yes

77
Q

How accessible does an implementation of an abstract method, or an overridden method, need to be?

A

At least as accessible as the method when it is first declared.

78
Q

How many interfaces is a class allowed to implement?

A

Any number.

79
Q

What is an interface?

A

An abstract data type that defines a list of abstract public methods that any class implementing the interface must provide.

80
Q

Are interfaces allowed to include a list of constant variables and default methods?

A

yes

81
Q

Why doesn’t this compile?

public interface TestInterface1 {

public int one = 1;

static int two = 2;

final int three = 3;

protected int four = 4;

}

A

only public, static, and final modifiers are allowed for interface constants.

82
Q

Why doesn’t this compile?

public interface TestInterface1 {

public int one = 1;

static int two = 2;

final int three = 3;

public int four = one + 4;

public String name = “Jedd”;

public abstract int getNumberOne();

public int getNumberFour() {return this.four};

public default int getNumberTwo() {return TestInterface1.two;}

public default int getNumberThree() {return three;}

public static int getNumbers() {return one;}

}

A

The methods of an interface must have either static or default in the signature. Otherwise it wont compile. The rest are fine.

83
Q

What part of an interface signature is assumed (not shown) in this example?

public interface Whatever{}

A

The abstract keyword.

84
Q

Will this compile?

protected abstract interface Whatever{}

A

No. Protected is not allowed. All interfaces must be public.

85
Q

Will this compile as the declaration of an interface?

interface Whatever{}

A

Yes. Public and abstract are both assumed for all interfaces.

86
Q

What’s the proper way to define multiple implementations of interfaces on a concrete class?

A

public class Elephent implmements Interface1, Interface2, Interface3 and so on.

87
Q

Can interfaces be instantiated directly?

A

no

88
Q

Is an interface required to have any methods or fields?

A

no

89
Q

Can an interface be marked as final?

A

no

90
Q

Can an interface be marked as private?

A

no

91
Q

What access is assumed for an interface?

A

public or default?

92
Q

What level of concreteness is assumed for an interface?

A

abstract

93
Q

Will making an interface private, protected, or final trigger a compiler error?

A

yes. But only if it’s a top level interface.

94
Q

Which two method signature modifiers are all non-default methods on an interface assumed to have?

A

public abstract

95
Q

What happens if you mark a method on an interface protected, private, or final?

A

This will trigger a compiler error.

96
Q

Which asssumed keywords are we not seeing?

public interface Whatever{

void fly(int speed);

abstract void takeOff();

public abstract double dive();

A

public and abstract will be added to each method by the compiler.

97
Q

Why doesn’t any of this compile?

private final interface CanCrawl{

private void dig(int depth);

protected abstract double depth();

public final void surface();

}

A

can’t mark an interface final or private.

cant mark a interface method private

can’t mark an interface method protected

can’t mark an interface method final

98
Q

Which of the methods from this interface will need to be implemented by concrete class?

public interface TestInterface1 {

public int one = 1;

static int two = 2;

final int three = 3;

public int four = one + 4;

public String name = “Jedd”;

public abstract int getNumberOne();

public default int getNumberTwo() {return TestInterface1.two;}

public default int getNumberThree() {return three;}

public static int getNumbers() {return one;}

}

A

Only getNumberOne(); The others are default or static.

99
Q

An interface that extends another interface, as well as an abstract class that implements an interface, ____ all of the abstract methods as its own abstract methods.

A

inherits

100
Q

Does an interface implement another interface or extend it?

A

Extend

101
Q

Does this compile?

public interface TestInterface3 extends TestInterface1, TestInterface2 {

}

A

yes

102
Q

Can abstract classes extend multiple other abstract classes?

A

no

103
Q

Can an interface extend more than one other interface? Why would you do that?

A

yes. To pass along the abstract methods to the child.

104
Q

The first concrete class that implements an interface, or extends an abstract class that implements an interface, must _____

A

provide an implementation for all of the inherited abstract methods

105
Q

What happens when an abstract class implements an interface?

A

The abstract class inherits the interface’s abstract methods but is not required to implement them.

106
Q

Can a class extend an interface?

A

no

107
Q

Will this compile?

public class Hello extends [interface]{

}

A

No. Only

108
Q

Finish this sentence: class ____ interface {}

A

implements

109
Q

Fill in the blank: interface ____ interface

A

extends

110
Q

What happens if a class implements two interfaces, each of which has a method with the same signatures as that on the other interface? Will there be a collision when they are defined by the concrete class?

A

No. you only have to provide one implementation and it will count for both.

111
Q

What happens when a concrete class implements two interfaces, each of which has methods of the same name with albeit different method signatures? Is the concrete class forced to provide to implementations, one for each method signature?

A

Yes. You must provide an implementation for each method signature override.

112
Q

Is it possible in Java to define two methods in a class with the same name and input parameters but different return types? Why could this present a problem if the same method name has different return types, implemented from two different interfaces?

A

no. This is a porblem because you may think you are overriding a method, but you aren’t. The method name is the same but the signatures are not the same.

113
Q

Interface variables are assumed to be ____, _____, and _____

A

public, static and final

114
Q

The ____ of an interface variable must be ____ when it is declared since it is marked as _____ implicitly.

A

value, set, final

115
Q

What is a default method?

A

A method defined within an interface with the default keyword in which a method body is provided

116
Q

What’s the difference between an interface’s default method and it’s other “regular” methods?

A

The default can provide an implementation, which means it is not abstract.

117
Q

Are classes required to override an interfaces default method?

A

No. But they can if they want to

118
Q

Can a default method be declared in an abstract or concrete class?

A

No. Only in interfaces

119
Q

Must the default method be marked with the default keyword?

A

yes

120
Q

Can a method marked default not provide a body?

A

no

121
Q

Is a default method assumed to be static, final, and abstract?

A

No. This is clear as it can be overridden by another class.

122
Q

Like all methods in an interface, a default method is assumed to be _____ and will not compile if marked as private or protected

A

public

123
Q

The type of the object determines which _____ exist within the object in memory.

A

properties

124
Q

The type of the ______ to the object determines which methods and variables are accessible to the Java program.

A

reference

125
Q

Why doesn’t this compile?

public class Main {

public static void main(Strings…strings) {

Harold harold = new Harold();

System.out.println(harold.getName());

Object object = harold;

System.out.println(object.getName());

}

}

class Harold{

protected String name = “Harold” ;

protected int age = 55;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

A

Because once the reference object takes over the reference harold pointing at the Harold object, harold becomes only that which Object has the power to do.

126
Q

What is the output?

public class Main {

public static void main(String…strings) {

Harold harold = new Harold();

Object objectHarold = harold;

Harold harold2 = (Harold) objectHarold;

harold2.setName(“ChangedName”);

System.out.println(objectHarold);

System.out.println(harold);

System.out.println(harold==objectHarold);

System.out.println(harold==harold2);

}

}

class Harold{

protected String name = “Harold” ;

protected int age = 55;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public String toString() {

return getName();

}

}

A

ChangedName

ChangedName

true

true

127
Q

Does casting an object from a subclass to a superclass require an explicit cast?

A

no

128
Q

Does casting an object from a superclass to a subclass require an explicit cast?

A

yes

129
Q

Will the compiler allow casts to unrelated types?

A

no

130
Q

When casting an object type, is it possible for the code to compile without throwing an error during compile time, yet have an exception be thrown at runtime? If yes, why?

A

Yes. The error will be thrown at runtime of the object being cast is not actually an instance of that class.

131
Q

What is a virtual method?

A

a method in which the specific implementation is not determined until run time.

132
Q

In fact, all non-final, non-static, and non-private methods are considered ____ methods, since any of them can be overridden at runtime.

A

Virtual method

133
Q

What is the output? And what is the principle demonstrated (hint: virtual methods)

public class Main {

public static void main(String… strings) {

Bird bird = new Peacock();

bird.displayInfo();

}

}

class Bird {

public String getName() {

return “Unknown”;

}

public void displayInfo() {

System.out.println(“The bird name is “ + getName());

}

}

class Peacock extends Bird{

public String getName() {

return “Peacock”;

}

}

A

The bird name is Peacock

Even though the Bird class defines a method getName(), a class extending Birdclass redefines a method of the same name, which returns a different String. This does not occur until runtime (aka when an instance of Peacock is created the method on the parent is redefined).

134
Q

Explain a polymorphic parameter

A

A polymorphic parameter refers to the ability of subclasses to be passed as parameters to a method when the parameter defined is that of the super class. Because the superclass is the parameter type, you can pass in a subclass of the super as a parameter.

135
Q

What is an exception?

A

Java’s way of saying i give up. I don’t know what to do, you need to fix something.

136
Q

What do exceptions fundamentally do?

A

Interrupt program flow

137
Q

Java has a ____ exception superclass for all objects.

A

Throwable

138
Q

What are three subclasses of throwable?

java. lang._______
java. lang.________

(child of the preceding exception class)

java.lang_______

A

java. lang.Error
java. lang.Exception
java. lang.RuntimeException

139
Q
A
140
Q
A
141
Q
A