Fundamentals (Docs)



Excepting Object, which has no superclass, every class has one and only one direct superclass 
(single inheritance). 
In the absence of any other explicit superclass, every class is implicitly a subclass of Object.

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. 
Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass 
can be invoked from the subclass.

A subclass inherits all of the public and protected members of its parent, no matter what package the subclass
is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the 

	public class MountainBike extends Bicycle {

	public MountainBike myBike = new MountainBike();

MountainBike is descended from Bicycle and Object. Therefore, a MountainBike is a Bicycle and is also an 
Object, and it can be used wherever Bicycle or Object objects are called for.

The obj is both an Object and a Mountainbike. This is called implicit casting.

	Object obj = new MountainBike();

If, on the other hand, we write

	MountainBike myBike = obj;

we would get a compile-time error because obj is not known to the compiler to be a MountainBike. 

However, we can tell the compiler that we promise to assign a MountainBike to obj by explicit casting:

	MountainBike myBike = (MountainBike)obj;

This cast inserts a runtime check that obj is assigned a MountainBike so that the compiler can safely assume 
that obj is a MountainBike. If obj is not a Mountainbike at runtime, an exception will be thrown.

You can make a logical test as to the type of a particular object using the instanceof operator. 
This can save you from a runtime error owing to an improper cast. For example:
        if (obj instanceof MountainBike) {
	   MountainBike myBike = (MountainBike)obj;

Accessing Superclass Members (super)

If your method overrides one of its superclass's methods, you can invoke the overridden method through the use
 of the keyword super. 

	public class Superclass {
	    public void printMethod() {
	public class Subclass extends Superclass {
	    public void printMethod() { //overrides printMethod in Superclass
		System.out.println("Printed in Subclass");

The following example illustrates how to use the super keyword to invoke a superclass's constructor.

	public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) {
		super(startCadence, startSpeed, startGear);
		seatHeight = startHeight;

If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call
 to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, 
you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there 
is no problem.


You use the final keyword in a method declaration to indicate that the method cannot be overridden by 

	final ChessPlayer getFirstPlayer() {

Methods called from constructors should generally be declared final. If a constructor calls a non-final method, a 
subclass may redefine that method with surprising or undesirable results.


Abstract classes cannot be instantiated, but they can be subclassed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a 
semicolon), like this:

	abstract void moveTo(double deltaX, double deltaY);

If a class includes abstract methods, the class itself must be declared abstract, as in:

	public abstract class GraphicObject {
	   abstract void draw();

All of the methods in an interface are implicitly abstract, so the abstract modifier is not used with interface 
methods (it could be - it's just not necessary).

Abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to 
subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it 
should be declared as an interface instead.

Abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is 
subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also 
have some differences (the abstract methods).

An Abstract Class Example

	abstract class GraphicObject {
	    int x, y;
	    void moveTo(int newX, int newY) {
	    abstract void draw();
	    abstract void resize();

	class Circle extends GraphicObject {
	    void draw() {
	    void resize() {

Each non-abstract subclass of GraphicObject, such as Circle and Rectangle, must provide implementations for 
the draw and resize methods.