Understanding Inheritance
SINGLE VS. MULTIPLE INHERITANCE
How to prevent a class from being extended?
INHERITING OBJECT
Creating Classes
EXTENDING A CLASS
public abstract class ElephantSeal extends Seal {
// Methods and Variables defined here
}public or default (package-private) access modifierabstract or final keyword (optional)class keyword (required)extends parent class (optional)APPLYING CLASS ACCESS MODIFIERS
ACCESSING THE THIS REFERENCE
public class Flamingo {
private String color;
public void setColor(String color) {
color = color;
}
} Java uses the most granular scope, so when it sees color = color, it thinks you are assigning the method parameter value to itself.
instance method,constructor,instance initializer block.public void setColor(String color) {
this.color = color;
}CALLING THE SUPER REFERENCE
super reference refers the members accessible via inheritance.Declaring Constructors
CREATING A CONSTRUCTOR
public class Bunny {
public Bunny() { //constructor name match class name and not return type
System.out.println("constructor");
}
}constructor overloading.DEFAULT CONSTRUCTOR
private constructors
> [!NOTE]
Having only private constructors in a class tells the compiler not to provide a default no-argument constructor.It also prevents other classes from instantiating the class.This is useful when a class has only static methods or the developer wants to have full control of all calls to create new instances of the class.Remember, static methods in the class, including a main() method, may access private members, including private constructors.
CALLING OVERLOADED CONSTRUCTORS WITH this()
new before the name of the constructor.this() to calls another constructor on the same instance of the class.this() call must be the first statement in the constructor.this() in any constructorThere’s one last rule for overloaded constructors you should be aware of.
public class Gopher {
public Gopher(int dugHoles) {
this(5); // DOES NOT COMPILE
}
}public class Gopher {
public Gopher() {
this(5); // DOES NOT COMPILE
}
public Gopher(int dugHoles) {
this(); // DOES NOT COMPILE
}
}THIS VS. THIS()
CALLING PARENT CONSTRUCTORS WITH SUPER()
this(), or a call to a constructor in the direct parent class, using super().super() call also takes arguments.SUPER VS. SUPER()
super, is used to reference members of the parent class,super(), calls a parent constructor.Understanding Compiler Enhancements
Java compiler automatically inserts a call to the no-argument constructor super() if you do not explicitly call this() or super() as the first line of a constructor.
ARE CLASSES WITH ONLY PRIVATE CONSTRUCTORS CONSIDERED FINAL?
Missing a Default No-Argument Constructor
What happens if the parent class doesn’t have a no-argument constructor?
public class Mammal {
public Mammal(int age) {}
}
public class Elephant extends Mammal { // DOES NOT COMPILE
}SUPER() ALWAYS REFERS TO THE MOST DIRECT PARENT
For constructors, though, super() always refers to the most direct parent.
CONSTRUCTORS AND FINAL FIELDS
final follow similar rules. They can be assigned values inlocal final variables, which are not required to have a value unless they are actually used, final instance variables must be assigned a value.> [!NOTE]
On the exam, be wary of any instance variables marked final.
Make sure they are assigned a value in the line where they are declared, in an instance initializer, or in a constructor.
They should be assigned a value only once,
and failure to assign a value is considered a compiler error in the constructor.
We can assign a null value to final instance variables, so long as they are explicitly set.
line where they are declared,instance initializer,constructor.null value to final instance variables, so long as they are explicitly set.