Sunday 6 January 2013

Chapter 2: Object Orientation:Inheritance, Is-A, Has-A

Inheritance is everywhere in Java.
In order to explore this topic we're going to use the instanceof operator.
For now, just remember that instanceof returns true if the reference variable being tested is of the type being compared to.

package net.dharmaraj.scjp;

class Test
{
    public static void main(String[] args)
    {
        Test t1 = new Test();
        Test t2 = new Test();
        if (!t1.equals(t2))
            System.out.println("they're not equal");
        if (t1 instanceof Object)
            System.out.println("t1's an Object");
    }
}

Produces the output:
they're not equal
t1's an Object

common reasons to use inheritance are
- To promote code reuse
- To use polymorphism

package net.dharmaraj.scjp;

class GameShape
{
    public void displayShape()
    {
        System.out.println("displaying shape");
    }
    // more code
}

class PlayerPiece extends GameShape
{
    public void movePiece()
    {
        System.out.println("moving game piece");
    }
    // more code
}

public class TestShapes
{
    public static void main(String[] args)
    {
        PlayerPiece shape = new PlayerPiece();
        shape.displayShape();
        shape.movePiece();
    }
}

Outputs:
displaying shape
moving game piece



package net.dharmaraj.scjp;

class GameShape
{
    public void displayShape()
    {
        System.out.println("displaying shape");
    }
    // more code
}

class PlayerPiece extends GameShape
{
    public void movePiece()
    {
        System.out.println("moving game piece");
    }
    // more code
}

class TilePiece extends GameShape
{
    public void getAdjacent()
    {
        System.out.println("getting adjacent tiles");
    }
    // more code
}

public class TestShapes
{
    public static void main(String[] args)
    {
        PlayerPiece player = new PlayerPiece();
        TilePiece tile = new TilePiece();
        doShapes(player);
        doShapes(tile);
    }

    public static void doShapes(GameShape shape)
    {
        shape.displayShape();
    }
}


Outputs:
displaying shape
displaying shape

IS-A and HAS-A Relationships

IS-A
In OO, the concept of IS-A is based on class inheritance or interface
implementation.You express the IS-A relationship in Java through the keywords extends
(for class inheritance) and implements (for interface implementation).

package net.dharmaraj.scjp;

public class Car
{
    // Cool Car code goes here
}

class Maruti extends Car
{
    // Important Maruti-specific stuff goes here
    // Don't forget Maruti inherits accessible Car members which
    // can include both methods and variables.
}

A Car is a type of Vehicle, so the inheritance tree might start from the Vehicle
class as follows:

public class Vehicle { ... }
public class Car extends Vehicle { ... }
public class Maruti extends Car { ... }

"Car extends Vehicle" means "Car IS-A Vehicle."
"Maruti extends Car" means "Subaru IS-A Car."


HAS-A
HAS-A relationships are based on usage, rather than inheritance. In other words,
class A HAS-A B if code in class A has a reference to an instance of class B. For
example, you can say the following,

A Horse IS-A Animal. A Horse HAS-A Halter.
The code might look like this:

package net.dharmaraj.scjp;

public class Animal
{
}

public class Horse extends Animal
{
    private Halter myHalter;
}









package net.dharmaraj.scjp;

public class Horse extends Animal
{
    private Halter myHalter;

    public void tie(LeadRope rope)
    {
        myHalter.tie(rope); // Delegate tie behavior to the
        // Halter object
    }
}

public class Halter
{
    public void tie(LeadRope aRope)
    {
        // Do the actual tie work here
    }
}

No comments:

Post a Comment