Ch8 Class Design Flashcards
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 } }
super.type
What is the difference between super and this?
The super reference is similar to the this reference, except that it excludes any members found in the current class.
Can we call a parent variable using this keyword?
yes, as long as the variable does not exist in the current class.
What does the this keyword do?
example: this.label
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.
What are the constraints of a constructor?
- 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.
What are the rules of using this() inside a constructor to call another constructor?
- There can only be one this() call
- It must be the first statement in the method
- It cannot refer to its own constructor (loop)
True or false
The first line of every constructor MUST always be either this() or super().
True.
However, it is not required to code this. Java will automatically insert a call to the no-argument constructor super().
Does the following code compile?
public class Mammal {
public Mammal(int age) {}
}
public class Elephant extends Mammal {
}
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.
Can final variables be assigned after the constructor?
No. All final variables must be assigned by the time the constructor completes.
What is the order of initialization of a class?
Initialize class X
- If there is a superclass Y of X, then initialize class Y first.
- Process all static variable declarations in the order they appear in the class.
- Process all static initializers in the order they appear in the class.
What is the order of initialization of an instance?
Initialize instance of class X
- If there is a superclass Y of X, then initialize the instance of class Y first.
- Process all instance variable declarations in the order they appear in the class.
- Process all instance initializers in the order they appear in the class.
- Initialize the constructor including any overloaded contructors referenced with this().
What are the constructor rules?
- 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.
What are the rules of method overriding?
- The method in the child class must have the same signature of the method in the parent class.
- The method in the child class must be at least as accessible as the method in the parent class.
- 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.
- 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.
What will happen if super is not used in method overriding when it is neccessary to call the super method?
It will produce a StackOverflowError at runtime.
Can a method in a parent class be overridden with a more restrictive access modifier?
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