Java: The refresher | PART 2 | #118
Serious polymorphism
To exploit polymorphism, we need interfaces.
What’s an interface? It’s a 100% abstract class. What’s an abstract class? It’s a class that can’t be instantiated.
Every class in Java extends class Object.\
Class Object is the mother of all classes; it’s the superclass of everything. Any class that doesn’t explicitly extend another class, implicitly extends Object.
The compiler checks the class of the reference variable, not the class of the actual object at the other end of the reference.
Interfaces
A Java interface solves the problem of multiple inheritance.
How to define an interface?
public interface pet {...}
How to implement the interface?
public class Dog extends Canine implements pet {...}
All interface methods are abstract, so they MUST end in semicolons. Remember, they have no body!
Example interfaces
public interface Pet {
public abstract void beFriendly();
public abstract void play();
}
Classes from different inheritance trees can implement the same interface.
For example, consider you have a dog robot; it can extend the Robot class and still have the same interface of a pet.
A class can implement multiple interfaces, too:
public class Dog extends Animal implements Pet, Saveable, Paintable { ... }
Life and death of an object
All objects live on the garbage-collectible heap.
The two kinds of variables whose lives we care about now are instance variables and local variables(stack variables).
Instance variables are declared inside a class but not inside a method. Local variables are declared inside a method, including method parameters.
Instance variables are assigned a default value, even when you don’t explicitly assign one. The default values are 0/0. 0/false for primitives, and null for references.
When you call a method, the method lands on the top of a call stack. That new thing that’s actually pushed onto the stack is the stack frame, and it holds the state of the method including which line of code is executing, and the values of all local variables.
The method at the top of the stack is always the currently running method for that stack. A method stays on the stack until the method hits its closing curly brace.
Constructors
A constructor must have the same name as the class, and must not have a return type. The key feature of a constructor is that it runs before the object can be assigned to a reference.
If you don’t put a constructor in your class, the compiler will put in a default constructor. The default constructor is always a no-arg constructor
public class Duck {
int size;
// constructor will have the same name as the class name
public Duck(int duckSize) {
System.out.println("Quack");
size = duckSize;
System.out.println("size is " + size);
}
}
Some time we would need a constructor without a known argument. That is create an object with unknown arguments as well as sometimes with known argument.
Overloaded constructors means you have more than one constructor in your class. To compile, each constructor must have a different argument list!
Numbers matter
Methods in the Math class don’t use any instance variable values. And because the methods are “static,” you don’t need to have an instance of Math. All you need is the Math class.
The keyword static lets a method run without any instance of the class.
A class with static methods is not meant to be instantiated. You can restrict other code from instantiating a non-abstract class by marking the constructor private.
A constructor when marked private can be instantiated or invoked by the methods within the class.
Static methods run without knowing about any particular instance of the static method’s class.
Do not call static methods using reference variable, use class name instead
Static variable: value is the same for ALL instances of the class.
Static variables are initialized when a class is loaded. A class is loaded because the JVM decides it’s time to load it.
And there are two guarantees about static initialization:
Static variables in a class are initialized before any object of that class can be created.
Static variables in a class are initialized before any static method of the class runs.
A static initializer is a block of code that runs when a class is loaded, before any other code can use the class, so it’s a great place to initialize a static final variable.
class ConstantInit1 {
final static int X;
static {
X = 42;
}
}