Inheritance

Java supports inheritance by allowing one class to to have access to its members and allows the subclass to add to (extends) the parent class. The extends keyword is used to inherit from another Java class.

Java supports single inheritance. A class can only have one parent class. If a class does not have a parent it automatically inherits from the Object class. This creates an inheritance hierarchy.

Inheritance Hierarchy

This is the class inheritance hierarchy of the Socket class.

Example

The Socket class we will be using to send and receive messages in the chat application inherits from the Emitter class. Here is how its defined :

public class Emitter {
    //some methods have been omitted for readability's sake
    public Emitter() {
    }

    public Emitter on(String event, Emitter.Listener fn) {
    }
    public Emitter off(String event) {
    }

    public Emitter emit(String event, Object... args) {
    }
}

The Socket class inherits from the Emitter class by using the extends keyword. Here is its definition :

public class Socket extends Emitter {}

The Socket class now have access to all accessible fields and methods from the Emitter class. This is how are able to call the on, off and emit methods even if they are not defined in the Socket class.

//on method is defined in the parent Emitter class
socket.on(Socket.EVENT_CONNECT, onConnected);

Definitions

  1. superclass - Also called the parent or base class,is the class from which other classes inherit from.
  2. subclass - Also called the child or extending class or the deriving class. This is the class that inherits from another class. It can also be the super class of another child class.

Private Members in a Superclass

Private members from the superclass are not inherited. Only public, default and preotected members are inherited.

UpCasting

Casting allows one object type to be converted to another. Since Object is the parent of any Java class, automatically any object is an object.

We can assign an Emitter object to a variable of type Object. This is called upcasting. The opposite is called downcasting.

Emitter emitter = new Emitter();
Object obj = emitter;

When upcasting, we are moving from a more specific object to a general one. In the above case, we will loose all the methods from the Emitter object.

Downcasting

To downcast we will have to be explicit that we need to convert the Object to the Emitter type. This can cause an exception if the wrong type is contained in the superclass.

// will work since the obj did not convert our Emitter object.
// It only contained it
emitter = (Emitter) obj;

Now we will be able to access the methods in the Emitter class since we are now using the specific reference.

What can you do in a Subclass

  1. The inherited fields can be used directly, just like any other fields.
  2. You can declare a field in the subclass with the same name as the one in the parent class, thus hiding it(not recommended).
  3. You can declare new fiels in the subclass that are not in the parent class.
  4. The inherited methods can be used directly as they are.
  5. You can write new instance methods in the subclass that has the same signature as the one in the parent class,(overriding).
  6. You can write new static methods in the subclass that has the same signature as the one in the superclass, thus hiding it.
  7. You can declare new methods in the subclass that are not in the parent class.
  8. You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

results matching ""

    No results matching ""