Chapter 8 Flashcards

1
Q

How do we prevent a class to be extended?

A

Using the final modifier on the class.

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

What does the super keyword do?

example: super.label

A

Look for label references excluding the current class. It will only look at inherited classes.

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

Can a class have multiple constructors?

A

yes, as long as each constructor has a unique signature (paremeters must be different).

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

Can we call a constructor without the new keyword from outside the class?

A

no. You need to use the new keyword for a constructor to be called.

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

What does Java do when the new keyword is used?

A

It allocated memory for the new object and then looks for a constructor with a matching signature and calls it.

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

What is the default constructor?

A

Every class in Java has a constructor wether you code one or not. This Java-created constructor is called a default constructor.

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

What does a default constructor look like?

A

A default constructor is a Java created constructor without any parameters or body.

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

Multiple constructors in a class is called ..

A

constructor overloading.

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

How do we call an overloaded constructor from a constructor within the same class?

A

using this().

When this() is used with paranthesis, Java calls another constructor on the same instance of the class.

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

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

What is the difference between this and this()?

A

The this keyword refers to an instance of the class, while the this() keyword refers to a constructor call within the class.

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

What is the difference between super and super()?

A

The super keyword refers to members of the parent class, while the super() keyword calls a parent constructor.

19
Q

Can a class that doesn’t extend another class use the super() method?

A

Yes. It will call the constructor of the class Object.

20
Q

Will the following code compile?

public Zoo() {
    System.out.println("Zoo created");
    super();
}
A

No. The super() (and this()) method can only be called in the first line of the method.

21
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().

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

23
Q

A class may have multiple ancestors via inheritance. In the case of Mammal -> Elephant -> AfricanElephant, which class is referred when using super() in AfricanElephant?

A

The Elephant class.

super() always refers to the most direct parent.

24
Q

Can final variables be assigned after the constructor?

A

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

25
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.
26
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().
27
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.
28
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.
29
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.

30
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, it can only be package-private or public.

31
Q

Can a method in a parent class be overridden with a method that has a narrowed checked exception?

A

Yes.

32
Q

Can a method in a parent class be overridden with a method that returns a child object of the object that is returned in the parent method?

A

Yes.

33
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).

34
Q

Can generic methods be overloaded by just changing the generic parameter type?

A

No.

page 334

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

36
Q

What is the output?

public class Bear {
   public static void eat() {
      System.out.println("Bear is eating");
   }
}
public class Panda extends Bear {
   public static void main(String... hey) {
      eat();
   }
}
A

Bear is eating.

37
Q

What is the output?

public class Bear {
   public static 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

Panda is chewing

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

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

40
Q

What does a final method mean?

A

The method cannot be replaced by one in a child class.

41
Q

WHat does polymorphism mean?

A

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

42
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.
43
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.

page 343

44
Q

Can you use the this keyword to access a static variable or method?

A

Yes but it is not encouraged.