Ch8 Class Design Flashcards

1
Q

Given the following code, how do we print the variable in the parent class?

class Mammal {
String type = “mammal”;
}

public class Bat extends Mammal {
String type = “bat”;

public String printParentType(){
    return ... //code here
}  }
A

super.type

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

What is the difference between super and this?

A

The super reference is similar to the this reference, except that it excludes any members found in the current class.

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

Can we call a parent variable using this keyword?

A

yes, as long as the variable does not exist in the current class.

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

What does the this keyword do?

example: this.label

A

Look for label references including the current class. If it cant find any in the current class, it will try and look in inherited classes.

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

What are the constraints of a constructor?

A
  • The name must be the same of the class (case sensitive)
  • It cannot have a return type.
  • It cannot have a parameter of type var.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the rules of using this() inside a constructor to call another constructor?

A
  • There can only be one this() call
  • It must be the first statement in the method
  • It cannot refer to its own constructor (loop)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

True or false

The first line of every constructor MUST always be either this() or super().

A

True.

However, it is not required to code this. Java will automatically insert a call to the no-argument constructor super().

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

Does the following code compile?

public class Mammal {
public Mammal(int age) {}
}
public class Elephant extends Mammal {
}

A

No.

Since Elephant does not define any constructors, the Java compiler will attempt to insert a default no-argument constructor. It will also insert a call to super() as the first time on the constructor.

Since Mammal does not have a no-argument constructor and no default no-arg constructor is inserted by Java, it does not compile.

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

Can final variables be assigned after the constructor?

A

No. All final variables must be assigned by the time the constructor completes.

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

What is the order of initialization of a class?

A

Initialize class X

  1. If there is a superclass Y of X, then initialize class Y first.
  2. Process all static variable declarations in the order they appear in the class.
  3. Process all static initializers in the order they appear in the class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the order of initialization of an instance?

A

Initialize instance of class X

  1. If there is a superclass Y of X, then initialize the instance of class Y first.
  2. Process all instance variable declarations in the order they appear in the class.
  3. Process all instance initializers in the order they appear in the class.
  4. Initialize the constructor including any overloaded contructors referenced with this().
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the constructor rules?

A
  • The first statement of every constructor is a call to an overloaded constructor via this(), or a direct parent constructor via super().
  • If the first statement of a constructor is not a call to this() or super(), then the compiler will insert a no-argument super() as the first statement of the constructor.
  • Calling this() and super() after the first statement of a constructor results in a compiler error
  • If the parent class doesn’t have a no-argument constructor, then every constructor in the child class must start with an explicit this() or super() constructor call.
  • If the parent class doesn’t have a no-argument constructor and the child doesn’t define any constructors, then the child class will not compile.
  • If a class only defines private constructors, then it cannot be extended by a top-level class.
  • All final instance variables must be assigned a value exactly once by the end of the contstructor. Any final instance variables not assigned a value will be reported as a compiler error on the line the constructor is declared.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the rules of method overriding?

A
  1. The method in the child class must have the same signature of the method in the parent class.
  2. The method in the child class must be at least as accessible as the method in the parent class.
  3. The method in the child class may not declare a checked exception that is new or broader than the class of any exception declared in the parent class method.
  4. If the method returns a value, it must be the same or a subtype 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
14
Q

What will happen if super is not used in method overriding when it is neccessary to call the super method?

A

It will produce a StackOverflowError at runtime.

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

Can a method in a parent class be overridden with a more restrictive access modifier?

A

No.

Example: If the method in the parent class is package-private(Default), it can only be package-private, Protected or Public.

Leel of restictiveness:
1. Most restrictive: Private
1. Default
1. Proected
1. Least restrictive: Public

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

Does the following code compile?

public class LongTailAnimal {
protected void chew(List input) {}
}
public class Anteater extends LongTailAnimal {
protected void chew(ArrayList input) {}
}

A

Yes. They are considered overloaded methods (not overridden) because the signature is not the same).

17
Q

What is the difference between method overriding and method hiding?

A

it is method hiding if the two methods are marked static and method overriding if they are not marked static.

If one is marked static and the other isn’t, it will not compile.

18
Q

What is the output?

public class Bear {
public void eat() {
System.out.println(“Bear is eating”);
}
}
public class Panda extends Bear {
public static void eat() {
System.out.println(“Panda is chewing”);
}
public static void main(String… hey) {
eat();
}
}

A

Compile error

Cannot overload a method (eat()) where one is static and the other isn’t.

19
Q

What is the output?

public class Bear {
public static void eat() {
System.out.println(“Bear is eating”);
}
}
public class Panda extends Bear {
protected static void eat() {
System.out.println(“Panda is chewing”);
}
public static void main(String… hey) {
eat();
}
}

A

Compile error

Cannot override a method where the access modifier in the child class is more restrictive than the one it inherits.

20
Q

What does polymorphism mean?

A

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

21
Q

What are the rules of casting objects?

A
  1. Casting a reference from a subtype to a supertype doesn’t require an explicit cast.
  2. Casting a reference from a supertype to a subtype requires an explicit cast.
  3. The compiler disallows casts to an unrelated class.
  4. At runtime, in invalid cast of a reference to an unrelated type results in a ClassCastException being thrown.
22
Q

What does the following code output?

public class Rodent {}

public class Capybara extends Rodent {
public static void main(String[] args) {
Rodent rodent = new Rodent();
Capybara capybara = (Capybara) rodent;
}
}

A

It will throw a ClassCastException.