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?
What are the rules of using this() inside a constructor to call another constructor?
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
What is the order of initialization of an instance?
Initialize instance of class X
What are the constructor rules?
What are the rules of method overriding?
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
Does the following code compile?
public class LongTailAnimal {
protected void chew(List input) {}
}
public class Anteater extends LongTailAnimal {
protected void chew(ArrayList input) {}
}
Yes. They are considered overloaded methods (not overridden) because the signature is not the same).
What is the difference between method overriding and method hiding?
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.
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();
}
}
Compile error
Cannot overload a method (eat()) where one is static and the other isn’t.
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();
}
}
Compile error
Cannot override a method where the access modifier in the child class is more restrictive than the one it inherits.
What does polymorphism mean?
The property of an object to take on many different forms.
What are the rules of casting objects?
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;
}
}
It will throw a ClassCastException.