IS-A
Relationship :-
The term it self indicates that "Tight Coupling" of two things.In java Sub class is a part of super class.This IS-A relationship is achieved through Inheritance,Interface concept.
The term it self indicates that "Tight Coupling" of two things.In java Sub class is a part of super class.This IS-A relationship is achieved through Inheritance,Interface concept.
IS-A
is a way of saying, "this thing is a type of that thing."
For example, a Mustang is a type of horse, so in O O terms we
can say, "Mustang IS-A Horse." Subaru IS-A Car. Broccoli
IS-A Vegetable (not a very fun one, but it still counts). You express
the IS-A relationship in Java through the keyword.
Example
:consider following code
public
class Parent
{
public
void display() //method in super class
{
System.out.println("Display from parent");
}
}//super
class
public
class Child extends Parent
{
public
void childDisplay() //display method in child class
{
System.out.println("Display from child");
}
}//child
class
class
Demo //it contains the main method
{
public
static void main(String[] args)
{
Child
ch1=new Child();
Demo
d=new Demo();
d.doDisplay(ch1);
}
public
void doDisplay(Parent p)
{
p.display();
}
}//demo
class
output:
Display From Parent
"Child Extends Parent means Child IS-A Parent"
Consider
another example:
Imagine
we now have two specialized subclasses that extend the more generic
GameShape class, PlayerPiece and TilePiece:
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
}
Now
imagine a test class has a method with a declared argument type of
GameShape, that means it can take any kind of GameShape. In other
words,any subclass of GameShape can be passed to a method with an
argument of type
GameShape.
This 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
The
key point is that the doShapes() method is declared with a GameShape
argument but can be passed any subtype (in this example, a subclass)
of GameShape.The method can then invoke any method of GameShape,
without any concern for the actual runtime class type of the object
passed to the method. There are implications, though. The doShapes()
method knows only that the objects are a type of GameShape, since
that's how the parameter is declared. And using a reference variable
declared as type GameShape—regardless of whether the variable is a
method parameter, local variable, or instance variable—means that
only the methods of GameShape can be invoked on it. The
methods you can call on a reference are totally dependent on the
declared type of the variable, no matter what
the
actual object is, that the reference is referring to. That means you
can't use a GameShape variable to call, say, the getAdjacent() method
even if the object passed in is of type TilePiece.
2 comments:
very nice ajay,,,and i m fond of ur posts
i am looking forward to some other topics from you.
Post a Comment