public class ParentClass
STATIC variables and STATIC functions
- public, protected and default static methods
- private static variables
- private static methods
CONSTRUCTORS
- A constructor is used to make the object.
INSTANCE variables and functions
- public, protected and default methods
- private variables
- private methods
↓
Everything that is not in black gets copied to a subclass.
* Private variables and methods cannot be accessed at all by subclasses.
* Parent class constructors can be accessed by using the word super().
↓
public class SubClass extends ParentClass
Everything inherited from the parent class is here.
Also:
* constructors for this class
* new static variables and static methods
* new instance variables and methods
Cosntructors in subclasses
When you make a subclass Java first has to make an object of the parent class and then change it to be the subclass object.
This means that you always, somehow, run a constructor for the parent class.
(All classes are subclasses of the Object class -- so if they don't extend some other class, then they run the default constructor in the object class.)
If the parent class doesn't have any constructors, the default constructor from the Object class is run.
Example:
public class Animal { }
public class Cat extends Animal { }You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor. Note: If another class cannot call a MyClass constructor, it cannot directly create MyClass objects.
Overriding
A subclass can modify the methods used in the parent class. (This is one of the main reasons for making a subclass.)
For example, in the parent class class mammal {
there may be a method move()
.
This method would describe how a mammal moves. But in the BAT subclass (class Bat extends Mammal {
), you need to write your own
move() method that is used by the bats for flying. This happens automatically.
Bat b = new Bat(); b.move();Java first looks in the Bat class for the method move(). If it finds it, then it runs it. If it does not find it, then it looks in the parent class (mammal). If it finds it there, then it runs the mammal version. This continues with the parent class of Mammal, all the way up to the Object class. If there is no move() method in any of them, then you get an error at compile time. When you write a method that overrides a parent method, it is good form to use the annotation @Override . This checks to see that there really is a method in the parent class with the same name. This is extremely useful for catching typos.
Example
@Override void move() { //... the bat flies through the trees }
Note that @Override annotation is just a compile-time check. It is not a line of Java code and it does not affect how your program runs.
If you wanted, for some reason, to run the move() method in the parent class instead of the one in the current class, you need to use the super
keyword: super.move()
Overriding variables
still to be written